Develop Java Apps

Maven

To build your Java application using the YugabyteDB version of the Jedis driver, add the following Maven dependency to your application:

  1. <dependency>
  2. <groupId>com.yugabyte</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>2.9.0-yb-16</version>
  5. </dependency>

Working example

Prerequisites

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.

  1. <?xml version="1.0"?>
  2. <!-- Copyright (c) Yugabyte, Inc. -->
  3. <project
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
  5. xmlns="http://maven.apache.org/POM/4.0.0"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  7. <modelVersion>4.0.0</modelVersion>
  8. <groupId>com.yugabyte.sample.apps</groupId>
  9. <artifactId>hello-world</artifactId>
  10. <version>1.0</version>
  11. <packaging>jar</packaging>
  12. <dependencies>
  13. <dependency>
  14. <groupId>com.yugabyte</groupId>
  15. <artifactId>jedis</artifactId>
  16. <version>2.9.0-yb-16</version>
  17. </dependency>
  18. </dependencies>
  19. <build>
  20. <plugins>
  21. <plugin>
  22. <groupId>org.apache.maven.plugins</groupId>
  23. <artifactId>maven-dependency-plugin</artifactId>
  24. <version>2.1</version>
  25. <executions>
  26. <execution>
  27. <id>copy-dependencies</id>
  28. <phase>prepare-package</phase>
  29. <goals>
  30. <goal>copy-dependencies</goal>
  31. </goals>
  32. <configuration>
  33. <outputDirectory>${project.build.directory}/lib</outputDirectory>
  34. <overWriteReleases>true</overWriteReleases>
  35. <overWriteSnapshots>true</overWriteSnapshots>
  36. <overWriteIfNewer>true</overWriteIfNewer>
  37. </configuration>
  38. </execution>
  39. </executions>
  40. </plugin>
  41. </plugins>
  42. </build>
  43. </project>

Writing a HelloWorld Redis app

Create the appropriate directory structure as expected by maven.

  1. $ 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.

  1. package com.yugabyte.sample.apps;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import redis.clients.jedis.Jedis;
  5. public class YBRedisHelloWorld {
  6. public static void main(String[] args) {
  7. try {
  8. // Create a Jedis client.
  9. Jedis jedisClient = new Jedis("127.0.0.1");
  10. // Prepare the employee information to insert.
  11. String userid = "1";
  12. Map<String, String> userProfile = new HashMap<String, String>();
  13. userProfile.put("name", "John");
  14. userProfile.put("age", "35");
  15. userProfile.put("language", "Redis");
  16. // Insert the data.
  17. String result = jedisClient.hmset(userid, userProfile);
  18. System.out.println("HMSET returned " + result + ": id=1, name=John, age=35, language=Redis");
  19. // Query the data.
  20. Map<String, String> userData = jedisClient.hgetAll(userid);
  21. System.out.println("Query result: name=" + userData.get("name") +
  22. ", age=" + userData.get("age") + ", language=" + userData.get("language"));
  23. // Close the client.
  24. jedisClient.close();
  25. } catch (Exception e) {
  26. System.err.println("Error: " + e.getMessage());
  27. }
  28. }
  29. }

Building and running the app

To build the application, just run the following command.

  1. $ mvn package

To run the program, do the following.

  1. % java -cp "target/hello-world-1.0.jar:target/lib/*" com.yugabyte.sample.apps.YBRedisHelloWorld

You should see the following as the output.

  1. HMSET returned OK: id=1, name=John, age=35, language=Redis
  2. Query result: name=John, age=35, language=Redis