Jedis

Use Java to manipulate Redis

If you want to use Java to operate Redis, then you should be familiar with Jedis

test

  • Import the corresponding dependencies
    <! Select * from 'jedisd';
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>3.2.0</version>
    </dependency>

    <! -- fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.62</version>
    </dependency>
Copy the code
  • Coding test
    • Connecting to a Database
    • Operation command
    • disconnect

PING

    @Test
    void TestPing(a) {
        //1 new Jedis object
        Jedis jedis = new Jedis("127.0.0.1".6379);
        // All jedis commands are his basic commands, which are methods on objects
        System.out.println(jedis.ping());;
    }
Copy the code

Output PONG

Commonly used API

  • String
  • List
  • Set
  • Hash
  • Zset

Redis-key

    @Test
    void TestKey(a) {
        Jedis jedis = new Jedis("127.0.0.1".6379);
        System.out.println("Clear data" + jedis.flushDB());
        System.out.println("Determine whether a key exists:" + jedis.exists("username"));
        System.out.println(Add key pair '
      
        '
      ,> + jedis.set("username"."mxz"));
        System.out.println("Add key-value pair '
      
        '"
      ,> + jedis.set("password"."mxz"));
        System.out.println("All key-value pairs in the system are as follows:");
        Set<String> keys = jedis.keys("*");
        System.out.println(keys);

        System.out.println("Delete password:" + jedis.del("password"));
        System.out.println("Check whether key password exists:" + jedis.exists("password"));
        System.out.println("Check the type of value stored by the key username:" + jedis.type("username"));
        System.out.println("Randomly return one of the key Spaces:" + jedis.randomKey());
        System.out.println("Rename key:" + jedis.rename("username"."name"));
        System.out.println("Query by index:" + jedis.select(0));
        System.out.println(Delete all keys from the currently selected database: + jedis.flushDB());
        System.out.println("Return the number of keys in the current database: + jedis.dbSize());
        System.out.println(Delete all keys from all databases: + jedis.flushAll());
    }
Copy the code

The output

____ ____ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | | | (_ | |))))' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.1) 19:28:31 2021-01-08. 7492-602 the INFO [main] cn.com.codincge.RedisApplicationTests: Starting RedisApplicationTests using Java 1.8.0_181 on DESKTOP-GMI8GKQ with PID 7492 (Started by MXZ in D:\mxz_code\codingce-java\codingce-redis\redis-01-jedis) 2021-01-08 19:28:31.608 INFO 7492 - [main] cn.com.codincge.RedisApplicationTests : No active profile set, falling back to default profiles: Default 19:28:32 2021-01-08. 7492-290 the INFO [main] cn.com.codincge.RedisApplicationTests: Started RedisApplicationTests in 1.133 seconds (JVM running for 3.804)<username, MXZ > key-value pair'new' OK<password, MXZ > key-value pair[password, username] Delete key password: 1 Check whether key password exists: false Check the type of value stored by key username: string Randomly return one of the key space: Username Rename key: OK Query by index: OK Delete all keys in the currently selected database: OK Return the number of keys in the current database: 0 Delete all keys in all databases: OKCopy the code

