七、Redis中對ZSet類型的操做命令

寫在前面的話:讀書破萬卷,編碼若有神
--------------------------------------------------------------------
 
--------------------------------------------------------------------
一、zadd :添加元素,格式是:zadd zset的key score值 項的值, Score和項能夠是多對,score能夠是整數,也能夠是浮點數,還能夠是+inf表示無窮大,-inf表示負無窮大
操做以下:
java示例代碼:
 1 import redis.clients.jedis.Jedis;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 /**
 7  * redis中對於ZSet類型的: zadd 操做示例
 8  * zadd :添加元素,格式是:zadd zset的key score值 項的值, Score和項能夠是多對,score能夠是整數,也能夠是浮點數,還能夠是+inf表示無窮大,-inf表示負無窮大
 9  */
10 public class ZSet_zadd_operation {
11     public static void main(String[] args) {
12         Jedis jedis = new Jedis("127.0.0.1",6379);
13 
14         /**
15          * 示例1: zadd zset的key score值 項的值, Score和項能夠是多對,score能夠是整數,也能夠是浮點數,還能夠是+inf表示無窮大,-inf表示負無窮大
16          */
17         Long zadd = jedis.zadd("myzset", 10.0, "v1");
18         System.out.println("zadd = " + zadd);
19         
20 
21         Map<String,Double> map = new HashMap<>();
22         map.put("v2",20.0);
23         map.put("v3",30.0);
24         Long myzset = jedis.zadd("myzset", map);
25         System.out.println("myzset = " + myzset);
26     }
27 }
二、zrange :獲取索引區間內的元素,格式是:zrange zset的key 起始索引 終止索引 (withscore)
操做以下:
java示例代碼:
 1 import redis.clients.jedis.Jedis;
 2 import redis.clients.jedis.Tuple;
 3 
 4 import java.util.Set;
 5 
 6 /**
 7  * redis中對於ZSet類型的: zrange 操做示例
 8  * zrange :獲取索引區間內的元素,格式是:zrange zset的key 起始索引 終止索引 (withscore)
 9  */
