Build an application using Java


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


Working example


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 Quick start.
  • JDK version 1.8 or later
  • Maven 3.3 or later

Create 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. -->




Write a HelloWorld Java application

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/

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("");

      // Prepare the employee information to insert.
      String userid = "1";
      Map<String, String> userProfile = new HashMap<String, String>();
      userProfile.put("name", "John");
      userProfile.put("age", "35");
      userProfile.put("language", "Redis");

      // Insert the data.
      String result = jedisClient.hmset(userid, userProfile);
      System.out.println("HMSET returned " + result + ": id=1, name=John, age=35, language=Redis");

      // Query the data.
      Map<String, String> userData = jedisClient.hgetAll(userid);
      System.out.println("Query result: name=" + userData.get("name") +
                         ", age=" + userData.get("age") + ", language=" + userData.get("language"));

      // Close the client.
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());

Build and run the application

To build the application, run the following command.

$ mvn package

To start the application, run the following command.

% 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