当前位置:   article > 正文

Redis中对ZSet类型的操作命令_zrangewithscores

zrangewithscores

1、zadd :添加元素,格式是:zadd zset的key score值 项的值, Score和项可以是多对,score可以是整数,也可以是浮点数,还可以是+inf表示无穷大,-inf表示负无穷大

操作如下:

java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. /**
  5. * redis中对于ZSet类型的: zadd 操作示例
  6. * zadd :添加元素,格式是:zadd zset的key score值 项的值, Score和项可以是多对,score可以是整数,也可以是浮点数,还可以是+inf表示无穷大,-inf表示负无穷大
  7. */
  8. public class ZSet_zadd_operation {
  9. public static void main(String[] args) {
  10. Jedis jedis = new Jedis("127.0.0.1",6379);
  11. /**
  12. * 示例1: zadd zset的key score值 项的值, Score和项可以是多对,score可以是整数,也可以是浮点数,还可以是+inf表示无穷大,-inf表示负无穷大
  13. */
  14. Long zadd = jedis.zadd("myzset", 10.0, "v1");
  15. System.out.println("zadd = " + zadd);
  16. Map<String,Double> map = new HashMap<>();
  17. map.put("v2",20.0);
  18. map.put("v3",30.0);
  19. Long myzset = jedis.zadd("myzset", map);
  20. System.out.println("myzset = " + myzset);
  21. }
  22. }

2、zrange :获取索引区间内的元素,格式是:zrange zset的key 起始索引 终止索引 (withscore)

操作如下:

 java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. import redis.clients.jedis.Tuple;
  3. import java.util.Set;
  4. /**
  5. * redis中对于ZSet类型的: zrange 操作示例
  6. * zrange :获取索引区间内的元素,格式是:zrange zset的key 起始索引 终止索引 (withscore)
  7. */
  8. public class ZSet_zrange_operation {
  9. public static void main(String[] args) {
  10. Jedis jedis = new Jedis("127.0.0.1",6379);
  11. /**
  12. * 示例1: zrange zset的key 起始索引 终止索引 (withscore)
  13. */
  14. Set<String> zrange = jedis.zrange("myzset", 0, -1);
  15. System.out.println("zrange = " + zrange);
  16. Set<Tuple> zrangeWithScores = jedis.zrangeWithScores("myzset", 0, -1);
  17. System.out.println("zrangeWithScores = " + zrangeWithScores);
  18. }
  19. }

3、zrangebyscore :获取分数区间内的元素,格式是:zrangebyscore zset的key 起始score 终止score (withscore),默认是包含端点值的,如果加上"("表示不包含,后面还可以加上limit来限制。

操作如下:

java示例代码:

  1. /**
  2. * redis中对于ZSet类型的: zrange 操作示例
  3. * zrangebyscore :获取分数区间内的元素,格式是:zrangebyscore zset的key 起始score 终止score (withscore),默认是包含端点值的,如果加上"("表示不包含,后面还可以加上limit来限制。
  4. */
  5. public class ZSet_zrangebyscore_operation {
  6. public static void main(String[] args) {
  7. Jedis jedis = new Jedis("127.0.0.1",6379);
  8. /**
  9. * 示例1: zrangebyscore zset的key 起始score 终止score (withscore),默认是包含端点值的,如果加上"("表示不包含,后面还可以加上limit来限制。
  10. */
  11. Set<String> zrangeByScore = jedis.zrangeByScore("myzset", 10.0, 25.0);
  12. System.out.println("zrangeByScore = " + zrangeByScore);
  13. Set<Tuple> zrangeByScoreWithScores = jedis.zrangeByScoreWithScores("myzset", 10.0, 25.0);
  14. System.out.println("zrangeByScoreWithScores = " + zrangeByScoreWithScores);
  15. }
  16. }

4、zrem :删除元素,格式是:zrem zset的key 项的值,项的值可以是多个

操作如下:

java示例代码:

  1. /**
  2. * redis中对于ZSet类型的: zrange 操作示例
  3. * zrem :删除元素,格式是:zrem zset的key 项的值,项的值可以是多个
  4. */
  5. public class ZSet_zrem_operation {
  6. public static void main(String[] args) {
  7. Jedis jedis = new Jedis("127.0.0.1",6379);
  8. /**
  9. * zrem zset的key 项的值,项的值可以是多个
  10. */
  11. Long zrem = jedis.zrem("myzset", "v1", "v2");
  12. System.out.println("zrem = " + zrem);
  13. }
  14. }

5、zcard :获取集合中元素个数,格式是:zcard zset的key

操作如下:

java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. /**
  3. * redis中对于ZSet类型的: zrange 操作示例
  4. * zcard :获取集合中元素个数,格式是:zcard zset的key
  5. */
  6. public class Zset_zcard_operation {
  7. public static void main(String[] args) {
  8. Jedis jedis = new Jedis("127.0.0.1",6379);
  9. /**
  10. * 示例1: zcard zset的key
  11. */
  12. Long zcard = jedis.zcard("myzset");
  13. System.out.println("zcard = " + zcard);
  14. }
  15. }

6、zincrby :增减元素的score,格式是:zincrby zset的key 正负数字 项的值

操作如下:

java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. /**
  3. * redis中对于ZSet类型的: zrange 操作示例
  4. * zincrby :增减元素的score,格式是:zincrby zset的key 正负数字 项的值
  5. */
  6. public class ZSet_zincrby_operation {
  7. public static void main(String[] args) {
  8. Jedis jedis = new Jedis("127.0.0.1",6379);
  9. /**
  10. * 示例1: zincrby zset的key 正负数字 项的值
  11. */
  12. Double zincrby = jedis.zincrby("myzset", 12, "v1");
  13. System.out.println("zincrby = " + zincrby);
  14. }
  15. }

7、zcount : 获取分数区间内元素个数,格式是:zcount zset的key 起始score 终止score

操作如下:

java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. /**
  3. * redis中对于ZSet类型的: zrange 操作示例
  4. * zcount : 获取分数区间内元素个数,格式是:zcount zset的key 起始score 终止score
  5. */
  6. public class ZSet_zcount_operation {
  7. public static void main(String[] args) {
  8. Jedis jedis = new Jedis("127.0.0.1",6379);
  9. /**
  10. * 示例1: zcount zset的key 起始score 终止score
  11. */
  12. Long zcount = jedis.zcount("myzset", 20.0, 50.0);
  13. System.out.println("zcount = " + zcount);
  14. }
  15. }

8、zrank : 获取项在zset中的索引,格式是:zrank zset的key 项的值

操作如下:

java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. /**
  3. * redis中对于ZSet类型的: zrange 操作示例
  4. * zrank : 获取项在zset中的索引,格式是:zrank zset的key 项的值
  5. */
  6. public class ZSet_zrank_operation {
  7. public static void main(String[] args) {
  8. Jedis jedis = new Jedis("127.0.0.1",6379);
  9. /**
  10. * 示例1:zrank zset的key 项的值
  11. */
  12. Long zrank = jedis.zrank("myzset", "v2");
  13. System.out.println("zrank = " + zrank);
  14. Long zrank2 = jedis.zrank("myzset", "v3");
  15. System.out.println("zrank2 = " + zrank2);
  16. }
  17. }

9、zscore :获取元素的分数,格式是:zscore zset的key 项的值,返回项在zset中的score

操作如下:

java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. /**
  3. * redis中对于ZSet类型的: zrange 操作示例
  4. * zscore :获取元素的分数,格式是:zscore zset的key 项的值,返回项在zset中的score
  5. */
  6. public class ZSet_zscore_operation {
  7. public static void main(String[] args) {
  8. Jedis jedis = new Jedis("127.0.0.1",6379);
  9. /**
  10. * 示例1:zscore zset的key 项的值,返回项在zset中的score
  11. */
  12. Double zscore = jedis.zscore("myzset", "v2");
  13. System.out.println("zscore = " + zscore);
  14. Double zscore2 = jedis.zscore("myzset", "v4");
  15. System.out.println("zscore2 = " + zscore2);
  16. Double zscore3 = jedis.zscore("myzset", "v10");
  17. System.out.println("zscore3 = " + zscore3);
  18. }
  19. }

10、zrevrank :获取项在zset中倒序的索引,格式是:zrevrank zset的key 项的值

操作如下:

java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. /**
  3. * redis中对于ZSet类型的: zrange 操作示例
  4. * zrevrank :获取项在zset中倒序的索引,格式是:zrevrank zset的key 项的值
  5. */
  6. public class ZSet_zrevrank_operation {
  7. public static void main(String[] args) {
  8. Jedis jedis = new Jedis("127.0.0.1",6379);
  9. /**
  10. * 示例1:zrevrank zset的key 项的值
  11. */
  12. Long zrevrank = jedis.zrevrank("myzset", "v2");
  13. System.out.println("zrevrank = " + zrevrank);
  14. Long zrevrank2 = jedis.zrevrank("myzset", "v1");
  15. System.out.println("zrevrank2 = " + zrevrank2);
  16. Long zrevrank3 = jedis.zrevrank("myzset", "v6");
  17. System.out.println("zrevrank3 = " + zrevrank3);
  18. }
  19. }

11、zrevrange :获取索引区间内的元素,格式是:zrevrange zset的key 起始索引 终止索引(withscores)

操作如下:

