Java implements Redis sorted set (sortedset) command

For configuration files, please see the last Java implementation of redis   https://blog.csdn.net/qq_41934990/article/details/81612180

Let's test the redis string type. The code in the comment is the redis command in Linux

package com.huadian.sortedset; 

import com.huadian.redisUntil.JedisPoolUntil; 
import org.junit.Assert; 
import org.junit.Before; 
import org.junit.Test; 
import redis.clients.jedis.Jedis; 
import redis.clients .jedis.ScanResult; 
import redis.clients.jedis.Tuple; 

import java.util.HashMap; 
import java.util.Map; 
import java.util.Set; 

public class SortedSet { 
    private Jedis jedis = null; 
    @Before 
    public void fun () { 
       jedis = JedisPoolUntil.getJedis (); 
    } 

    / ** 
     * Redis ordered collections are also collections of string-type elements like collections, and duplicate members are not allowed. 

     The difference is that each element is associated with a double type score. Redis uses the score to sort the members of the set from small to large.

     The members of the ordered set are unique, but the score (score) can be repeated 

     Redis Zrange returns to the ordered set, the members in the specified interval. 

     The position of the members is sorted by increasing numerical value (from small to large). 

     Members with the same score value are arranged in lexicographical order. 

     If you need 

     to arrange members in descending order (largest to smallest), use the ZREVRANGE command. 

     The subscript parameters start and stop are based on 0, that is, 0 means the first member of the ordered set, 1 means the second member of the ordered set, and so on. 

     You can also use negative subscripts, with -1 for the last member, -2 for the penultimate member, and so on. 
     * / 
    @Test 
    public void sortAdd () { 
       jedis.zadd ("mysort", 1.0, "Three Kingdoms"); 
       jedis.zadd ("mysort", 2.0, "Journey to the West"); 
       jedis.zadd ("mysort", 3.0, "Water Margin"); 
       jedis.zadd ("mysort", 4.0, "Red Mansion"); 
       Map <String, Double> map = new HashMap <> (); 
       Set <String> mysort = jedis.zrange ("mysort", 0, -1); 
       System.out.println (mysort); 
       // Reverse order 
       Set <String> mysort1 = jedis.zrange ("mysort",-1,0); 
        System.out.println (mysort1); 
    } 

    / ** 
     * The Redis Zcard command is used to calculate the number of elements in the collection. 
     * redis 127.0.0.1:6379> ZCARD KEY_NAME 
     * / 
    @Test 
    public void zcard () { 
        Long mysort = jedis.zcard ("mysort"); 
        System.out.println (mysort); 
    } 

    / ** 
     * Redis Zcount command Used to count the number of members in a specified score interval in an ordered set. 
     * redis 127.0.0.1:6379> 
    public void zcount () { 
        Long mysort = jedis.zcount ("mysort", 0, 4); 
        System.out.println (mysort); 
    } 

    / ** 
     * The Redis Zincrby command adds the score of the specified member in the ordered set Incremental increments 

     can be obtained by passing a negative value increment to the score minus the corresponding value, such as ZINCRBY key -5 member, which is to subtract 5 from the score value of the member. 

     When the key does not exist, or the score is not a member of the key, ZINCRBY key increment member is equivalent to ZADD key increment member. 

     When the key is not an ordered set type, an error is returned. 

