One 、Redis Hash (Hash)
Redis hash It's a string Type of field( Field ) and value( value ) Mapping table ,hash Ideal for storing objects .
Redis Each of them hash Can be stored 232 - 1 Key value pair (40 More than ).
A simple example :
127.0.0.1:6379> HMSET runoobkey name "redis tutorial" description "redis basic commands for caching" likes 20 visitors 23000 OK 127.0.0.1:6379> HGETALL runoobkey 1) "name" 2) "redis tutorial" 3) "description" 4) "redis basic commands for caching" 5) "likes" 6) "20" 7) "visitors" 8) "23000"
Two 、Hash Type of operation command
1、hset
hset: Set the value . The format is :hset hash Of key Term key Item value
The operation is as follows :
java Sample code :
import redis.clients.jedis.Jedis;
/**
* redis For in the hash Type of : hset The operation sample
* hset: Set the value . The format is :hset hash Of key Term key Item value
*/
public class Hash_hset_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
/**
* Example 1: hset hash Of key Term key Item value
*/
jedis.hset("myhash","id","3");
jedis.hset("myhash","name","xiaohong");
jedis.hset("myhash","age","13");
}
}
2、hmset
hmset: Set multiple pairs of values at the same time . The format is :hmset hash Of key Term key Item value .( Term key The values of the and terms can be many pairs of )
The operation is as follows :
java Sample code :
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.Map;
/**
* redis For in the hash Type of : hmset The operation sample
* hmset: Set multiple pairs of values at the same time . The format is :hmset hash Of key Term key Item value .( Term key The values of the and terms can be many pairs of )
*/
public class Hash_hmset_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
/**
* Example 1: hmset hash Of key Term key Item value .( Term key The values of the and terms can be many pairs of )
*/
Map<String,String> map = new HashMap<>();
map.put("id","1");
map.put("name","dongdong");
map.put("age","3");
jedis.hmset("myhash",map);
}
}
3、hget
hget: Get value . The format is :hget hash Of key Term key
The operation is as follows :
java Sample code :
import redis.clients.jedis.Jedis;
/**
* redis For in the hash Type of : hget The operation sample
* hget: Get value . The format is :hget hash Of key Term key
*/
public class Hash_hget_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
/**
* Example 1: hget hash Of key Term key
*/
String id = jedis.hget("myhash", "id");
String name = jedis.hget("myhash", "name");
String age = jedis.hget("myhash", "age");
System.out.println(String.format("id = %s, name = %s, age = %s",id,name,age));
}
}
4、hmget
hmget: Get multiple pairs of values at the same time . The format is :hmget hash Of key Term key.( Term key There can be multiple )
The operation is as follows :
java Sample code :
import redis.clients.jedis.Jedis;
import java.util.List;
/**
* redis For in the hash Type of : hmget The operation sample
* hmget: Get multiple pairs of values at the same time . The format is :hmget hash Of key Term key.( Term key There can be multiple )
*/
public class Hash_hmget_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
/**
* Example 1: hmget hash Of key Term key.( Term key There can be multiple )
*/
List<String> hmget = jedis.hmget("myhash", "id", "name", "age");
System.out.println("hmget = " + hmget);
}
}
5、hgetall
hgetall: Get the key Under all the values . The format is :hgetall hash Of key
The operation is as follows :
java Sample code :
import redis.clients.jedis.Jedis;
import java.util.Map;
/**
* redis For in the hash Type of : hgetall The operation sample
* hgetall: Get the key Under all the values . The format is :hgetall hash Of key
*/
public class Hash_hgetall_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
/**
* Example 1: hgetall hash Of key
*/
Map<String, String> myhash = jedis.hgetAll("myhash");
System.out.println("myhash = " + myhash);
}
}
6、hdel
hdel: Delete an item . The format is :hdel hash Of key Term key
The operation is as follows :
java Sample code :
import redis.clients.jedis.Jedis;
import java.util.Map;
/**
* redis For in the hash Type of : hdel The operation sample
* hdel: Delete an item . The format is :hdel hash Of key Term key
*/
public class Hash_hdel_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
Map<String, String> myhash = jedis.hgetAll("myhash");
System.out.println("myhash = " + myhash);
/**
* Example 1: hdel hash Of key Term key
*/
Long hdel = jedis.hdel("myhash", "age");
System.out.println("hdel = " + hdel);
Map<String, String> myhash1 = jedis.hgetAll("myhash");
System.out.println("myhash1 = " + myhash1);
}
}
7、hlen
hlen: obtain key The number of key value pairs inside . The format is :hlen hash Of key
The operation is as follows :
java Sample code
import redis.clients.jedis.Jedis;
import java.util.Map;
/**
* redis For in the hash Type of : hlen The operation sample
* hlen: obtain key The number of key value pairs inside . The format is :hlen hash Of key
*/
public class Hash_hlen_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
Map<String, String> myhash = jedis.hgetAll("myhash");
System.out.println("myhash = " + myhash);
/**
* Example 1: hlen hash Of key
*/
Long hlen = jedis.hlen("myhash");
System.out.println("hlen = " + hlen);
}
}
8、hexists
hexists: Determine whether the key value exists . The format is :hexists hash Of key Term key
The operation is as follows :
java Sample code :
import redis.clients.jedis.Jedis;
import java.util.Map;
/**
* redis For in the hash Type of : hexists The operation sample
* hexists: Determine whether the key value exists . The format is :hexists hash Of key Term key
*/
public class Hash_hexists_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
Map<String, String> myhash = jedis.hgetAll("myhash");
System.out.println("myhash = " + myhash);
/**
* Example 1: hexists hash Of key Term key
*/
Boolean hexists = jedis.hexists("myhash", "id");
System.out.println("hexists = " + hexists);
}
}
9、hkeys
hkeys: Get all item Of key, The format is :hkeys hash Of key
The operation is as follows :
java Sample code :
import redis.clients.jedis.Jedis;
import java.util.Map;
import java.util.Set;
/**
* redis For in the hash Type of : hexists The operation sample
* hkeys: Get all item Of key, The format is :hkeys hash Of key
*/
public class Hash_hkeys_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
Map<String, String> myhash = jedis.hgetAll("myhash");
System.out.println("myhash = " + myhash);
/**
* Example 1: hkeys hash Of key
*/
Set<String> hkeys = jedis.hkeys("myhash");
System.out.println("hkeys = " + hkeys);
}
}
10、hvals
hvals: Get all item Value , The format is :hvals hash Of key
The operation is as follows :
java Sample code :
import redis.clients.jedis.Jedis;
import java.util.List;
import java.util.Map;
/**
* redis For in the hash Type of : hexists The operation sample
* hvals: Get all item Value , The format is :hvals hash Of key
*/
public class Hash_hvals_operation {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
Map<String, String> myhash = jedis.hgetAll("myhash");
System.out.println("myhash = " + myhash);
/**
* Example 1: hvals hash Of key
*/
List<String> hvals = jedis.hvals("myhash");
System.out.println("hvals = " + hvals);
}
}
Related articles :
Redis Hash (Hash) Relevant command
Java in Redis Yes Hash Type of operation command