java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. import java.util.Set;
  3. /**
  4. * redis中对于ZSet类型的: zrange 操作示例
  5. * zrevrange :获取索引区间内的元素,格式是:zrevrange zset的key 起始索引 终止索引(withscores)
  6. */
  7. public class ZSet_zrevrange_operation {
  8. public static void main(String[] args) {
  9. Jedis jedis = new Jedis("127.0.0.1",6379);
  10. /**
  11. * 示例1:zrevrange zset的key 起始索引 终止索引(withscores)
  12. */
  13. Set<String> zrevrange = jedis.zrevrange("myzset", 0, -1);
  14. System.out.println("zrevrange = " + zrevrange);
  15. }
  16. }

12、zrevrangebyscore:获取分数区间内的元素,格式是:zrevrangebyscore zset的key 终止score 起始score(withscores)

操作如下:

java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. import redis.clients.jedis.Tuple;
  3. import java.util.Set;
  4. /**
  5. * redis中对于ZSet类型的: zrange 操作示例
  6. * zrevrangebyscore:获取分数区间内的元素,格式是:zrevrangebyscore zset的key 终止score 起始score(withscores)
  7. */
  8. public class ZSet_zrevrangebyscore_operation {
  9. public static void main(String[] args) {
  10. Jedis jedis = new Jedis("127.0.0.1",6379);
  11. /**
  12. * 示例1:zrevrangebyscore zset的key 终止score 起始score(withscores)
  13. */
  14. Set<String> zrevrangeByScore = jedis.zrevrangeByScore("myzset", 35.0, 15.0);
  15. System.out.println("zrevrangeByScore = " + zrevrangeByScore);
  16. Set<Tuple> zrevrangeByScoreWithScores = jedis.zrevrangeByScoreWithScores("myzset", 35.0, 15.0);
  17. System.out.println("zrevrangeByScoreWithScores = " + zrevrangeByScoreWithScores);
  18. }
  19. }

13、zremrangebyrank:删除索引区间内的元素,格式是:zremrangebyrank zset的key 起始索引 终止索引

操作如下:

java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. /**
  3. * redis中对于ZSet类型的: zremrangebyrank 操作示例
  4. * zremrangebyrank:删除索引区间内的元素,格式是:zremrangebyrank zset的key 起始索引 终止索引
  5. */
  6. public class ZSet_zremrangebyrank_operation {
  7. public static void main(String[] args) {
  8. Jedis jedis = new Jedis("127.0.0.1",6379);
  9. /**
  10. * 示例1:zremrangebyrank zset的key 起始索引 终止索引
  11. */
  12. Long zremrangeByRank = jedis.zremrangeByRank("myzset", 0, 2);
  13. System.out.println("zremrangeByRank = " + zremrangeByRank);
  14. }
  15. }

14、zremrangebyscore:删除分数区间内的元素,格式是:zremrangebysocre zset的key 起始score 终止score

操作如下:

java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. /**
  3. * redis中对于ZSet类型的: zremrangebyrank 操作示例
  4. * zremrangebyscore:删除分数区间内的元素,格式是:zremrangebysocre zset的key 起始score 终止score
  5. */
  6. public class ZSet_zremrangebyscore_operation {
  7. public static void main(String[] args) {
  8. Jedis jedis = new Jedis("127.0.0.1",6379);
  9. /**
  10. * 示例1:zremrangebysocre zset的key 起始score 终止score
  11. */
  12. Long zremrangeByScore = jedis.zremrangeByScore("myzset", 30, 40);
  13. System.out.println("zremrangeByScore = " + zremrangeByScore);
  14. }
  15. }

15、zinterstore:交集,格式是:zinterstore dest-key key-count key[key ...][WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]

操作如下:

原始数据:

java示例代码:

  1. import redis.clients.jedis.Jedis;
  2. import redis.clients.jedis.ZParams;
  3. /**
  4. * redis中对于ZSet类型的: zinterstore 操作示例
  5. * zinterstore:交集,格式是:zinterstore dest-key key-count key[key ...][WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
  6. */
  7. public class ZSet_zinterstore_operation {
  8. public static void main(String[] args) {
  9. Jedis jedis = new Jedis("127.0.0.1",6379);
  10. /**
  11. * 示例1:zinterstore dest-key key-count key[key ...][WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
  12. */
  13. Long zinterstore = jedis.zinterstore("myzset3", "myzset1", "myzset2");
  14. System.out.println("zinterstore = " + zinterstore);
  15. Long zinterstore1 = jedis.zinterstore("myzset4", new ZParams().aggregate(ZParams.Aggregate.MAX), "myzset1", "myzset2");
  16. System.out.println("zinterstore1 = " + zinterstore1);
  17. }
  18. }

16、zunionstore:并集,格式是:zunionstore dest-key key-count key[key ...][WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]

操作如下:

原始数据:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/649597
推荐阅读
相关标签
  

闽ICP备14008679号