Enterprise Java

Getting Started With Jedis

Hi, these days I started looking into Redis. I’ve heard a lot about it so I decided to have a try.

Redis is defined in its websites as “an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets“.

We can find very good examples on where Redis is a good fit on the Shades of Gray blog. In this post we will not focus on the features and capabilities of Redis, we will have a quick look at Jedis, a Java Redis Client. The Redis commands are very simple to learn, the Jedis api, it is also very simple to learn.

We can download the Jedis on its github repository (https://github.com/xetorthio/jedis). It is a simple jar that we can just add it to our application and start using it.

Below we can see a simple example of the Jedis api:

import redis.clients.jedis.Jedis;
public class TestJedis {

public static void main(String[] args) {
  //Connecting to Redis on localhost
  Jedis jedis = new Jedis("localhost");
  //adding a new key
  jedis.set("key", "value");
  //getting the key value
  System.out.println(jedis.get("key"));

 }

}

On the example above, we create a connection to the redis server, once we connect we add a new key on the datastore, using the method set, this method will call the SET command on Redis. With that we created a new key called “key” with a value “value”.Once we have a value for that key, we can get its value using the method get which calls the GET command on Redis.

A very useful command is the INCR that increments a key value. We can see an example of using this command below:

import redis.clients.jedis.Jedis;
public class IncrTest {

public static void main(String[] args) {
 Jedis jedis = new Jedis("localhost");
 System.out.println(jedis.get("counter"));
 jedis.incr("counter");
 System.out.println(jedis.get("counter"));
 }

}

One of the good use cases for Redis is caching. We can use it as caching system, it is very good for that because we can set a expiration time for a given key through the EXPIRE commad. Also we can get the TTL for the key using the TTL command. Below we can see an example with Jedis api.

import redis.clients.jedis.Jedis;
public class TestJedis {

public static void main(String[] args) throws InterruptedException {
 String cacheKey = "cachekey";
 Jedis jedis = new Jedis("localhost");
 //adding a new key
 jedis.set(cacheKey, "cached value");
 //setting the TTL in seconds
 jedis.expire(cacheKey, 15);
 //Getting the remaining ttl
 System.out.println("TTL:" + jedis.ttl(cacheKey));
 Thread.sleep(1000);
 System.out.println("TTL:" + jedis.ttl(cacheKey));
 //Getting the cache value
 System.out.println("Cached Value:" + jedis.get(cacheKey));

 //Wait for the TTL finishs
 Thread.sleep(15000);

 //trying to get the expired key
 System.out.println("Expired Key:" + jedis.get(cacheKey));
 }

}

Also redis can store some other values like Lists, Hashs, set and others. Below we can see an example of using Sets in Redis.

import redis.clients.jedis.Jedis;
public class TestJedis {

public static void main(String[] args) {
 String cacheKey = "languages";
 Jedis jedis = new Jedis("localhost");
 //Adding a set as value
 jedis.sadd(cacheKey,"Java","C#","Python");//SADD

 //Getting all values in the set: SMEMBERS
 System.out.println("Languages: " + jedis.smembers(cacheKey));
 //Adding new values
 jedis.sadd(cacheKey,"Java","Ruby");
 //Getting the values... it doesn't allow duplicates
 System.out.println("Languages: " + jedis.smembers(cacheKey));

 }

}

There are many other use cases for Redis, this post was intended to give an little introduction to the Jedis library. See you in the next post!

 

Reference: Getting Started With Jedis from our JCG partner Francisco Ribeiro Junior at the XICO JUNIOR’S WEBLOG blog.

Francisco Ribeiro Junior

Francisco is a senior software engineer working in the telecom/banking domain focused on Web Content Management. He has been technical lead on many projects for different Brazilian and worldwide clients.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
shubhada M
shubhada M
10 years ago

nice post to begin with jedis…Thanx:)

Ashif
Ashif
9 years ago
Reply to  shubhada M

The above code doesn’t work… even if adding the jedis java jar file

Ashif
Ashif
9 years ago

The import is not working

Francisco Ribeiro Junior
Reply to  Ashif

Hi Ashif,

How are you adding jedis jar into your project?

If you are using maven, you just need the dependency below on your pom.xml:

redis.clients
jedis
2.6.0
jar
compile

I hope this helps.

Skyler Tao
9 years ago

Good simple example. Then, where can we find more complicated and powerful examples using JEDIS ?

Sanket Mendon
Sanket Mendon
8 years ago

For those of your having problems related to resolving the jar dependencies, please try downloading the jar from the below link:

jedis-2.1.0.jar

http://www.java2s.com/Code/Jar/j/Downloadjedis210jar.htm

The one I previously downloaded was giving me some issues. This one works fine. And thanks for the help Francisco.

Som
Som
2 years ago

So here is the challenge, design a REDIS structure that resembles a table and Jedis code that does CRUD operations on that table!

Back to top button