java command Redis Set Operation Example (Jedis)

package com.jjf.redis;
 
import org.junit.Assert;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.ScanResult;
 
import java.util.List;
import java.util.Set;
 
/**
 * Created by jjf_lenovo on 2017/3/31.
 */
public class RedisSetTest {
    Jedis jedis = null;
    static final String DATASOURCE_URL = "182.254.213.106";
    static final int DATASOURCE_SORT = 6379;
    static final String DATASOURCE_PASS = "123456";
    static final int DATASOURCE_SELECT = 1;
    public RedisSetTest(){
        //基本配置
        = new new Jedis jedis (DATASOURCE_URL, DATASOURCE_SORT);
        jedis.auth (DATASOURCE_PASS);
        jedis.select (DATASOURCE_SELECT);
    }
 
    @Test
    public void testSAdd () {
        // Sadd one or more elements to a set of key member which has exists in the collection of member elements are ignored.
        Assert.assertTrue (jedis.sadd ( "SET", "A") ==. 1);
        Assert.assertTrue (jedis.sadd ( "SET", "A", "B", "C") == 2);
        Assert.assertTrue (jedis.del ( "sET") ==. 1);
    }
 
    @Test
    public void testSCard () {
        // return SCard set base key (the number of elements in the set).
        Assert.assertTrue (jedis.sadd ( "SET", "A", "B", "C") ==. 3);
        the Assert.
        Assert.assertTrue (jedis.del ( "SET") ==. 1);
    }
 
    @Test
    public void testSDiff () {
        // The sdiff returns all members of a set, the set is the difference between the set of all the given set.
        Assert.assertTrue (jedis.sadd ( "SET", "A", "B", "C") ==. 3);
        Assert.assertTrue (jedis.sadd ( "the SET", "A", "B", " C "," a "," B "," C ") ==. 6);
        the Set <String> a = jedis.sdiff (" set "," the SET "); // this set is to identify, SET not element, multi-level, then, one by one than the
        Set <String> b = jedis.sdiff ( "SET", "set"); // is to identify the SET, set no element, multi-level, then, one by one than
        System. Out.println (a.toString () + ":::" + b.toString ());
        Assert.assertTrue (jedis.del ( "SET", "the SET") == 2);

 



        Assert.assertTrue(jedis.sadd("set","a","b","c")==3);
        Assert.assertTrue(jedis.sadd("SET","A","B","C","a","b","c")==6);
        Assert.assertTrue(jedis.sdiffstore("store","SET","set")==3);
        Assert.assertTrue(jedis.sdiffstore("store","set","SET")==0);
        Assert.assertTrue(jedis.del("set","SET")==2);
    }
 
    @Test
    public void testSInter(){
        //sinter 返回一个集合的全部成员,该集合是所有给定集合的交集。
        Assert.assertTrue(jedis.sadd("set","a","b","c")==3);
        Assert.assertTrue(jedis.sadd("SET","A","B","C","a","b","c")==6);
        Set<String> set = jedis.sinter("set","SET");
        System.out.println(set.toString());
        Assert.assertTrue(set.size()==3);
        Assert.assertTrue (jedis.del ( "SET", "the SET") == 2);
    }
 
    @Test
    public void testSInterStore () {
        // This command is similar to sinterstore SINTER command, but it will save the results to the set destination, and not simply return a result set.
        Assert.assertTrue (jedis.sadd ( "SET", "A", "B", "C") ==. 3);
        Assert.assertTrue (jedis.sadd ( "the SET", "A", "B", " C "," A "," B "," C ") ==. 6);
        Assert.assertTrue (jedis.sinterstore (" Store "," SET "," the SET ") ==. 3);
        Assert.assertTrue (jedis .del ( "set", "SET ", "store") == 3);
    }
 
    @Test
    public void testSIsMember () {
        // sismember member determines whether the set key element members.
        Assert.assertTrue (jedis.sadd ( "SET", "A", "B", "C") ==. 3);
        Assert.assertTrue (jedis.sismember ( "SET", "B"));
        Assert.assertTrue (jedis.del ( "SET") ==. 1);
    }
 
    @Test
    public void testSMembers () {
        // return all smembers of key members of the collection. The key does not exist to be seen as an empty set.
        Assert.assertTrue (jedis.sadd ( "SET", "A", "B", "C") ==. 3);
        the Set <String> = jedis.smembers SET ( "SET");
        System.out.println ( set.toString ());
        Assert.assertTrue (set.size () ==. 3);
        Assert.assertTrue (jedis.del ( "SET") ==. 1);
    }
 