String(String)

    @Test
    void TestString(a) {
        Jedis jedis = new Jedis("127.0.0.1".6379);
        System.out.println("===== add data =====");
        System.out.println(jedis.set("key1"."value1"));
        System.out.println(jedis.set("key2"."value2"));
        System.out.println(jedis.set("key3"."value3"));
        System.out.println(Delete key key2 + jedis.del("key2"));
        System.out.println("Get key key2" + jedis.get("key2"));
        System.out.println("Modify key1" + jedis.set("key1"."valueChanged"));
        System.out.println("Get the value of key1" + jedis.get("key1"));
        System.out.println("Add the value after key3" + jedis.append("key3"."end"));
        System.out.println(The value of "key3" + jedis.get("key3"));
        System.out.println("Add multiple key-value pairs:" + jedis.mset("key01"."value01"."key02"."value02"));
        System.out.println("Get multiple key-value pairs:" + jedis.mget("key01"."key02"."key03"));
        System.out.println("Get multiple key-value pairs:" + jedis.mget("key01"."key02"."key03"."key04"));
        System.out.println("Delete multiple key pairs:" + jedis.del("key01"."key02"));
        System.out.println("Get multiple key-value pairs:" + jedis.mget("key01"."key02"."key03"));

        jedis.flushDB();
        System.out.println("===== Add key-value pair to prevent overwriting old value =====");
        System.out.println(jedis.setnx("key1"."value1"));
        System.out.println(jedis.setnx("key2"."value2"));
        System.out.println(jedis.setnx("key2"."value2-new"));
        System.out.println(jedis.get("key1"));
        System.out.println(jedis.get("key2"));

        System.out.println("===== Add key-value pair and set valid time =====");
        System.out.println(jedis.setex("key3".2."value3"));
        System.out.println(jedis.get("key3"));
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(jedis.get("key3"));


        System.out.println("===== get original value, update to new value =====");
        System.out.println(jedis.getSet("key2"."key2GetSet"));
        System.out.println(jedis.get("key2"));

        System.out.println("Get the value of key2 string:" + jedis.getrange("key2".2.4));
    }
Copy the code

The output

____ ____ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | | | (_ | |))))' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.1) 09:56:23 2021-01-09. 9088-763 the INFO [main] cn.com.codincge.RedisApplicationTests: Starting RedisApplicationTests using Java 1.8.0_181 on DESKTOP-GMI8GKQ with PID 9088 (Started by MXZ in D:\mxz_code\codingce-java\codingce-redis\redis-01-jedis) 2021-01-09 09:56:23.825 INFO 9088 - [main] cn.com.codincge.RedisApplicationTests : No active profile set, falling back to default profiles: Default 09:56:25 2021-01-09. 9088-462 the INFO [main] cn.com.codincge.RedisApplicationTests: Started RedisApplicationTests in 2.349 seconds (JVM running for 5.252) ===== Add data ===== OK OK OK Delete key21 obtain key2NULL ValueChanged add 9 to key3 value3end Add multiple key/value pairs: OK Obtain multiple key/value pairs: [value01, value02, null] Obtain multiple key/value pairs: [value01, value02, null, null] Delete multiple key-value pairs: [NULL, null, null] ===== Add a key/value pair to prevent overwriting the original value ===== 1 1 0 value1 value2 ===== Add a key/value pair and set the valid time ===== OK value3 null ===== Obtain the original value, Updated to new value ===== value2 key2GetSet Gets the value of key2 string: y2GCopy the code

