How to Connect Redis on Java using Jedis

Introduction

This tutorial will demonstrate how to connect Redis to Java with the Jedis client library for Java using Maven in Eclipse. Redis is an open-source data store that is used as a cache and database with builtin replication that supports data structures such as bitmaps, indexes, lists, sets, sorted sets and strings. Java is a general-purpose, open-source programming language designed with minimal implementation dependencies. Jedis is a client library written in Java for Redis.

Prerequisites

  • Redis must be properly installed and configured.
  • Confirm the Redis installation with the redis-cli --version command. The results should resemble the following:
redis-cli 4.0.9
  • Java and Eclipse must be properly installed and configured.

  • JAVA

  • Eclipse IDE

Jedis Overview

Jedis is a client library written in Java for Redis. It is small and considerably faster that the other Java clients and has the largest community.

How to Create a New Maven Project in Eclipse

Create a Maven project as shown here:

A gif file showing on how to create a new Maven project in eclipse

  1. Open Eclipse IDE
  2. Click file then select “New” or it’s corresponding shortcut Alt+Shift+N,
  3. Click on “Project” and then select “Maven Project” and finally click the “Next” button to proceed to the next step.

A GIF file showing on how to create a new Maven project in Eclipse IDE

  1. In the “New Maven project” window, check the box “Create a simple project (skip archetype selection)” and then click the “Next” button to proceed.
  2. In the next window set the Group ID, Artifact ID and the SNAPSHOT version and then click “Finish” to create the Maven project.

The results should resemble the following:

Successfully created the maven project

How to Declare Jedis in the Maven Dependencies

Declare dependencies in Maven with the following xml code in the pom.xml of the Maven project with the following script”

<dependencies>

<dependency>
<groupid>redis.clients</groupid>
<artifactid>jedis</artifactid>
<version>2.9.0</version>
</dependency>
</dependencies>

Copy this piece of xml code in the maven online repository. Look for the latest version of Jedis and copy and paste it into the “pom.xml” and then click “Save.”

Successfully imported Jedis in Maven Dependencies

Eclipse has now successfully imported the needed Maven dependency as specified in the pom.xml file.

How to Connect Java to the Redis Server

Now that the Maven dependency is properly configured, connect the Java application to the Redis server.

How to Start the Redis Server

Start the Redis server by executing the command redis-server; the result should resemble the following:

Successfully started the Redis sever

How to Create a New Java Class

With the Redis server running, create the first Java Class and configure the class to connect to the Redis server by performing the following sequence of actions:

Creating a new Java Class in Eclipse

  1. Select “File”
  2. Select “New”
  3. Select “Class”4. Give the new class a name, in this case “MyApp”.
  4. Check the box “public static void main(String[]Args())”6 and then click the “Finish” button to create the class.

The Java Code

Open the newly created file for the Java program and add the following code:

try {
Jedis jedis = new Jedis("localhost");
// prints out "Connection Successful" if Java successfully connects to Redis server.
System.out.println("Connection Successful");
System.out.println("The server is running " + jedis.ping());
jedis.set("company-name", "500Rockets.io");
System.out.println("Stored string in redis:: "+ jedis.get("company-name"));



}catch(Exception e) {
System.out.println(e);
}

Here is a breakdown of the above code:

  1. Jedis jedis = new Jedis("localhost"); This connects our Java to Redis server running on our local host.

  2. System.out.println("The server is running " + jedis.ping()); this will return an output of “The server is running Pong”.

  3. jedis.set("company-name", "500Rockets.io"); This is trying to crate a record within Redis via key-value pair using the .set() method.

  4. System.out.println("Stored string in redis: "+ jedis.get("company-name")); This will obtain the data stored inside Redis by using the .get() method.

The result of the above code should resemble the following:

Connection Successful
The server is running PONG
Stored string in redis: 500Rockets.io

Conclusion

This tutorial explained how to properly set up a Maven project to obtain the needed dependencies to connect Redis to Java with Jedis client library for Java. The tutorial specifically explained how to start the Redis server, how to declare Jedis in the Maven dependencies, how to create a new Maven project in Eclipse and how to connect Java to the Redis server. Remember that Redis, Java and Eclipse must all be installed on the same machine in order to execute the the examples in this tutorial.

The Java Code.

MyApp.java

package mavendemo;

import redis.clients.jedis.Jedis;

public class MyApp {

public static void main(String[] args) throws Exception{
try {
Jedis jedis = new Jedis("localhost");
System.out.println("Connection Successful");
System.out.println("The server is running " + jedis.ping());
jedis.set("company-name", "500Rockets.io");
System.out.println("Stored string in redis: "+ jedis.get("company-name"));



}catch(Exception e) {
System.out.println(e);
}

}

}

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.