六、Redis中對Hash類型的操做命令

寫在前面的話:讀書破萬卷,編碼若有神
--------------------------------------------------------------------

 

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

一、對Hash類型的操做命令java

1.一、hset:設置值。格式是:hset hash的key 項的key 項的值redis

操做以下:ui

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.二、hmset:同時設置多對值。格式是:hmset hash的key 項的key 項的值。(項的key和項的值能夠多對)spa

操做以下:3d

java示例代碼:code

 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.三、hget:獲取值。格式是:hget hash的key 項的keyorm

操做以下:blog

java示例代碼:ci

 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.四、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.五、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.六、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.七、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.八、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.九、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.十、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.十一、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.十二、hincrbyfloat :增減float數值,格式是:hincrbyfloat Hash的key 項的key 正負float

操做以下:

1.1三、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 }
相關文章
相關標籤/搜索