6、Redis中对Hash类型的操作命令

写在前面的话:读书破万卷,编码如有神
--------------------------------------------------------------------

 

--------------------------------------------------------------------

1、对Hash类型的操作命令

1.1、hset:设置值。格式是:hset hash的key 项的key 项的值

操作如下:

java示例代码:

 1 import redis.clients.jedis.Jedis;
 2 
 3 /**
 4  * redis中对于hash类型的: hset 操作示例
 5  * hset:设置值。格式是:hset hash的key 项的key 项的值
 6  */
 7 public class Hash_hset_operation {
 8     public static void main(String[] args) {
 9         Jedis jedis = new Jedis("127.0.0.1",6379);
10         /**
11          * 示例1: hset hash的key 项的key 项的值
12          */
13         jedis.hset("myhash","id","3");
14         jedis.hset("myhash","name","xiaohong");
15         jedis.hset("myhash","age","13");
16     }
17 }

1.2、hmset:同时设置多对值。格式是:hmset hash的key 项的key 项的值。(项的key和项的值可以多对)

操作如下:

java示例代码:

 1 import redis.clients.jedis.Jedis;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 /**
 7  * redis中对于hash类型的: hmset 操作示例
 8  * hmset:同时设置多对值。格式是:hmset hash的key 项的key 项的值。(项的key和项的值可以多对)
 9  */
10 public class Hash_hmset_operation {
11     public static void main(String[] args) {
12         Jedis jedis = new Jedis("127.0.0.1",6379);
13         /**
14          * 示例1: hmset hash的key 项的key 项的值。(项的key和项的值可以多对)
15          */
16         Map<String,String> map = new HashMap<>();
17         map.put("id","1");
18         map.put("name","dongdong");
19         map.put("age","3");
20         jedis.hmset("myhash",map);
21     }
22 }

1.3、hget:获取值。格式是:hget hash的key 项的key

操作如下:

java示例代码:

 1 import redis.clients.jedis.Jedis;
 2 
 3 /**
 4  * redis中对于hash类型的: hget 操作示例
 5  * hget:获取值。格式是:hget hash的key 项的key
 6  */
 7 public class Hash_hget_operation {
 8     public static void main(String[] args) {
 9         Jedis jedis = new Jedis("127.0.0.1",6379);
10         /**
11          * 示例1: hget hash的key 项的key
12          */
13         String id = jedis.hget("myhash", "id");
14         String name = jedis.hget("myhash", "name");
15         String age = jedis.hget("myhash", "age");
16 
17         System.out.println(String.format("id = %s, name = %s, age = %s",id,name,age));
18     }
19 }

1.4、hmget:同时获取多对值。格式是:hmget hash的key 项的key。(项的key可以有多个)

操作如下:

java示例代码:

 1 import redis.clients.jedis.Jedis;
 2 
 3 import java.util.List;
 4 
 5 /**
 6  * redis中对于hash类型的: hmget 操作示例
 7  * hmget:同时获取多对值。格式是:hmget hash的key 项的key。(项的key可以有多个)
 8  */
 9 public class Hash_hmget_operation {
10     public static void main(String[] args) {
11         Jedis jedis = new Jedis("127.0.0.1",6379);
12         /**
13          * 示例1: hmget hash的key 项的key。(项的key可以有多个)
14          */
15         List<String> hmget = jedis.hmget("myhash", "id", "name", "age");
16         System.out.println("hmget = " + hmget);
17     }
18 }

1.5、hgetall:获取该key下所有的值。格式是:hgetall hash的key

操作如下:

java示例代码:

 1 import redis.clients.jedis.Jedis;
 2 
 3 import java.util.Map;
 4 
 5 /**
 6  * redis中对于hash类型的: hgetall 操作示例
 7  * hgetall:获取该key下所有的值。格式是:hgetall hash的key
 8  */
 9 public class Hash_hgetall_operation {
10     public static void main(String[] args) {
11         Jedis jedis = new Jedis("127.0.0.1",6379);
12         /**
13          * 示例1: hgetall hash的key
14          */
15         Map<String, String> myhash = jedis.hgetAll("myhash");
16         System.out.println("myhash = " + myhash);
17     }
18 }

1.6、hdel:删除某个项。格式是:hdel hash的key 项的key

操作如下:

java示例代码:

 1 import redis.clients.jedis.Jedis;
 2 
 3 import java.util.Map;
 4 
 5 /**
 6  * redis中对于hash类型的: hdel 操作示例
 7  * hdel:删除某个项。格式是:hdel hash的key 项的key
 8  */
 9 public class Hash_hdel_operation {
10     public static void main(String[] args) {
11         Jedis jedis = new Jedis("127.0.0.1",6379);
12 
13         Map<String, String> myhash = jedis.hgetAll("myhash");
14         System.out.println("myhash = " + myhash);
15         /**
16          * 示例1: hdel hash的key 项的key
17          */
18         Long hdel = jedis.hdel("myhash", "age");
19         System.out.println("hdel = " + hdel);
20 
21         Map<String, String> myhash1 = jedis.hgetAll("myhash");
22         System.out.println("myhash1 = " + myhash1);
23     }
24 }

1.7、hlen:获取key里面的键值对数量。格式是:hlen hash的key

操作如下:

java示例代码:

 1 import redis.clients.jedis.Jedis;
 2 
 3 import java.util.Map;
 4 
 5 /**
 6  * redis中对于hash类型的: hlen 操作示例
 7  * hlen:获取key里面的键值对数量。格式是:hlen hash的key
 8  */
 9 public class Hash_hlen_operation {
10 
11     public static void main(String[] args) {
12         Jedis jedis = new Jedis("127.0.0.1",6379);
13 
14         Map<String, String> myhash = jedis.hgetAll("myhash");
15         System.out.println("myhash = " + myhash);
16         /**
17          * 示例1: hlen hash的key
18          */
19         Long hlen = jedis.hlen("myhash");
20         System.out.println("hlen = " + hlen);
21     }
22 }

1.8、hexists:判断键值是否存在。格式是:hexists hash的key 项的key

操作如下:

java示例代码:

 1 import redis.clients.jedis.Jedis;
 2 
 3 import java.util.Map;
 4 
 5 /**
 6  * redis中对于hash类型的: hexists 操作示例
 7  * hexists:判断键值是否存在。格式是:hexists hash的key 项的key
 8  */
 9 public class Hash_hexists_operation {
10     public static void main(String[] args) {
11         Jedis jedis = new Jedis("127.0.0.1",6379);
12 
13         Map<String, String> myhash = jedis.hgetAll("myhash");
14         System.out.println("myhash = " + myhash);
15         /**
16          * 示例1: hexists hash的key 项的key
17          */
18         Boolean hexists = jedis.hexists("myhash", "id");
19         System.out.println("hexists = " + hexists);
20     }
21 }

1.9、hkeys:获取所有的item的key,格式是:hkeys hash的key 

操作如下:

java示例代码:

 1 import redis.clients.jedis.Jedis;
 2 
 3 import java.util.Map;
 4 import java.util.Set;
 5 
 6 /**
 7  * redis中对于hash类型的: hexists 操作示例
 8  * hkeys:获取所有的item的key,格式是:hkeys hash的key
 9  */
10 public class Hash_hkeys_operation {
11     public static void main(String[] args) {
12         Jedis jedis = new Jedis("127.0.0.1",6379);
13 
14         Map<String, String> myhash = jedis.hgetAll("myhash");
15         System.out.println("myhash = " + myhash);
16         /**
17          * 示例1: hkeys hash的key
18          */
19         Set<String> hkeys = jedis.hkeys("myhash");
20         System.out.println("hkeys = " + hkeys);
21     }
22 }

1.10、hvals:获取所有的item的值,格式是:hvals hash的key 

操作如下:

java示例代码:

 1 import redis.clients.jedis.Jedis;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 /**
 7  * redis中对于hash类型的: hexists 操作示例
 8  * hvals:获取所有的item的值,格式是:hvals hash的key
 9  */
10 public class Hash_hvals_operation {
11     public static void main(String[] args) {
12         Jedis jedis = new Jedis("127.0.0.1",6379);
13 
14         Map<String, String> myhash = jedis.hgetAll("myhash");
15         System.out.println("myhash = " + myhash);
16         /**
17          * 示例1: hvals hash的key
18          */
19         List<String> hvals = jedis.hvals("myhash");
20         System.out.println("hvals = " + hvals);
21     }
22 }

1.11、hincrby : 增减整数数字,格式是:hincrby Hash的key 项的key 正负整数

操作如下:

java示例代码:

 1 import redis.clients.jedis.Jedis;
 2 
 3 import java.util.Map;
 4 
 5 /**
 6  * redis中对于hash类型的: hexists 操作示例
 7  * hincrby : 增减整数数字,格式是:hincrby Hash的key 项的key 正负整数
 8  */
 9 public class Hash_hincrby_operation {
10     public static void main(String[] args) {
11         Jedis jedis = new Jedis("127.0.0.1",6379);
12 
13         Map<String, String> myhash = jedis.hgetAll("myhash");
14         System.out.println("myhash = " + myhash);
15         /**
16          * 示例1: hincrby Hash的key 项的key 正负整数
17          */
18         Long hincrBy = jedis.hincrBy("myhash", "id", 4);
19         System.out.println("hincrBy = " + hincrBy);
20 
21         Map<String, String> myhash2 = jedis.hgetAll("myhash");
22         System.out.println("myhash2 = " + myhash2);
23     }
24 }

1.12、hincrbyfloat :增减float数值,格式是:hincrbyfloat Hash的key 项的key 正负float

操作如下:

1.13、hsetnx : 如果项不存在则赋值,存在时什么都不做,格式是:hsetnx Hash的key 项的key 项的值

操作如下:

java示例代码:

 1 import redis.clients.jedis.Jedis;
 2 
 3 import java.util.Map;
 4 
 5 /**
 6  * redis中对于hash类型的: hexists 操作示例
 7  * hsetnx : 如果项不存在则赋值,存在时什么都不做,格式是:hsetnx Hash的key 项的key 项的值
 8  */
 9 public class Hash_hsetnx_operation {
10 
11     public static void main(String[] args) {
12         Jedis jedis = new Jedis("127.0.0.1",6379);
13 
14         Map<String, String> myhash = jedis.hgetAll("myhash");
15         System.out.println("myhash = " + myhash);
16         /**
17          * 示例1: hsetnx Hash的key 项的key 项的值
18          */
19         jedis.hsetnx("myhash","id","123");
20         jedis.hsetnx("myhash","address","chengdou");
21 
22         Map<String, String> myhash2 = jedis.hgetAll("myhash");
23         System.out.println("myhash2 = " + myhash2);
24     }
25 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2018-07-11 20:00  火爆泡菜  阅读(25845)  评论(0编辑  收藏