10 public class ZSet_zrange_operation {
11     public static void main(String[] args) {
12         Jedis jedis = new Jedis("127.0.0.1",6379);
13 
14         /**
15          * 示例1: zrange zset的key 起始索引 終止索引 (withscore)
16          */
17         Set<String> zrange = jedis.zrange("myzset", 0, -1);
18         System.out.println("zrange = " + zrange);
19 
20         Set<Tuple> zrangeWithScores = jedis.zrangeWithScores("myzset", 0, -1);
21         System.out.println("zrangeWithScores = " + zrangeWithScores);
22     }
23
三、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 
 7     public static void main(String[] args) {
 8         Jedis jedis = new Jedis("127.0.0.1",6379);
 9 
10         /**
11          * 示例1: zrangebyscore zset的key 起始score 終止score (withscore),默認是包含端點值的,若是加上"("表示不包含,後面還能夠加上limit來限制。
12          */
13         Set<String> zrangeByScore = jedis.zrangeByScore("myzset", 10.0, 25.0);
14         System.out.println("zrangeByScore = " + zrangeByScore);
15 
16         Set<Tuple> zrangeByScoreWithScores = jedis.zrangeByScoreWithScores("myzset", 10.0, 25.0);
17         System.out.println("zrangeByScoreWithScores = " + zrangeByScoreWithScores);
18 
19     }
20 }
四、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         /**
10          * zrem zset的key 項的值,項的值能夠是多個
11          */
12         Long zrem = jedis.zrem("myzset", "v1", "v2");
13         System.out.println("zrem = " + zrem);
14     }
15 }
五、zcard :獲取集合中元素個數,格式是:zcard zset的key
操做以下:
java示例代碼:
 1 import redis.clients.jedis.Jedis;
 2 
 3 /**
 4  * redis中對於ZSet類型的: zrange 操做示例
 5  * zcard :獲取集合中元素個數,格式是:zcard zset的key
 6  */
 7 public class Zset_zcard_operation {
 8     public static void main(String[] args) {
 9         Jedis jedis = new Jedis("127.0.0.1",6379);
10 
11         /**
12          * 示例1: zcard zset的key
13          */
14         Long zcard = jedis.zcard("myzset");
15         System.out.println("zcard = " + zcard);
16     }
17 }
六、zincrby :增減元素的score,格式是:zincrby zset的key 正負數字 項的值
操做以下:
java示例代碼:
 1 import redis.clients.jedis.Jedis;
 2 
 3 /**
 4  * redis中對於ZSet類型的: zrange 操做示例
 5  * zincrby :增減元素的score,格式是:zincrby zset的key 正負數字 項的值
 6  */
 7 public class ZSet_zincrby_operation {
 8     public static void main(String[] args) {
 9         Jedis jedis = new Jedis("127.0.0.1",6379);
10 
11         /**
12          * 示例1: zincrby zset的key 正負數字 項的值
13          */
14         Double zincrby = jedis.zincrby("myzset", 12, "v1");
15         System.out.println("zincrby = " + zincrby);
16     }
17 }
七、zcount : 獲取分數區間內元素個數,格式是:zcount zset的key 起始score 終止score
操做以下:
java示例代碼:
 1 import redis.clients.jedis.Jedis;
 2 
 3 /**
 4  * redis中對於ZSet類型的: zrange 操做示例
 5  * zcount : 獲取分數區間內元素個數,格式是:zcount zset的key 起始score 終止score
 6  */
 7 public class ZSet_zcount_operation {
 8     public static void main(String[] args) {
 9         Jedis jedis = new Jedis("127.0.0.1",6379);
10 
11         /**
12          * 示例1: zcount zset的key 起始score 終止score
13          */
14         Long zcount = jedis.zcount("myzset", 20.0, 50.0);
15         System.out.println("zcount = " + zcount);
16     }
17 }
八、zrank : 獲取項在zset中的索引,格式是:zrank zset的key 項的值
操做以下:
java示例代碼:
 1 import redis.clients.jedis.Jedis;
 2 
 3 /**
 4  * redis中對於ZSet類型的: zrange 操做示例
 5  * zrank : 獲取項在zset中的索引,格式是:zrank zset的key 項的值
 6  */
 7 public class ZSet_zrank_operation {
 8     public static void main(String[] args) {
 9         Jedis jedis = new Jedis("127.0.0.1",6379);
10 
11         /**
12          * 示例1:zrank zset的key 項的值
13          */
14         Long zrank = jedis.zrank("myzset", "v2");
15         System.out.println("zrank = " + zrank);
16 
17         Long zrank2 = jedis.zrank("myzset", "v3");
18         System.out.println("zrank2 = " + zrank2);
19     }
20 }
九、zscore :獲取元素的分數,格式是:zscore zset的key 項的值,返回項在zset中的score
操做以下:
java示例代碼:
 1 import redis.clients.jedis.Jedis;
 2 
 3 /**
 4  * redis中對於ZSet類型的: zrange 操做示例
 5  * zscore :獲取元素的分數,格式是:zscore zset的key 項的值,返回項在zset中的score
 6  */
 7 public class ZSet_zscore_operation {
 8     public static void main(String[] args) {
 9         Jedis jedis = new Jedis("127.0.0.1",6379);
10 
11         /**
12          * 示例1:zscore zset的key 項的值,返回項在zset中的score
13          */
14         Double zscore = jedis.zscore("myzset", "v2");
15         System.out.println("zscore = " + zscore);
16 
17         Double zscore2 = jedis.zscore("myzset", "v4");
18         System.out.println("zscore2 = " + zscore2);
19 
20         Double zscore3 = jedis.zscore("myzset", "v10");
21         System.out.println("zscore3 = " + zscore3);
22     }
23 }
十、zrevrank :獲取項在zset中倒序的索引,格式是:zrevrank zset的key 項的值
操做以下:
java示例代碼:
 1 import redis.clients.jedis.Jedis;
 2 
 3 /**
 4  * redis中對於ZSet類型的: zrange 操做示例
 5  * zrevrank :獲取項在zset中倒序的索引,格式是:zrevrank zset的key 項的值
 6  */
 7 public class ZSet_zrevrank_operation {
 8     public static void main(String[] args) {
 9         Jedis jedis = new Jedis("127.0.0.1",6379);
10 
11         /**
12          * 示例1:zrevrank zset的key 項的值
13          */
14         Long zrevrank = jedis.zrevrank("myzset", "v2");
15         System.out.println("zrevrank = " + zrevrank);
16 
17         Long zrevrank2 = jedis.zrevrank("myzset", "v1");
18         System.out.println("zrevrank2 = " + zrevrank2);
19 
20         Long zrevrank3 = jedis.zrevrank("myzset", "v6");
21         System.out.println("zrevrank3 = " + zrevrank3);
22     }
23 }
十一、zrevrange :獲取索引區間內的元素,格式是:zrevrange zset的key 起始索引 終止索引(withscores)
操做以下:
java示例代碼:
 1 import redis.clients.jedis.Jedis;
 2 
 3 import java.util.Set;
 4 
 5 /**
 6  * redis中對於ZSet類型的: zrange 操做示例
 7  * zrevrange :獲取索引區間內的元素,格式是:zrevrange zset的key 起始索引 終止索引(withscores)
 8  */
 9 public class ZSet_zrevrange_operation {
10     public static void main(String[] args) {
11         Jedis jedis = new Jedis("127.0.0.1",6379);
12 
13         /**
14          * 示例1:zrevrange zset的key 起始索引 終止索引(withscores)
15          */
16         Set<String> zrevrange = jedis.zrevrange("myzset", 0, -1);
17         System.out.println("zrevrange = " + zrevrange);
18     }
19 }
十二、zrevrangebyscore:獲取分數區間內的元素,格式是:zrevrangebyscore zset的key 終止score 起始score(withscores)
操做以下:
java示例代碼:
 1 import redis.clients.jedis.Jedis;
 2 import redis.clients.jedis.Tuple;
 3 
 4 import java.util.Set;
 5 
 6 /**
 7  * redis中對於ZSet類型的: zrange 操做示例
 8  * zrevrangebyscore:獲取分數區間內的元素,格式是:zrevrangebyscore zset的key 終止score 起始score(withscores)
 9  */