     The fractional value can be an integer value or a double-precision floating-point number. 
     redis 127.0.0.1:6379> ZINCRBY key increment member 
     * / 
    @Test 
    public void zincrby () { 
        Double mysort = jedis.zincrby ("mysort", 10, "5"); 
        System.out.println (mysort);

     * The Redis Zinterstore command calculates the intersection of one or more ordered sets, where the number of given keys must be specified with the numkeys parameter and stores the intersection (result set) to the destination. 

     By default, the score value of a member in the result set is the sum of the score values ​​of the member in all the given sets. 

     redis 127.0.0.1:6379> ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM | MIN | MAX] 
     * / 
    @Test 
    public void zinterstore () { 
        Map <String, Double > map = new HashMap <String, Double> (); 
        for (int i = 0; i <10; i ++) { 
            map.put ("value:" + i, Double.valueOf (i)); // overwritten The above assignment 
        } 
        jedis.zadd ("keya", map); 
        for (int i = 0; i <10; i ++) { 
            map.put ("value:" + i, Double.valueOf (i + 3)); // Override the above assignment 
        } 
        map.put ("
        jedis.zadd ("keyb", map); 
        jedis.zinterstore ("destination", "keya", "keyb"); 
        Assert.assertTrue (jedis.zcard ("destination") == 10); 
        Assert.assertTrue (jedis .del ("keya", "keyb", "destination") == 3); 
    } 

    / ** 
     * The Redis Zlexcount command calculates the number of members in the specified dictionary interval in the ordered set. 
     * redis 127.0.0.1:6379> ZRANGE key start stop [WITHSCORES] 
     * / 
    @Test 
    public void zlexcount () { 
        Long mysort = jedis.zlexcount ("mysort", "-", "+"); 
        System.out.println (mysort); 
    } 

    / ** 
     * Redis Zrangebylex returns the members of an ordered set through a dictionary interval. 
     * redis 127. 
    public void zrangbylex () { 
        Set <String> mysort = jedis.zrangeByLex ("mysort", "-", "+"); 
        System.out.println (mysort); 
    } 

    / ** 
     * Redis Zrangebyscore returns order A list of members in the set with a specified score interval. The members of the ordered set are arranged in order of increasing numerical value (from small to large). 

     Members with the same score value are arranged in lexicographic order (this attribute is provided by an ordered set and no additional calculation is required). 

     By default, the value of the interval uses the closed interval (less than or equal to or greater than or equal to), you can also use the optional open interval (less than or greater than) by adding (sign to the parameter. 
     Redis 127.0.0.1:6379> ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] 
     * / 
    @Test 
    public void zrangebyscore () { 
        Set <String> mysort = jedis.zrangeByScore ("mysort", 1.0, 10.0); 
        System.out.println (mysort);
        Set <String> mysort1 = jedis.zrangeByScore ("mysort", 5, 10.0); 
    } 

    / ** 
    @Test 
    public void zrem () { 
        Long mysort = jedis.zrem ("
     * Redis Zrank returns the rank of the specified member in the ordered set. The ordered set members are arranged in order of increasing numerical value (from small to large). 
     * redis 127.0.0.1:6379> ZRANK key member 
     * / 
    @Test 
    public void zrank () { 
        Long mysort = jedis.zrank ("mysort", "Red Building Dream"); 
        System.out.println (mysort); 
    } 

    / ** 
     * The Redis Zrem command is used to remove one or more members in an ordered set. Members that do not exist will be ignored. 

     When the key exists but is not an ordered set type, an error is returned. 

     Note: Before Redis 2.4, ZREM can only delete one element at a time. 
     redis 127.0.0.1:6379> ZRANK key member 
     * / 
        System.out.println (mysort); 
    } 

    / ** 
     * The Redis Zremrangebylex command is used to remove all members of a given dictionary range in an ordered set. 
     * redis 127.0.0.1:6379> ZREMRANGEBYLEX key min max 
     * /
    @Test 
    public void zremrangebylex () { 
        Long mysort = jedis.zremrangeByLex ("mysort", "-", "+"); 
        System.out.println (mysort); 
    } 

    / ** 
     * Redis Zremrangebyrank command is used to remove In the sequence set, specify all members in the rank range. 
     * redis 127.0.0.1:6379> ZREMRANGEBYRANK key start stop 
     * / 
    @Test 
    public void zremrangbyrank () { 
        Long mysort = jedis.zremrangeByRank ("mysort", 5, 10); 
        System.out.println (mysort); 
    } 

    / * *
     * The Redis Zremrangebyscore command is used to remove all members in an ordered set within a specified score interval. 
     * redis 127.0.0.1:6379> ZREMRANGEBYSCORE key min max 
        Set <String> mysort = jedis.zrevrange ("mysort", 0, 5); 
     * / 
    @Test
    public void zrenrangebyscore () { 
        Long mysort = jedis.zremrangeByScore ("mysort", 5, 10); 
        System.out.println (mysort); 
    } 

    / ** 
     * The Redis Zrevrange command returns the members in the specified set. 

     The positions of the members are arranged in descending order (from large to small). 

     Members with the same score value are arranged in reverse lexicographical order. 

     The ZREVRANGE command is the same as the ZRANGE command except that the members are arranged in descending order of points. 

     redis 127.0.0.1:6379> ZREVRANGE key start stop [WITHSCORES] 
     * / 
    @Test 
    public void zrevrange () { 
     * Redis Zrevrangebyscore returns all within the specified score interval in the ordered set a member of. The members of the ordered set are arranged in descending order (from large to small).
        System.out.println (mysort); 
    } 

    / ** 

     Members with the same score value are arranged in reverse lexicographical order. 

     The ZREVRANGEBYSCORE command is otherwise the same as the ZRANGEBYSCORE command except that members are arranged in descending order of score values. 

     redis 127.0.0.1:6379> ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] 
     * / 
    @Test 
    public void zrevrangbyscore () { 
        Set <String> mysort = jedis.zrevrangeByScore ("mysort", 10, 0); 
        System. out.println (mysort); 
    } 
    / ** 
     * Redis Zrevrank command returns the rank of the members in the ordered set. The members of the ordered set are sorted in descending order (from large to small). 

     The ranking is based on 0, that is, the member with the highest score is ranked 0. 

     Use the ZRANK command to get the ranking of members in increasing order (from small to large). 

     redis 127.0.0.1:6379> ZREVRANK key member
     * / 
    @Test  
    public void zrevrank () { 
        Long zrevrank = jedis.zrevrank ("mysort", "West Journey");
        System.out.println (zrevrank); 
    } 

    / ** 
     * The Redis Zscore command returns the score value of the members in the ordered set. If the member element is not a member of the ordered set key, or the key does not exist, return nil. 
     * 
     * redis 127.0.0.1:6379> ZSCORE key member 
     * / 
    @Test 
    public void zscore () { 
        Double zscore = jedis.zscore ("mysort", "Red Mansion"); 
        System.out.println (zscore); 
    } 

    / * * 
     * The Redis Zunionstore command calculates the union of one or more ordered sets, where the number of keys must be specified with the numkeys parameter and stores the union (result set) in the destination. 

     By default, the score value of a member in the result set is the sum of the score values ​​of the member in all the given sets. 

     redis 127.0.0.1:6379> ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM | MIN | MAX]
     * / 
    @Test 
    public void zunionstore () { 
        Map <String, Double> map = new HashMap <String, Double> (); 
        for (int i = 0; i <10; i ++) {
            map.put ("value:" + i, Double.valueOf (i)); 
        } 
        jedis.zadd ("keya", map); 
        for (int i = 0; i <10; i ++) { 
            map.put (" value: "+ i, Double.valueOf (i + 3)); 
        } 
        map.put (" adsad ", 123d); 
        jedis.zadd (" keyb ", map); 
        jedis.zunionstore (" destination "," keya " , "keyb"); 
        Assert.assertTrue (jedis.zcard ("destination") == 11); 
        Assert.assertTrue (jedis.del ("keya", "keyb", "destination") == 3); 

    } 

    /**
     * The Redis Zscan command is used to iterate over the elements in an ordered collection (including element members and element scores) 
     * Each element returned is an ordered collection element, and an ordered collection element consists of a member and a score (Score) composition. 
     * redis 127.0. 
     * / 
    @Test 
    public void zscan () { 
        ScanResult <Tuple> mysort = jedis.zscan ("mysort", "2") ; 
        System.out.println (mysort); 
    } 
}
Published 105 original articles · Like 536 · Visits 70,000+

Guess you like

Origin blog.csdn.net/qq_41934990/article/details/81624353