The List (List)

    @Test
    void TestList(a) {
        Jedis jedis = new Jedis("127.0.0.1".6379);
        jedis.flushDB();

        System.out.println("===== add a List=====");
        jedis.lpush("collections"."ArrayList"."Vector"."Stack"."HashMap"."WeakHashMap"."LinkedHashMap");
        jedis.lpush("collections"."HashSet");
        jedis.lpush("collections"."TreeSet");
        jedis.lpush("collections"."TreeMap");
        System.out.println("Contents of Collections" + jedis.lrange("collections".0, -1));// -1 is the reciprocal
        System.out.println("Elements of the collections interval 0-3:" + jedis.lrange("collections".2.3));

        System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
        // Delete the value specified in the list. The second parameter is the number of deleted values (if there is duplication). The added value is deleted first
        System.out.println("Delete specified number of elements:" + jedis.lrem("collectionws".2."HashMap"));
        System.out.println("Contents of Collections:" + jedis.lrange("collections".0, -1));
        System.out.println("Delete elements other than those in table 0-3 below: + jedis.ltrim("collections".0.3));
        System.out.println("Collections content:" + jedis.lrange("collections".0, -1));
        System.out.println("Collections list off the stack (left end):" + jedis.lpop("collections"));
        System.out.println("Contents of Collections:" + jedis.lrange("collections".0, -1));
        System.out.println("Collections adds elements from the right side of the list, corresponding to lpush:" + jedis.rpush("collections"."test"));
        System.out.println("Contents of Collections:" + jedis.lrange("collections".0, -1));
        System.out.println("Collections list off the stack (right end):" + jedis.rpop("collections"));
        System.out.println("Contents of Collections:" + jedis.lrange("collections".0, -1));
        System.out.println("Collections specifies the content of subscript 1:" + jedis.lset("collections".1."FLinkedHashMap"));
        System.out.println("Contents of Collections:" + jedis.lrange("collections".0, -1));

        System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
        System.out.println("Length of collections:" + jedis.llen("collections"));
        System.out.println("Get elements with index 2 for Collections" + jedis.lindex("collections".2));
        System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
        jedis.lpush("sortedList"."3"."6"."2"."4"."5"."Seven"."9");
        System.out.println("Before sortedList sort:" + jedis.lrange("sortedList".0, -1));
        System.out.println(jedis.sort("sortedList"));
        System.out.println("After sortedList is sorted:" + jedis.lrange("sortedList".0, -1));
    }
Copy the code

The output

____ ____ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | | | (_ | |))))' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.1) 10:43:37 2021-01-09. 12624-900 the INFO [main] cn.com.codincge.RedisApplicationTests: Starting RedisApplicationTests using Java 1.8.0_181 on DESKTOP-GMI8GKQ with PID 12624 (Started by MXZ in D:\mxz_code\codingce-java\codingce-redis\redis-01-jedis) 2021-01-09 10:43:37.900 INFO 12624 -- [main] cn.com.codincge.RedisApplicationTests : No active profile set, falling back to default profiles: Default 10:43:38 2021-01-09. 12624-572 the INFO [main] cn.com.codincge.RedisApplicationTests: Started RedisApplicationTests in 0.997 seconds (JVM running for 2.26) ===== Add a List===== collections content [TreeMap, TreeSet, HashSet, LinkedHashMap, WeakHashMap, HashMap, Stack, Vector, ArrayList] collections range 0-3 elements: [HashSet, LinkedHashMap] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = remove the specified number of elements: 0 collections of content: [TreeMap, TreeSet, HashSet, LinkedHashMap, WeakHashMap, HashMap, Stack, Vector, ArrayList] Delete the elements outside the following table 0-3: OK collections contents: [TreeMap, TreeSet, HashSet, LinkedHashMap] Collections list out stack (left end) [TreeSet, HashSet, LinkedHashMap] Collections Adds elements from the right side of the list, corresponding to lpush: [TreeSet, HashSet, LinkedHashMap, test] Collections list out stack (right end): [TreeSet, HashSet, LinkedHashMap] collections specifies subscript 1 content: [TreeSet, FLinkedHashMap, LinkedHashMap] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = the length of the collections: 3 for the collections element with a subscript 2 LinkedHashMap = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = sortedList before ordering: [9, 7, 5, 4, 2, 6, 3], [2, 3, 4, 5, 6, 7, 9] sortedList sorted: [9, 7, 5, 4, 2, 6, 3]Copy the code

