Using Sorted Sets with Jedis API

jieforest發表於2012-07-15
Sorted Sets and Jedis

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:

CODE:

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 scoreMembers = new HashMap();
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
}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-735499/,如需轉載,請註明出處,否則將追究法律責任。

相關文章