@[toc]

Next post: Jedis connection pooling

To use Java to operate redis, you need to ensure that the redis service is installed on the server and that the Java Redis driver (Jedis) is already available on the local project.

Maven introduces Jedis dependencies

This needs to be copied in the central warehouse

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
Copy the code

Jedis connection and data access

public class JedisExercise {
    public static void main(String[] args){
        /*=========== Connect to the Redis service ===========*/
        / / IP and port
        Jedis jedis = new Jedis("127.0.0.1".6379);
        
        // Verify password" password"
        jedis.auth("password");
        System.out.println("Login to Redis database successfully");
        
        /*=========== redis String operation ===========*/
        // Add and read data, overwrite it if it exists
        jedis.set("string"."String");
        System.out.println("Add String data:" + jedis.get("string"));
        
        // Splice data
        jedis.append("string"."Redis storage");
        System.out.println("After concatenating String data:" + jedis.get("string"));
        
        // Delete data
        jedis.del("string");
        System.out.println("After deleting String data:" + jedis.get("string"));
        
        // Add data in batches
        jedis.mset("string1"."String 1"."string2"."String 2"."string3"."String 3");
        System.out.println("Batch add String data" + jedis.get("string1") + jedis.get("string2") + jedis.get("string3"));
        
        /*=========== redis Hash operation ===========*/
        // Add data
        Map<String, String> map = new Map<String, String>();
        map.put("key1"."value1");
        map.put("key2"."value2");
        map.put("key3"."value3");
        jedis.hmset("hash", map);
        List<String> list = jedis.hmget("hash"."key1"."key2"."key3");
        System.out.println("New enhancement of data in Hash data types:" + list);
        
        // The iterator reads data
        Iterator<String> it = jedis.hkeys("hash").iterator();
        while(it.hasNext()){
            String key = it.next();
            System.out.println(key + ":" + jedis.hget("hash", key));
        }
        
        // Splice data
        jedis.hset("hash"."key4"."value4");
        System.out.println("After concatenating Hash data:" + jedis.hmget("hash"."key1"."key2"."key3"."key4"));
        
        // Return Hash to store the associated data value
        System.out.println("Number of values stored in this Hash store:" + jedis.hlen("hash"));
        System.out.println("Is there a record where key is user:" + jedis.exists("hash"));
        System.out.println("All keys in the Hash store" + jedis.hkeys("hash"));
        System.out.println("All values in the Hash store" + jedis.hvals("hash"));
        
        // Delete data
        jedis.hdel("hash"."key1");
        System.out.println("Delete data from Hash data type:" + jedis.hmget("hash"."key1"));
        jedis.del("hash");
        System.out.println("Delete the entire Hash store");
       
        /*=========== redis List operation ===========*/
        // Add and read data
        jedis.lpush("list"."Table data");
        jedis.lpush("list"."Table data");
        jedis.lpush("list"."Table Data 3");
        jedis.lpush("list"."Table Data 4");
        List<String> list = jedis.lrange("list".0.3);
        System.out.prinln("New increment of data in List data:" + list);
        
        // Delete data
        String popStr = jedis.lpop("list");
        System.out.prinln("The leftmost data removed is:" + popStr);
        List<String> strAfterPop = jedis.lrange("list".0, -1);
        System.out.println("The data after removal is:" + strAfterPop);
        
        /*=========== redis Set operation ===========*/
        // Add and read data
        jedis.sadd("set"."Set data");
        jedis.sadd("set"."Set data 2");
        jedis.sadd("set"."Set data 3");
        jedis.sadd("set"."Set data");
        Set<String> set = jedis.smembers("set");
        System.out.println("Add Set type data:" + set);
        
        // Return Set to store related data values
        System.out.prinltn("Determine if an element is a Set element:" + jedis.sismember("set"."Set element 1"));
        System.out.println("Return a random element in the set:" + jedis.srandmember("set"));
        System.out.println("Number of elements in Set:" + jedis.scard("set"));
        jedis.sadd("set2"."Set data");
        jedis.sadd("set2"."Set data 4");
        System.out.println("Get intersection of two databases:" + jedis.sinter("set"."set2"));
        System.out.println("Get the union of two databases:" + jedis.sunion("set"."set2"));
        
        // Delete data
        jedis.srem("set"."Set data 2");
        
        /*=========== redis ZSet operation ===========*/
        // Add and read data
        jedis.zadd("zset".1."Z set data 1");
        jedis.zadd("zset".5."Z set data 2");
        jedis.zadd("zset".22."Z set data 3");
        jedis.zadd("zset".22."Z set data 4");
        Set<String> zset = jedis.zrange("zset".0, -1);
        System.out.println("All data in ZSet:" + zset);
        
        // Delete data
        jedis.zremrangeByScore("zset".1.5);
        System.out.println("After removing data 1-5 by score:", zset); }}Copy the code

Next post: Jedis connection pooling