Set (Set)

    @Test
    void TestSet(a) {
        Jedis jedis = new Jedis("127.0.0.1".6379);
        jedis.flushDB();
        System.out.println("====== Add elements to the collection (do not repeat)======");
        System.out.println(jedis.sadd("eleSet"."e0"."e1"."e2"."e3"."e4"."e5"));
        System.out.println(jedis.sadd("eleSet"."e6"));
        System.out.println(jedis.sadd("eleSet"."e6"));
        System.out.println("All elements of eleSet are:" + jedis.smembers("eleSet"));
        System.out.println("Delete an element e0" + jedis.srem("eleSet"."e0"));
        System.out.println("Delete two elements e6, e7" + jedis.srem("eleSet"."e7"."e6"));
        System.out.println("All elements of eleSet are:" + jedis.smembers("eleSet"));
        System.out.println("Randomly removes an element from the set:" + jedis.spop("eleSet"));
        System.out.println("Randomly removes an element from the set:" + jedis.spop("eleSet"));
        System.out.println("All elements of eleSet are:" + jedis.smembers("eleSet"));
        System.out.println("Number of elements in eleSet:" + jedis.scard("eleSet"));
        System.out.println("Is E3 in eleSet?" + jedis.sismember("eleSet"."e3"));
        System.out.println("Is E1 in eleSet:" + jedis.sismember("eleSet"."e1"));
        System.out.println("Is E5 in eleSet?" + jedis.sismember("eleSet"."e5"));
        System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
        System.out.println(jedis.sadd("eleSet1"."e0"."e1"."e2"."e3"."e4"."e5"));
        System.out.println(jedis.sadd("eleSet2"."e0"."e1"."e2"."e3"."e4"."e5"."e6"."e7"."e8"));
        System.out.println("Delete E1 from eleSet1 and store in eleSet3:" + jedis.smove("eleSet1"."eleSet3"."e1"));
        System.out.println("Delete E2 from eleSet2 and store in eleSet3: + jedis.smove("eleSet2"."eleSet3"."e2"));
        System.out.println("Elements in eleSet1:" + jedis.smembers("eleSet1"));
        System.out.println("Elements in eleSet2:" + jedis.smembers("eleSet2"));
        System.out.println("===== Set operation =====");
        System.out.println("Elements in eleSet1:" + jedis.smembers("eleSet1"));
        System.out.println("Elements in eleSet2:" + jedis.smembers("eleSet2"));
        System.out.println("Intersection of eleSet1 and eleSet2." + jedis.sinter("eleSet1"."eleSet2"));
        System.out.println("Union of eleSet1 and eleSet2" + jedis.sunion("eleSet1"."eleSet2"));
        System.out.println("Difference set of eleSet1 and eleSet2" + jedis.sdiff("eleSet1"."eleSet2")); //eleSet1 has eleSet1, EleSet2 does not
        jedis.sinterstore("eleSet4"."eleSet1"."eleSet2"); // Find the intersection and save the intersection to the set of dstkeys
        System.out.println("Elements in eleSet4:" + jedis.smembers("eleSet4"));
    }
Copy the code

The output

____ ____ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | | | (_ | |))))' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.1) 11:25:15 2021-01-09. 6516-843 the INFO [main] cn.com.codincge.RedisApplicationTests: Starting RedisApplicationTests using Java 1.8.0_181 on DESKTOP-GMI8GKQ with PID 6516 (Started by MXZ in D:\mxz_code\codingce-java\codingce-redis\redis-01-jedis) 2021-01-09 11:25:15.843 INFO 6516 -- [main] cn.com.codincge.RedisApplicationTests : No active profile set, falling back to default profiles: Default 11:25:16 2021-01-09. 6516-718 the INFO [main] cn.com.codincge.RedisApplicationTests: Started RedisApplicationTests in 1.207 seconds (JVM running for 2.739) ====== Add element to set ====== 6 1 0 eleSet [e5, e1, e0, e2, e3, e6, e4] Remove one element e01 remove two elements e6 e71 eleSet all elements are: E2 All eleSet elements are as follows: [E1, e3, e4] Number of eleSet elements: 3 E3 In eleSet: true E1 in eleSet: true e5 in eleSet: False = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 6 9 will delete eleSet1 e1 and deposited in the eleSet3:1 will delete eleSet2 e2 and deposited in the eleSet3:1 eleSet1 elements in: [e3, e5, e0, e2, e4] eleSet2 elements: [E1, e0, e3, e6, e4, e8, e5, e7] ===== Set operation ===== eleSet1 elements: [e3, E5, e0, e2, e4] eleSet2 [e1, e0, e3, e6, e4, e8, e5, e7] Intersection of eleSet1 and eleSet2 [e3, e5, e0, e4] Union of eleSet1 and eleSet2 [e7, e5, E1, e0, e8, e2, e3, e4, E6] eleSet1 and eleSet2 difference set [e2] eleSet4 elements: [e0, e5, e4, e3]Copy the code

