Memory-based distributed NoSQL database Redis (4) Jedis: How to use it

Article directory

    • Knowledge point 14: Jedis: usage and Jedis dependencies
    • Knowledge point 15: Jedis: building connections
    • Knowledge point 16: Jedis: String operation
    • Knowledge point 17: Jedis: other types of operations
    • postscript

Knowledge point 14: Jedis: usage and Jedis dependencies

  • Goal: Master how to use Redis and build Jedis project dependencies

  • Path

    • step1: How to use Redis
    • step2: Jedis dependencies
  • Implementation

    • How to use Redis

      • Command operation Redis, generally used in the testing and development phase

      • Distributed computing or Java programs read and write Redis, generally used for actual production development

        • Spark/Flink reads and writes Redis

        • The overall operation mode of all databases using Java is similar

          //todo:1-Build client connection object
          Connection conn = DriverManager.getConnect(url,username,password)
          //todo:2-Perform the operation: all operations are in the client connection object: method
          prep.execute(SQL)
          //todo:3-release the connection
          conn.close
          
    • Jedis dependency

      • Refer to Appendix 1 to add dependencies.
  • Summary

    • Master how to use Redis and build Jedis project dependencies

Knowledge point 15: Jedis: building connections

  • Goal: Implement Jedis client connection

  • Implementation

     //todo:1-Build connection object
        Jedisjedis = null;
    
        @Before
        public void getConnection(){<!-- -->
            //Method 1: Directly construct the Jedis object
    // jedis = new Jedis("node1",6379);
            //Method 2: Build Jedis through connection pool
            //Build the connection pool configuration object
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(10);//Total connection construction
            config.setMaxIdle(5);//Maximum idle connection
            config.setMinIdle(2);//Minimum idle connection
            //Build connection pool object
            JedisPool jedisPool = new JedisPool(config,"node1",6379);
            //Get connection
            jedis = jedisPool.getResource();
        }
    
        //todo:2-Call the method of the connection object to implement the operation
    
    
        //todo:3-release the connection
        @After
        public void closeConnection(){<!-- -->
            jedis.close();
        }
    
  • Summary

    • Implement Jedis client connection

Knowledge point 16: Jedis: String operation

  • Goal: Implement String operations in Jedis

  • Implementation

    set/get/incr/exists/expire/setexp/ttl
    
     @Test
        public void testString(){<!-- -->
            //set/get/incr/exists/expire/setexp/ttl
    // jedis.set("s1","hadoop");
    // System.out.println(jedis.exists("s1"));
    // System.out.println(jedis.exists("s2"));
    // System.out.println(jedis.get("s1"));
    // jedis.set("s2","3");
    // jedis.incr("s2");
    // System.out.println(jedis.get("s2"));
    // jedis.expire("s2",10);
    // while(true){<!-- -->
    // System.out.println(jedis.ttl("s2"));
    // }
            //setex: When building KV, directly set the life cycle
            jedis.setex("s3",10,"oozie");
    
        }
    
  • Summary

    • Implementing String operations in Jedis

Knowledge point 17: Jedis: other types of operations

  • Goal: Implement other types of operations in Jedis

  • Implementation

    • Hash type

      hset/hmset/hget/hgetall/hdel/hlen/hexists
      
       public void testHash(){<!-- -->
              //hset/hmset/hget/hgetall/hdel/hlen/hexists
              jedis.hset("m1","name","zhangsan");
              System.out.println(jedis.hget("m1","name"));
              Map<String,String> maps = new HashMap<>();
              maps.put("age","18");
              maps.put("phone","110");
              jedis.hmset("m1",maps);
              List<String> hmget = jedis.hmget("m1", "name", "age");
              System.out.println(hmget);
              System.out.println("=");
              Map<String, String> m1 = jedis.hgetAll("m1");
              for(Map.Entry map : m1.entrySet()){<!-- -->
                  System.out.println(map.getKey() + "\t" + map.getValue());
              }
              System.out.println("=");
              System.out.println(jedis.hlen("m1"));
              jedis.hdel("m1","name");
              System.out.println(jedis.hlen("m1"));
              System.out.println(jedis.hexists("m1","name"));
              System.out.println(jedis.hexists("m1","age"));
          }
      
    • List type

      lpush/rpush/lrange/llen/lpop/rpop
      
       @Test
          public void testList(){<!-- -->
              //lpush/rpush/lrange/llen/lpop/rpop
              jedis.lpush("list1","1","2","3");
              System.out.println(jedis.lrange("list1",0,-1));
              jedis.rpush("list1","4","5","6");
              System.out.println(jedis.lrange("list1",0,-1));
              System.out.println(jedis.llen("list1"));
              jedis.lpop("list1");
              jedis.rpop("list1");
              System.out.println(jedis.lrange("list1",0,-1));
          }
      
    • Set type

      sadd/smembers/sismember/scard/srem
      
       @Test
          public void testSet(){<!-- -->
              //sadd/smembers/sismember/scard/srem
              jedis.sadd("set1","1","2","3","1","2","3","4\ ","5","6");
              System.out.println("Length:" + jedis.scard("set1"));
              System.out.println("Content:" + jedis.smembers("set1"));
              System.out.println(jedis.sismember("set1","1"));
              System.out.println(jedis.sismember("set1","7"));
              jedis.srem("set1","2");
              System.out.println("Content:" + jedis.smembers("set1"));
      
          }
      
    • Zset type

      zadd/zrange/zrevrange/zcard/zrem
      
       @Test
          public void testZset(){<!-- -->
              //zadd/zrange/zrevrange/zcard/zrem
              jedis.zadd("zset1",20.9,"yuwen");
              jedis.zadd("zset1",10.5,"yinyu");
              jedis.zadd("zset1",70.9,"shuxue");
              jedis.zadd("zset1",99.9,"shengwu");
              Set<String> zset1 = jedis.zrange("zset1", 0, -1);
              System.out.println(zset1);
              System.out.println(jedis.zrevrange("zset1",0,-1));
              System.out.println(jedis.zcard("zset1"));
              jedis.zrem("zset1","yuwen");
              System.out.println(jedis.zrangeWithScores("zset1",0,-1));
          }
      
  • Summary

    • Implement other types of operations in Jedis

Postscript

Blog homepage: https://manor.blog.csdn.net

Welcome to like Collect ?Leave a message Please correct me if there are any errors!
This article was originally written by Maynor and first appeared on the CSDN blog
You can’t stare at the phone screen all the time. You should raise your head from time to time to see where the boss is?
The column is continuously updated, welcome to subscribe: https://blog.csdn.net/xianyu120/category_12394313.html