Java
Attention
This page documents an earlier version. Go to the latest (v2.3) version.Maven
To build your Java application using YugabyteDB's version of the Jedis driver, add the following Maven dependency to your application:
<dependency>
<groupId>com.yugabyte</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0-yb-16</version>
</dependency>
Working Example
Pre-requisites
This tutorial assumes that you have:
- installed YugabyteDB, created a universe and are able to interact with it using the Redis shell. If not, please follow these steps in the quick start guide.
- installed JDK version 1.8+ and maven 3.3+
Creating the maven build file
Create a maven build file pom.xml
and add the following content into it.
<?xml version="1.0"?>
<!-- Copyright (c) Yugabyte, Inc. -->
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yugabyte.sample.apps</groupId>
<artifactId>hello-world</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.yugabyte</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0-yb-16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Writing a HelloWorld Redis app
Create the appropriate directory structure as expected by maven.
$ mkdir -p src/main/java/com/yugabyte/sample/apps
Copy the following contents into the file src/main/java/com/yugabyte/sample/apps/YBRedisHelloWorld.java
.
package com.yugabyte.sample.apps;
import java.util.HashMap;
import java.util.Map;
import redis.clients.jedis.Jedis;
public class YBRedisHelloWorld {
public static void main(String[] args) {
try {
// Create a Jedis client.
Jedis jedisClient = new Jedis("127.0.0.1");
// Prepare the employee information to insert.
String <span style="color:#19177c">userid</span> <span style="color:#666">=</span> <span style="color:#ba2121">"1"</span>;
Map<String, String> <span style="color:#19177c">userProfile</span> <span style="color:#666">=</span> new HashMap<String, String><span style="color:#666">()</span>;
userProfile.put<span style="color:#666">(</span><span style="color:#ba2121">"name"</span>, <span style="color:#ba2121">"John"</span><span style="color:#666">)</span>;
userProfile.put<span style="color:#666">(</span><span style="color:#ba2121">"age"</span>, <span style="color:#ba2121">"35"</span><span style="color:#666">)</span>;
userProfile.put<span style="color:#666">(</span><span style="color:#ba2121">"language"</span>, <span style="color:#ba2121">"Redis"</span><span style="color:#666">)</span>;
// Insert the data.
String <span style="color:#19177c">result</span> <span style="color:#666">=</span> jedisClient.hmset<span style="color:#666">(</span>userid, userProfile<span style="color:#666">)</span>;
System.out.println<span style="color:#666">(</span><span style="color:#ba2121">"HMSET returned "</span> + result + <span style="color:#ba2121">": id=1, name=John, age=35, language=Redis"</span><span style="color:#666">)</span>;
// Query the data.
Map<String, String> <span style="color:#19177c">userData</span> <span style="color:#666">=</span> jedisClient.hgetAll<span style="color:#666">(</span>userid<span style="color:#666">)</span>;
System.out.println<span style="color:#666">(</span><span style="color:#ba2121">"Query result: name="</span> + userData.get<span style="color:#666">(</span><span style="color:#ba2121">"name"</span><span style="color:#666">)</span> +
<span style="color:#ba2121">", age="</span> + userData.get<span style="color:#666">(</span><span style="color:#ba2121">"age"</span><span style="color:#666">)</span> + <span style="color:#ba2121">", language="</span> + userData.get<span style="color:#666">(</span><span style="color:#ba2121">"language"</span><span style="color:#666">))</span>;
// Close the client.
jedisClient.close<span style="color:#666">()</span>;
<span style="color:#666">}</span> catch <span style="color:#666">(</span>Exception e<span style="color:#666">)</span> <span style="color:#666">{</span>
System.err.println<span style="color:#666">(</span><span style="color:#ba2121">"Error: "</span> + e.getMessage<span style="color:#666">())</span>;
<span style="color:#666">}</span>
}
}
Building and running the app
To build the application, just run the following command.
$ mvn package
To run the program, do the following.
% java -cp "target/hello-world-1.0.jar:target/lib/*" com.yugabyte.sample.apps.YBRedisHelloWorld
You should see the following as the output.
HMSET returned OK: id=1, name=John, age=35, language=Redis
Query result: name=John, age=35, language=Redis