redis基本命令hash


1.hset

hset(name,key,value)

設置name對應的hash中的一個鍵值對,不存在則創建;存在則修改。

import redis,time
r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hset('hash1','name','wl')  # 設置值
print(r.hkeys('hash1'))  # 取hash中的所有key  ['name']
print(r.hget('hash1','name'))  # 取hash中的name值  wl

2.hsetenx

hsetnx(name,key,value)

設置那么中對應的鍵值對,是只能新建攸,親。不能修改

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hsetnx('hash1','name','wl')  # 設置值
r.hsetnx('hash1','name','wl1')  # 設置值
print(r.hget('hash1','name'))  # wl

3.hmset

hmset(name,mapping),批量設置。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 設置值
print(r.hgetall('hash1'))  # {'name1': 'wl1', 'name': 'wl'}

4.hget和hmget

  • hget(name,key):獲取1個key對應的值
  • hmget(name,key,args):獲取多個個key對應的值
import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 設置值
print(r.hget('hash1','name'))  # wl
print(r.hmget('hash1','name','name1'))  #  ['wl', 'wl1']

5.hgetall

hgetall(name)—獲取name中所有的鍵值對

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 設置值
print(r.hgetall('hash1'))  # {'name1': 'wl1', 'name': 'wl'}

6.hlen

hlen(name)—獲取name的長度。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 設置值
print(r.hlen('hash1'))  # 獲取hash中鍵值對的個數   2

7.hkeys

hkeys(name)—獲取name中所有的個數。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 設置值
print(r.hkeys('hash1'))  # ['name', 'name1']

8.hvals

hvals(name)—獲取name中所有的value。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 設置值
print(r.hvals('hash1'))  # ['wl', 'wl1']

9.hexists

hexists(name,key)

判斷key對應的值是否在name中,就是判斷成員是否存在。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 設置值
print(r.hexists('hash1','name'))  # True
print(r.hexists('hash1','name2'))  # False

10.hdel

hdel(name,*key)—刪除指定的鍵值對。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 設置值
r.hdel('hash1','name','name1')
print(r.hgetall('hsah1'))  # {}

11.hincrby

1)hincrby(name,key,amount=1),自增自減函數,將key對應的值自增或者減1。復數為減。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1','age':21})  # 設置值
r.hincrby('hash1','age',amount=1)
print(r.hgetall('hash1'))  # {'name1': 'wl1', 'age': '22', 'name': 'wl'}

2)hincrbyfloat(name,key,amount=1),自增自減函數,將key對應的值自增或者減,浮點數。復數為減。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1','age':21.0})  # 設置值
r.hincrbyfloat('hash1','age',amount=1.0)
print(r.hgetall('hash1'))  # {'name1': 'wl1', 'age': '22', 'name': 'wl'}

12.hscan(name,cursor=0,match=None,count=1)

取值查看,分片閱讀。這種讀取方式對於大數據量的讀取非常有用,分片的讀取數據可以有效的防治一次性讀入超量數據導致內存撐爆。

參數:

cursor:游標-基於游標獲取數據

match:匹配指定的key,None表示所有。

count:每次讀取的個數,None表示redis默認的讀取個數。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1','age':21.0})  # 設置值
r.hincrbyfloat('hash1','age',amount=1.0)
print(r.hscan('hash1',cursor=0,match=None,count=1))
# (0, {'name': 'wl', 'name1': 'wl1', 'age': '22'})

13.hscan_iter

hscan_iter(name,match=None,count=None)

使用yield封裝hash創建生成器,實現分批獲取數據。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1','age':21.0})  # 設置值
r.hincrbyfloat('hash1','age',amount=1.0)

for item in r.hscan_iter('hash1'):
    print(item)
# ('name1', 'wl1') ('age', '22') ('name', 'wl')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM