Enterprise Java

Using Sorted Sets with Jedis API

In the previous post we started looking into Jedis API a Java Redis Client. In this post we will look into the Sorted Set(zsets).

Sorted Set works like a Set in the way it doesn’t allow duplicated values. The big difference is that in Sorted Set each element has a score in order to keep the elements sorted.

We can see some  commands below:
 
 
 

import java.util.HashMap;
import java.util.Map;

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

public static void main(String[] args) {
 String key = "mostUsedLanguages";
 Jedis jedis = new Jedis("localhost");
 //Adding a value with score to the set
 jedis.zadd(key,100,"Java");//ZADD

 //We could add more than one value in one calling
 Map<Double, String> scoreMembers = new HashMap<Double, String>();
 scoreMembers.put(90d, "Python");
 scoreMembers.put(80d, "Javascript");
 jedis.zadd(key, scoreMembers);

 //We could get the score for a member
 System.out.println("Number of Java users:" + jedis.zscore(key, "Java"));

 //We could get the number of elements on the set
 System.out.println("Number of elements:" + jedis.zcard(key));//ZCARD
 }
}

In the example above we saw some Zset commands. To add elements to the zet we set the zadd method, the difference for the sets is that we pass also the score for the element. There is a overloaded version that we can pass many values using a map. The zadd could be used for both add and update the score for an existing element.

We can get a score for a given element with the zscore and the number of elements using the zcard command.

Below we can see other commands from zsets:

import java.util.Set;

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

public static void main(String[] args) {
 String key = "mostUsedLanguages";
 Jedis jedis = new Jedis("localhost");

 //get all the elements sorted from bottom to top
 System.out.println(jedis.zrange(key, 0, -1));

 //get all the elements sorted from top to bottom
 System.out.println(jedis.zrevrange(key, 0, -1));
 //We could get the elements with the associated score
 Set<Tuple> elements = jedis.zrevrangeWithScores(key, 0, -1);
 for(Tuple tuple: elements){
 System.out.println(tuple.getElement() + "-" + tuple.getScore());
 }

 //We can increment a score for a element using ZINCRBY
 System.out.println("Score before zincrby:" + jedis.zscore(key, "Python"));
 //Incrementing the element score
 jedis.zincrby(key, 1, "Python");
 System.out.println("Score after zincrby:" + jedis.zscore(key, "Python"));
 }
}

With the zrange we can get the elements for the given range. It returns the elements sorted from bottom to top. We can get the elements from top to bottom using the method zrevrrange. Redis also allow us to get the elements with the associated scores. In redis we pass the option “withscores“. With Jedis API we use the method zrevrangeWithScores that returns a Set of Tuple objects. Other useful command is  zincrby that we can increment the score for a member in the set.

There are other commands for zsets, this post was intended only to show some basical usage with Jedis API. We can find a good use case for sorted sets in this post.

See you in the next post.
 

Reference: Using Sorted Sets with Jedis API 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.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button