hash hash operation

Hash is a mapping table of field and value of string type. Hash is especially suitable for storing objects. Its operation is very similar to String operation.

 

1. Set the value of the field field in the hash table key to value HSET key field value 

2. Get the value of the specified field stored in the hash table. HGET key field 

3. Simultaneously set multiple field-value (field-value) pairs to the hash table key. HMSET key field1 value1 [field2 value2 ] 

4. Get the value of all given fields HMGET key field1 [field2] 

5. Check whether the specified field exists in the hash table key. HEXISTS key field 

6. Delete one or more hash table fields HDEL key field1 [field2] 

7. Set the value of the field field in the hash table key to value. HSET key field value 

8. Get the number of fields in the hash table HLEN key 

9. Get the field HKEYS key in all hash tables 

10. Get all the values ​​in the hash table HVALs key 

11. Get all fields and values ​​of the specified key in the hash table HGETALL key 

12. Add increment to the integer value of the specified field in the hash table key. HINCRBY key field increment 

 

Hash code operation:

package com.study.util;

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

import redis.clients.jedis.Jedis;

public class RedisHash {

    public static void main(String[] args) {
        Jedis jedis = RedisUtil.getJedis();
         // Assign the user's id to 1 
        jedis.hset("user", "id", "1" );
         // Get the user's id 
        String id = jedis.hget(" user", "id" );
        System.out.println("id:" + id);
        
        // Set the user's name to zs and age to 18 
        Map<String,String> hash = new HashMap<> ();
        hash.put("name", "zs");
        hash.put("age", "18");
        jedis.hmset("user", hash);
        //获取user的id,name,age值
        List<String> hList = jedis.hmget("user", "id","name","age");
        System.out.print("user的id,name,age值为:");
        for (String value : hList) {
            System.out.print(value);
        }
        System.out.println();
        
        // Check if user has id field 
        boolean hexists = jedis.hexists("user", "id" );
        System.out.println( "Does the user have an id field: " + hexists);
        
        // Delete the age field in 
        user jedis.hdel("user", "age" );
        System.out.print( "User's field after deleting age field in user:" );
         // Get all fields of user 
        Set<String> keys = jedis.hkeys("user" );
         for (String key : keys) {
            System.out.print(key);
        }
        System.out.println();
        // Change the name value of user to zhangsan 
        jedis.hset("user", "name", "zhangsan" );
         // Get the number of user fields 
        long length = jedis.hlen("user" );
        System.out.println( "Number of user fields: " + length);
        
        // Get all the values ​​of the user field 
        List<String> values ​​= jedis.hvals("user" );
        System.out.print( "All values ​​of user field: " );
         for (String value : values) {
            System.out.print(value);
        }
        System.out.println();
        
        // Get all fields of user and corresponding values 
        ​​Map<String,String> map = jedis.hgetAll("user" );
         for (String key : map.keySet()) {
            System.out.println(key + "->" + map.get(key));
        }
        
        jedis.close();
    }
}
View Code

 

Code git address: https://gitee.com/sjcq/redis.git

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325052636&siteId=291194637