Hash (Hash)

    @Test
    void TestHash(a) {
        Jedis jedis = new Jedis("127.0.0.1".6379);
        jedis.flushDB();
        Map<String, String> map = new HashMap<>();
        map.put("key1"."value1");
        map.put("key2"."value2");
        map.put("key3"."value3");
        map.put("key4"."value4");
        // Add a hash element named hash(key)
        jedis.hmset("hash", map);
        // Add the element with key5 and value value5 to the hash with the hash name
        jedis.hset("hash"."key5"."value5");
        System.out.println("All key-value pairs of the hash are:" + jedis.hgetAll("hash"));
        System.out.println("All keys of the hash are:" + jedis.hkeys("hash")); //return Set<String>
        System.out.println("All values of the hash are:" + jedis.hvals("hash")); //return List<String>
        System.out.println("Add an integer to the saved value of key6, or add key6 if it does not exist:" + jedis.hincrBy("hash"."key6".1));
        System.out.println("All key-value pairs of the hash are:" + jedis.hgetAll("hash"));
        System.out.println("Add an integer to the saved value of key6, or add key6 if it does not exist:" + jedis.hincrByFloat("hash"."key6".1.0));
        System.out.println("All key-value pairs of the hash are:" + jedis.hgetAll("hash"));
        System.out.println("Delete one or more key-value pairs:" + jedis.hdel("hash"."key2"));
        System.out.println("All key-value pairs of the hash are:" + jedis.hgetAll("hash"));
        System.out.println("Number of key-value pairs for hash:" + jedis.hlen("hash"));
        System.out.println("Determine if key2 exists in the hash:" + jedis.hexists("hash"."key2"));
        System.out.println("Determine if key3 exists in the hash:" + jedis.hexists("hash"."key3"));
        System.out.println("Get the value in hash:" + jedis.hmget("hash"."key3"));
        System.out.println("Get the value in hash:" + jedis.hmget("hash"."key3"."key4"));
    }
Copy the code

The output

____ ____ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | | | (_ | |))))' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.1) 13:27:34 2021-01-09. 11788-237 the INFO [main] cn.com.codincge.RedisApplicationTests: Starting RedisApplicationTests using Java 1.8.0_181 on DESKTOP-GMI8GKQ with PID 11788 (Started by MXZ in D:\mxz_code\codingce-java\codingce-redis\redis-01-jedis) 2021-01-09 13:27:34.242 INFO 11788 -- [main] cn.com.codincge.RedisApplicationTests : No active profile set, falling back to default profiles: Default 13:27:35 2021-01-09. 11788-059 the INFO [main] cn.com.codincge.RedisApplicationTests: Started RedisApplicationTests in 1.276 seconds (JVM running for 3.17) {key1=value1, key2=value2, key5=value5, key3=value3, key4=value4} All keys of the hash are: [key1, key2, key5, key3, key4] All values of the hash are: [value1, value3, value4, value2, value5] Add an integer to the saved value of key6, if key6 does not exist add all key-value pairs of the key6:1 hash to: {key1=value1, key2=value2, key5=value5, key6=1, key3=value3, key4=value4} Add an integer to the saved value of key6, or add key6 if key6 does not exist: All key-value pairs of the 2.0 hash are: {key1=value1, key2=value2, key5=value5, key6=2, key3=value3, key4=value4} Delete one or more key-value pairs: 1 All key-value pairs of the hash are as follows: {key1= Value1, KEY5 = Value5, KEY6 =2, key3=value3, key4= Value4} Number of all key-value pairs of the hash: 5 Check whether key2 exists in the hash. False Determine whether the hash exists. Key3: true Obtain the value in the hash: [value3] Obtain the value in the hash: [value3, value4]Copy the code

Project address github.com/xzMhehe/cod…