Redis哈希Hash鍵值對(duì)集合操作(查詢?cè)黾有薷?
一、哈希 Hash 鍵值對(duì)集合
Redis 中的 Hash 數(shù)據(jù) 是一個(gè) 鍵值對(duì)集合 , 類似于 Java 中的 Map 集合 ;
Hash 數(shù)據(jù)底層數(shù)據(jù)結(jié)構(gòu)是 :
- 壓縮列表 ZipList : Hash 中的 鍵值對(duì) 長(zhǎng)度較短時(shí) 使用 壓縮列表 ;
 - 哈希表 HashTable : Hash 中的 鍵值對(duì) 長(zhǎng)度較長(zhǎng)時(shí) 使用 哈希表 ;
 
Redis 中存儲(chǔ)對(duì)象的方式 :
存儲(chǔ)序列化之后的數(shù)據(jù) : 將 對(duì)象 序列化為 json 字符串 , 然后 存儲(chǔ)到 Redis 鍵值對(duì) 的 Value 值中 ;
- 如果要修改對(duì)象中的數(shù)據(jù) , 要 先將對(duì)象反序列化 , 然后修改對(duì)象中的值 , 最后將對(duì)象序列化并保存 ;
 
直接存儲(chǔ)對(duì)象字段 : 將每個(gè)對(duì)象的字段拆開 , 進(jìn)行分開存儲(chǔ) , 非常繁瑣 ;
- 每個(gè) Redis 的 鍵 都保存一個(gè) 對(duì)象字段 , 一個(gè)對(duì)象可能要消耗多個(gè) 鍵 ;
 
使用 Hash 存儲(chǔ) ( 推薦 ) : 將 對(duì)象 的 字段 , 都以 Hash 的 鍵值對(duì) 形式存儲(chǔ)起來 , 可以直接訪問修改對(duì)應(yīng)的對(duì)象字段 ;
- 每個(gè) Redis 鍵 保存一個(gè)對(duì)象 , 對(duì)象的屬性 由 Hash 鍵值對(duì) 保存 ;
 
鍵值對(duì)區(qū)分 : Redis 中的鍵值對(duì) 一般稱為 Key=Value , 在 Hash 中的鍵值對(duì) 一般稱為 Field=Value ;
二、查詢操作
1、Redis 中查詢 Hash 鍵值對(duì)數(shù)據(jù)
執(zhí)行
hget student name
命令 , 可以 獲取 Redis 中 student 鍵 對(duì)應(yīng)的 Hash 數(shù)據(jù)中的 name 鍵 對(duì)應(yīng)的 值 ;
代碼示例 :
127.0.0.1:6379> hset student name Tom (integer) 1 127.0.0.1:6379> get student (error) WRONGTYPE Operation against a key holding the wrong kind of value 127.0.0.1:6379> hget student (error) ERR wrong number of arguments for 'hget' command 127.0.0.1:6379> hget student name "Tom" 127.0.0.1:6379>

注意 : 讀取該 Hash 的 name=Tom 鍵值對(duì) 時(shí) , 需要使用 hget student name 命令 ;
2、查詢 Hash 鍵是否存在
執(zhí)行
hexists student name
命令 , 可以 獲取 Redis 中 student 鍵 對(duì)應(yīng)的 Hash 數(shù)據(jù)中的 name 鍵 是否存在 ;
- 如果存在 , 返回 1 ;
 - 如果不存在 , 返回 0 ;
 
代碼示例 :
127.0.0.1:6379> hexists student name (integer) 1 127.0.0.1:6379> hexists student name1 (integer) 0 127.0.0.1:6379>

3、查詢 Hash 中所有的鍵 Field
執(zhí)行
hkeys student
命令 , 可以 獲取 Redis 中 student 鍵 對(duì)應(yīng)的 Hash 數(shù)據(jù)中的 所有 鍵 Field ;
代碼示例 :
127.0.0.1:6379> hkeys student 1) "name" 2) "age" 127.0.0.1:6379>

4、查詢 Hash 中所有的值
執(zhí)行
hvals student
命令 , 可以 獲取 Redis 中 student 鍵 對(duì)應(yīng)的 Hash 數(shù)據(jù)中的 所有 值 ;
代碼示例 :
127.0.0.1:6379> 127.0.0.1:6379> hvals student 1) "Tom" 2) "18" 127.0.0.1:6379>

