Redis数据类型--散列类型
夫陶公清風(fēng)千古,余又何人,敢稱庶幾
文章目錄
- 介紹
- 一、基本命令
- 1. 賦值與取值
- 2. 判斷字段是否存在
- 3.當(dāng)字段不存在時賦值
- 4.增加數(shù)字
- 5.刪除字段
- 二、命令進階
- 1.只獲取字段名或字段值
- 2. 獲得字段數(shù)量
介紹
Hashes,由field和關(guān)聯(lián)的value組成的map。field和value都是字符串的, hash 可以存儲 2^32^ - 1 鍵值對(40多億)
一、基本命令
| HSET key field value | 一次設(shè)置單個鍵值 |
| HGET key field | 一次獲得單個值 |
| HMSET key field value [field value …] | 同時設(shè)置多個鍵值 |
| HMGET key field [field …] | 同時獲取多個鍵值 |
| HGETALL key | 一次獲取所有的鍵值 |
| HEXISTS key field | 判斷字段是否存在 |
| HSETNX key field value | 當(dāng)字段不存在時賦值 |
| HINCRBY key field increment | 增加數(shù)字 |
| HDEL key field [field …] | 刪除字段 |
1. 賦值與取值
HSET key field value
HGET key field
HMSET key field value [FIELD VALUE …]
hget key field [field …]
HGETALL key // 獲取所有字段的值
- HSET/HGET
一次只能設(shè)置和獲取一個字段的值。
解釋:案例中user為鍵,id、name、age為屬性,在面向?qū)ο笾?#xff0c;user相當(dāng)于類,其他三個字段相當(dāng)于成員屬性
實踐:使用Java操作Redis數(shù)據(jù)庫:HSET、HGET
@Testpublic void testHSETAndHGET() {Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.hset("user", "id", "1001");jedis.hset("user", "name", "breez");jedis.hset("user", "age", "22");String id = jedis.hget("user", "id");String name = jedis.hget("user", "name");String age = jedis.hget("user", "age");System.out.println("id:" + id);System.out.println("name:" + name);System.out.println("name:" + age);} id:1001 name:breez name:22- HMSET/HMGET
一次可以同時設(shè)置和獲取多個字段的值
- HGETALL
同時獲取所有字段的值
實踐:使用Java操作Redis數(shù)據(jù)庫:HMSET、HMGET
@Testpublic void testHMSETAndHMGET() {Jedis jedis = new Jedis("127.0.0.1", 6379);HashMap<String, String> map = new HashMap<>();map.put("id", "1001");map.put("name", "breez");map.put("age", "22");jedis.hmset("user", map);List<String> user = jedis.hmget("user", new String[]{"id", "name", "age"});user.forEach(u -> System.out.println(u));} 1001 breez 222. 判斷字段是否存在
HEXISTS key field
此命令用來判斷一個字段是否存在。如果存在返回1,不能存在返回0,如果鍵不存在也返回0.
127.0.0.1:6379> keys * 1) "user" 127.0.0.1:6379> hgetall user 1) "id" 2) "1001" 3) "name" 4) "breez" 5) "age" 6) "22" 127.0.0.1:6379> hexists user name //name字段存在返回 1 (integer) 1 127.0.0.1:6379> hexists user other //other字段不存在返回0 (integer) 0 127.0.0.1:6379> hexists other other //other鍵不存在返回0 (integer) 0實踐:使用Java操作Redis數(shù)據(jù)庫:HEXISTS
@Testpublic void testHEXISTS() {Jedis jedis = new Jedis("127.0.0.1", 6379);Boolean user_name = jedis.hexists("user", "name");Boolean user_other = jedis.hexists("user", "other");Boolean other_other = jedis.hexists("other", "other");System.out.println("user_name:" + user_name);System.out.println("user_other:" + user_other);System.out.println("other_other:" + other_other);} user_name:true user_other:false other_other:false3.當(dāng)字段不存在時賦值
HSETNX key field value
HSETNX 命令與HSET 命令類似,區(qū)別在于如果字段已經(jīng)存在,HSETNX命令將不執(zhí)行任何操作。
127.0.0.1:6379> keys * 1) "user" 127.0.0.1:6379> hgetall user 1) "id" 2) "1001" 3) "name" 4) "breez" 5) "age" 6) "22" 127.0.0.1:6379> hsetnx user id 1001 //id字段存在 (integer) 0 127.0.0.1:6379> hsetnx user id 1002 //id字段存在 (integer) 0 127.0.0.1:6379> hsetnx user sex male //sex 字段不存在 (integer) 1實踐:使用Java操作Redis數(shù)據(jù)庫:HSETNX
@Testpublic void testHSETNX() {Jedis jedis = new Jedis("127.0.0.1", 6379);Set<String> keys = jedis.keys("*");Map<String, String> user = jedis.hgetAll("user");System.out.println("所有的鍵:" + keys);System.out.println("user鍵中所有的鍵值:" + user);Long hsetnx_1 = jedis.hsetnx("user", "id", "1001");Long hsetnx_2 = jedis.hsetnx("user", "id", "1002");Long hsetnx_sex = jedis.hsetnx("user", "sex", "male");System.out.println(hsetnx_1);System.out.println(hsetnx_2);System.out.println(hsetnx_sex);} 所有的鍵:[user] user鍵中所有的鍵值:{name=breez, age=22, id=1001} 0 0 14.增加數(shù)字
HINCRBY key field increment
此命令的默認(rèn)值為0.
127.0.0.1:6379> keys * 1) "user" 127.0.0.1:6379> hgetall user 1) "id" 2) "1001" 3) "name" 4) "breez" 5) "age" 6) "22" 7) "sex" 8) "male" 127.0.0.1:6379> hincrby user score 20 (integer) 20 127.0.0.1:6379> hget user score "20" 127.0.0.1:6379> hincrby user score 30 (integer) 50 127.0.0.1:6379> hget user score "50" 127.0.0.1:6379> hgetall user1) "id"2) "1001"3) "name"4) "breez"5) "age"6) "22"7) "sex"8) "male"9) "score" 10) "50"實踐:使用Java操作Redis數(shù)據(jù)庫:HINCRBY
@Testpublic void testHINCRBY() {Jedis jedis = new Jedis("127.0.0.1", 6379);Map<String, String> user = jedis.hgetAll("user");System.out.println(user);Long score_20 = jedis.hincrBy("user", "score", 20);Long score_30 = jedis.hincrBy("user", "score", 30);System.out.println(score_20);System.out.println(score_30);System.out.println(jedis.hgetAll("user"));} {name=breez, age=22, id=1001, sex=male} 20 50 {name=breez, score=50, id=1001, age=22, sex=male}5.刪除字段
HDEL key field [field …]
HDEL命令可以刪除一個或多個字段,返回值是被刪除字段的個數(shù)。
127.0.0.1:6379> hgetall user //獲取所有字段值1) "id"2) "1001"3) "name"4) "breez"5) "age"6) "22"7) "sex"8) "male"9) "score" 10) "50" 127.0.0.1:6379> hdel user score //刪除score字段 (integer) 1 127.0.0.1:6379> hgetall user //獲取所有的字段 1) "id" 2) "1001" 3) "name" 4) "breez" 5) "age" 6) "22" 7) "sex" 8) "male" 127.0.0.1:6379> hdel user age sex //同時刪除字段age、 sex (integer) 2 127.0.0.1:6379> hgetall user //獲取所有的字段 1) "id" 2) "1001" 3) "name" 4) "breez"實踐:使用Java操作Redis數(shù)據(jù)庫:HDEL
@Testpublic void testHDEL() {Jedis jedis = new Jedis("127.0.0.1", 6379);Map<String, String> user = jedis.hgetAll("user");System.out.println(user);Long hdel = jedis.hdel("user", new String[]{"name", "age"});System.out.println(hdel);System.out.println(jedis.hgetAll("user"));} {name=breez, age=22, id=1001} 2 {id=1001}二、命令進階
| HKEYS key | 只獲取字段名 |
| HVALS key | 只獲取字段值 |
| HLEN key | 獲得字段數(shù)量 |
1.只獲取字段名或字段值
HKEYS key
HVALS key
- HKEYS
實踐:使用Java操作Redis數(shù)據(jù)庫:HKEYS
@Testpublic void testHKEYS() {Jedis jedis = new Jedis("127.0.0.1", 6379);System.out.println(jedis.hgetAll("user"));Set<String> user_keys = jedis.hkeys("user");System.out.println("所有的鍵:" + user_keys);} {name=breez, age=22, id=1001} 所有的鍵:[name, age, id]- HVALS
實踐:使用Java操作Redis數(shù)據(jù)庫:HVALS
@Testpublic void testHVALS() {Jedis jedis = new Jedis("127.0.0.1", 6379);System.out.println(jedis.hgetAll("user"));List<String> user_values = jedis.hvals("user");System.out.println("所有的值:" + user_values);} {name=breez, age=22, id=1001} 所有的值:[1001, breez, 22]2. 獲得字段數(shù)量
HLEN key
127.0.0.1:6379> hkeys user 1) "id" 2) "name" 3) "age" 127.0.0.1:6379> hlen user (integer) 3實踐:使用Java操作Redis數(shù)據(jù)庫:HLEN
@Testpublic void testHLEN() {Jedis jedis = new Jedis("127.0.0.1", 6379);Set<String> user_keys = jedis.hkeys("user");System.out.println("所有的鍵:" + user_keys);Long user_len = jedis.hlen("user");System.out.println("鍵的長度:" + user_len);} 所有的鍵:[name, age, id] 鍵的長度:3Redis中文官方網(wǎng)站:http://www.redis.cn
未完待續(xù),持續(xù)更新中...
總結(jié)
以上是生活随笔為你收集整理的Redis数据类型--散列类型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VBA IsNull 应用 - 捕获错误
- 下一篇: JavaScript-数据类型