10 public class ZSet_zrevrangebyscore_operation {
11     public static void main(String[] args) {
12         Jedis jedis = new Jedis("127.0.0.1",6379);
13 
14         /**
15          * 示例1:zrevrangebyscore zset的key 終止score 起始score(withscores)
16          */
17         Set<String> zrevrangeByScore = jedis.zrevrangeByScore("myzset", 35.0, 15.0);
18         System.out.println("zrevrangeByScore = " + zrevrangeByScore);
19 
20         Set<Tuple> zrevrangeByScoreWithScores = jedis.zrevrangeByScoreWithScores("myzset", 35.0, 15.0);
21         System.out.println("zrevrangeByScoreWithScores = " + zrevrangeByScoreWithScores);
22     }
23 }
1三、zremrangebyrank:刪除索引區間內的元素,格式是:zremrangebyrank zset的key 起始索引 終止索引
操做以下:
java示例代碼:
 1 import redis.clients.jedis.Jedis;
 2 
 3 /**
 4  * redis中對於ZSet類型的: zremrangebyrank 操做示例
 5  * zremrangebyrank:刪除索引區間內的元素,格式是:zremrangebyrank zset的key 起始索引 終止索引
 6  */
 7 public class ZSet_zremrangebyrank_operation {
 8     public static void main(String[] args) {
 9         Jedis jedis = new Jedis("127.0.0.1",6379);
10 
11         /**
12          * 示例1:zremrangebyrank zset的key 起始索引 終止索引
13          */
14         Long zremrangeByRank = jedis.zremrangeByRank("myzset", 0, 2);
15         System.out.println("zremrangeByRank = " + zremrangeByRank);
16     }
17 }
1四、zremrangebyscore:刪除分數區間內的元素,格式是:zremrangebysocre zset的key 起始score 終止score
操做以下:
java示例代碼:
 1 import redis.clients.jedis.Jedis;
 2 
 3 /**
 4  * redis中對於ZSet類型的: zremrangebyrank 操做示例
 5  * zremrangebyscore:刪除分數區間內的元素,格式是:zremrangebysocre zset的key 起始score 終止score
 6  */
 7 public class ZSet_zremrangebyscore_operation {
 8     public static void main(String[] args) {
 9         Jedis jedis = new Jedis("127.0.0.1",6379);
10 
11         /**
12          * 示例1:zremrangebysocre zset的key 起始score 終止score
13          */
14         Long zremrangeByScore = jedis.zremrangeByScore("myzset", 30, 40);
15         System.out.println("zremrangeByScore = " + zremrangeByScore);
16     }
17 }
1五、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 /**
 5  * redis中對於ZSet類型的: zinterstore 操做示例
 6  * zinterstore:交集,格式是:zinterstore dest-key key-count key[key ...][WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
 7  */
 8 public class ZSet_zinterstore_operation {
 9     public static void main(String[] args) {
10         Jedis jedis = new Jedis("127.0.0.1",6379);
11 
12         /**
13          * 示例1:zinterstore dest-key key-count key[key ...][WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
14          */
15         Long zinterstore = jedis.zinterstore("myzset3", "myzset1", "myzset2");
16         System.out.println("zinterstore = " + zinterstore);
17 
18         Long zinterstore1 = jedis.zinterstore("myzset4", new ZParams().aggregate(ZParams.Aggregate.MAX), "myzset1", "myzset2");
19         System.out.println("zinterstore1 = " + zinterstore1);
20     }
21 }

 

1六、zunionstore:並集,格式是:zunionstore dest-key key-count key[key ...][WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
操做以下:
原始數據:

相關文章
相關標籤/搜索