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

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.
Related Whitepaper:

Professional NoSQL

A hands-on guide to leveraging NoSQL databases!

NoSQL databases are an efficient and powerful tool for storing and manipulating vast quantities of data. Most NoSQL databases scale well as data grows. In addition, they are often malleable and flexible enough to accommodate semi-structured and sparse data sets. This comprehensive hands-on guide presents fundamental concepts and practical solutions for getting you ready to use NoSQL databases. Expert author Shashank Tiwari begins with a helpful introduction on the subject of NoSQL, explains its characteristics and typical uses, and looks at where it fits in the application stack. Unique insights help you choose which NoSQL solutions are best for solving your specific data storage needs.

Get it Now!  

Leave a Reply


× 8 = fifty six



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books