三、增加操作
1、Redis 中插入 Hash 鍵值對(duì)數(shù)據(jù)
執(zhí)行
hset student name Tom
命令 , 可以 給 鍵 student 中的 Hash 數(shù)據(jù)值 中 添加 name=Tom 鍵值對(duì) ;
代碼示例 : 向 Redis 的 student 鍵值 下 插入 name=Tom 鍵值對(duì) ;
127.0.0.1:6379> hset student name Tom (integer) 1 127.0.0.1:6379> get student (error) WRONGTYPE Operation against a key holding the wrong kind of value 127.0.0.1:6379> hget student (error) ERR wrong number of arguments for 'hget' command 127.0.0.1:6379> hget student name "Tom" 127.0.0.1:6379>

注意 : 讀取該 Hash 的 name=Tom 鍵值對(duì) 時(shí) , 需要使用 hget student name 命令 ;
2、批量插入 Hash 鍵值對(duì)數(shù)據(jù)
執(zhí)行
hmset student name Tom age 18
命令 , 可以 給 鍵 student 中的 Hash 數(shù)據(jù)值 中 添加 name=Tom 和 age=18 鍵值對(duì) ;
代碼示例 : 向 Redis 的 student 鍵值 下 插入 name=Tom 和 age=18 鍵值對(duì) ;
127.0.0.1:6379> hmset student name Tom age 18 OK 127.0.0.1:6379> hget student age "18" 127.0.0.1:6379> hget student name "Tom" 127.0.0.1:6379>

四、修改操作
1、Hash 中 Field 鍵對(duì)應(yīng)值增減值
執(zhí)行
hincrby student age -5
命令 , 可以 給 鍵 student 中的 Hash 數(shù)據(jù)值 中 age=18 數(shù)據(jù)中的值 -5 操作 ;
代碼示例 :
127.0.0.1:6379> 127.0.0.1:6379> hincrby student age -5 (integer) 13 127.0.0.1:6379> hvals student 1) "Tom" 2) "13" 127.0.0.1:6379>

2、設(shè)置 Hash 中 Field 鍵對(duì)應(yīng)值
執(zhí)行
hsetnx student weight 85
命令 , 可以 在 鍵 student 中的 Hash 數(shù)據(jù)值 中 如果不存在 weight 鍵 , 則 添加 weight=85 鍵值對(duì)數(shù)據(jù) ;
代碼示例 :
127.0.0.1:6379> 127.0.0.1:6379> hsetnx student weight 85 (integer) 1 127.0.0.1:6379> hkeys student 1) "name" 2) "age" 3) "weight" 127.0.0.1:6379>

到此這篇關(guān)于Redis哈希Hash鍵值對(duì)集合操作(查詢?cè)黾有薷?的文章就介紹到這了,更多相關(guān)Redis Hash鍵值操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
 RedisDesktopManager無法遠(yuǎn)程連接Redis的完美解決方法
下載RedisDesktopManager客戶端,輸入服務(wù)器IP地址,端口(缺省值:6379);點(diǎn)擊Test Connection按鈕測(cè)試連接,連接失敗,怎么回事呢?下面小編給大家?guī)砹薘edisDesktopManager無法遠(yuǎn)程連接Redis的完美解決方法,一起看看吧2018-03-03
 redis中事務(wù)機(jī)制及樂觀鎖的實(shí)現(xiàn)
這篇文章主要介紹了redis中事務(wù)機(jī)制及樂觀鎖的相關(guān)內(nèi)容,通過事務(wù)的執(zhí)行分析Redis樂觀鎖,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
 Redisson實(shí)現(xiàn)分布式鎖、鎖續(xù)約的案例
這篇文章主要介紹了Redisson如何實(shí)現(xiàn)分布式鎖、鎖續(xù)約,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
 Redis結(jié)合Lua腳本實(shí)現(xiàn)分布式鎖詳解
Lua?是一種輕量小巧的腳本語(yǔ)言,用標(biāo)準(zhǔn)C語(yǔ)言編寫并以源代碼形式開放,?本文主要為大家介紹了Redis如何結(jié)合Lua腳本實(shí)現(xiàn)分布式鎖,需要的可以參考下2024-02-02
 redis客戶端連接錯(cuò)誤 NOAUTH Authentication required
本文主要介紹了redis客戶端連接錯(cuò)誤 NOAUTH Authentication required,詳細(xì)的介紹了解決方法,感興趣的可以了解一下2021-07-07

