打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
通过 Jedis API 使用 Sorted Set 排序集合

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:


01import java.util.HashMap;
02import java.util.Map;
03 
04import redis.clients.jedis.Jedis;
05public class TestJedis {
06 
07public static void main(String[] args) {
08 String key = "mostUsedLanguages";
09 Jedis jedis = new Jedis("localhost");
10 //Adding a value with score to the set
11 jedis.zadd(key,100,"Java");//ZADD
12 
13 //We could add more than one value in one calling
14 Map<Double, String> scoreMembers = new HashMap<Double, String>();
15 scoreMembers.put(90d, "Python");
16 scoreMembers.put(80d, "Javascript");
17 jedis.zadd(key, scoreMembers);
18 
19 //We could get the score for a member
20 System.out.println("Number of Java users:" + jedis.zscore(key, "Java"));
21 
22 //We could get the number of elements on the set
23 System.out.println("Number of elements:" + jedis.zcard(key));//ZCARD
24 }
25}

In the example above we saw some Zset commands. To add elements to the zet we set the zaddmethod, 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. Thezaddcould be used for both add and update the score for an existing element.

We can get a score for a given element with thezscoreand the number of elements using the zcardcommand.

译者信息

Jedis 是 Redis 官方首选的 Java 客户端开发包。这篇文章我们将介绍如何使用 Sorted Set 排序集合(zsets)。

Sorted Set 跟一个集合一样,它是不会存在重复的数值,最大的不同是 Sorted Set 中每个元素都是经过排序的。

我们先看一些命令:

01import java.util.HashMap;
02import java.util.Map;
03 
04import redis.clients.jedis.Jedis;
05public class TestJedis {
06 
07public static void main(String[] args) {
08 String key = "mostUsedLanguages";
09 Jedis jedis = new Jedis("localhost");
10 //Adding a value with score to the set
11 jedis.zadd(key,100,"Java");//ZADD
12 
13 //We could add more than one value in one calling
14 Map<Double, String> scoreMembers = new HashMap<Double, String>();
15 scoreMembers.put(90d, "Python");
16 scoreMembers.put(80d, "Javascript");
17 jedis.zadd(key, scoreMembers);
18 
19 //We could get the score for a member
20 System.out.println("Number of Java users:" + jedis.zscore(key, "Java"));
21 
22 //We could get the number of elements on the set
23 System.out.println("Number of elements:" + jedis.zcard(key));//ZCARD
24 }
25}


上述例子中我们看到了 Zset 命令,为了将元素添加到 zet 中,我们使用 zadd 方法,不同的是我们还传递了一个元素的评分值,我们可以使用 Map 对象来一次传递很多个对象,而 zadd 方法可用于增加和更新已有元素的评分值。

我们可使用 zscore 来获取某元素的评分,通过 zcard 获取元素个数。

Below we can see other commands from zsets:

01import java.util.Set;
02 
03import redis.clients.jedis.Jedis;
04import redis.clients.jedis.Tuple;
05public class TestJedis {
06 
07public static void main(String[] args) {
08 String key = "mostUsedLanguages";
09 Jedis jedis = new Jedis("localhost");
10 
11 //get all the elements sorted from bottom to top
12 System.out.println(jedis.zrange(key, 0, -1));
13 
14 //get all the elements sorted from top to bottom
15 System.out.println(jedis.zrevrange(key, 0, -1));
16 //We could get the elements with the associated score
17 Set<Tuple> elements = jedis.zrevrangeWithScores(key, 0, -1);
18 for(Tuple tuple: elements){
19 System.out.println(tuple.getElement() + "-" + tuple.getScore());
20 }
21 
22 //We can increment a score for a element using ZINCRBY
23 System.out.println("Score before zincrby:" + jedis.zscore(key, "Python"));
24 //Incrementing the element score
25 jedis.zincrby(key, 1, "Python");
26 System.out.println("Score after zincrby:" + jedis.zscore(key, "Python"));
27 }
28}

With thezrangewe 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 methodzrevrrange. 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 methodzrevrangeWithScoresthat returns a Set of Tuple objects. Other useful command is  zincrbythat 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.


译者信息

下面的例子我们可看到来自 zsets 的其他命令:

01import java.util.Set;
02 
03import redis.clients.jedis.Jedis;
04import redis.clients.jedis.Tuple;
05public class TestJedis {
06 
07public static void main(String[] args) {
08 String key = "mostUsedLanguages";
09 Jedis jedis = new Jedis("localhost");
10 
11 //get all the elements sorted from bottom to top
12 System.out.println(jedis.zrange(key, 0, -1));
13 
14 //get all the elements sorted from top to bottom
15 System.out.println(jedis.zrevrange(key, 0, -1));
16 //We could get the elements with the associated score
17 Set<Tuple> elements = jedis.zrevrangeWithScores(key, 0, -1);
18 for(Tuple tuple: elements){
19 System.out.println(tuple.getElement() + "-" + tuple.getScore());
20 }
21 
22 //We can increment a score for a element using ZINCRBY
23 System.out.println("Score before zincrby:" + jedis.zscore(key, "Python"));
24 //Incrementing the element score
25 jedis.zincrby(key, 1, "Python");
26 System.out.println("Score after zincrby:" + jedis.zscore(key, "Python"));
27 }
28}


通过zrange我们能获取给定范围的元素,它将返回经过排序后的元素列表(自底向上),也可以通过zrevrrange获取自顶向下的元素列表。Redis 还允许我们通过关联的评分来获取元素,传递 “withscores“ 参数即可。使用 Jedis API 的zrevrangeWithScores方法可返回对象集合。另外一个有用的命令是zincrby可用于增加元素的评分值。

zsets 还有其他的命令,这里我们只是介绍跟 Jedis API 相关的一些基本用法。这里还有一些关于排序集合的介绍。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java对redis的基本操作
Redis随笔(五)Jedis、jedisCluster的使用
pom redis(Jedis)
Windows下Redis的安装使用
Java中使用Jedis连接Redis对String进行操作的常用命令
Redis实现文章投票功能
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服