    @Test
    public void testSMove () {
        // the member moves from the source element to the destination set collection. (Atomic)
        Assert.assertTrue (jedis.sadd ( "SET", "A", "B", "C") ==. 3);
        the Assert.
        Assert.assertTrue (jedis.smove ( "SET", "the SET", "A") ==. 1);
        the Set <String> A = jedis.smembers ( "SET");
        the Set <String> B = jedis.smembers ( "the SET");
        System.out.println (a.toString ());
        System.out.println (b.toString ());
        Assert.assertTrue (a.size () == 2);
        Assert.assertTrue (B .size () == 6); // already exists as a, so it is still 6
        Assert.assertTrue (jedis.del ( "SET", "the SET") == 2);
    }
 
    @Test
    public void testSPop () {
        // SPOP removes and returns a random element in the collection.
        Assert.assertTrue (jedis.sadd ( "SET", "A", "B", "C") ==. 3);
        System.out.println (jedis.spop ( "SET"));


        Assert.assertTrue (jedis.del ( "SET") ==. 1);
 
    }
 
    @Test
    public void testSRandMember () {
        / **
         * If the command is executed, only the key parameter, it returns a random element in the collection.
         Starting Redis 2.6 version, SRANDMEMBER count command accepts optional parameters:
         If count is positive, and smaller than the base set, then the command returns a count array contains the elements, elements of the array are different. If the count is greater than equal to the set base, then return the entire collection.
         If the count is negative, then the command returns an array of elements in the array may be repeated several times, and the length of the array is the absolute value of the count.
         * /
        Assert.assertTrue (jedis.sadd ( "SET", "A", "B", "C") ==. 3);
        System.out.println (jedis.srandmember ( "SET")); // different in spop, srandmember without removing
        the Set <String> A = jedis.smembers ( "SET");
        Assert.assertTrue (a.size () ==. 3);
 

        List <String> lista = jedis.srandmember ( "set", 2); // returns the two random numbers do not overlap
        System.out.println ( "Lista:" + lista.toString ());
        List <String> listB = jedis.srandmember ( "set", 5 ); // 5> 3 returns all data
        System.out.println ( "listB:" + listb.toString ());
        List <String> listc = jedis.srandmember ( "SET "-2); // returns | -2 | a repeat data
        System.out.println (" listc: "+ listc.toString ());
        List <String> listd = jedis.srandmember (" SET " -6); // returns | -6 | a repeat data
        System.out.println ( "listd:" + listd.toString ());
        Assert.assertTrue (jedis.del ( "SET") ==. 1) ;
    }
 
    @Test
    public void testSRem () {
        // set Srem remove a key member or a plurality of elements, the element member does not exist will be ignored.
        Assert.assertTrue(jedis.sadd("set","a","b","c")==3);
        Assert.assertTrue(jedis.srem("set","a","d")==1);
        Assert.assertTrue(jedis.del("set")==1);
    }
 
    @Test
    public void testSunion(){
        //sunion 返回一个集合的全部成员,该集合是所有给定集合的并集。
        Assert.assertTrue(jedis.sadd("set","a","b","c")==3);
        Assert.assertTrue(jedis.sadd("SET","A","B","C","a","b","c")==6);
        Set<String> set = jedis.sunion("set","SET");
        System.out.println(set.toString());
        Assert.assertTrue(set.size()==6);
        Assert.assertTrue(jedis.del("set","SET")==2);
    }
 
    @Test
    public void testSunionStore(){
        //sunion 返回一个集合的全部成员,该集合是所有给定集合的并集。
        Assert.assertTrue(jedis.sadd("set","a","b","c")==3);
        Assert.assertTrue(jedis.sadd("SET","A","B","C","a","b","c")==6);
        Assert.assertTrue(jedis.sunionstore("store","set","SET")==6);
        Assert.assertTrue(jedis.del("set","SET","store")==3);
    }
 
    @Test
    public void testSScan(){
        Pipeline pipeline = jedis.pipelined();
        for(int i=0;i<1000;i++){
            pipeline.sadd("set",String.valueOf(i));
        }
        pipeline.sync();
        ScanResult<String> result ;//=  jedis.sscan("set",0);
//        System.out.println(result.getResult().size()+"::"+result.getStringCursor());
//        System.out.println(result.getResult().toString());
        int count = 0;
        int cursor = 0;
        do{
//            System.out.println(result.getCursor());
            result = jedis.sscan("set",cursor);
            cursor = Integer.valueOf(result.getStringCursor());
            for (String ss : result.getResult()) {
                count++;
                System.out.print("<"+count+":" + ss + ">");
            }
            System.out.println();
        }
        while(cursor!=0);
        System.out.println(count);
        Assert.assertTrue(count==1000);
        Assert.assertTrue(jedis.del("set","SET","store")==1);
    }
}

Guess you like

Origin www.cnblogs.com/zhuyeshen/p/12147977.html