操作
import redis
conn = redis.Redis(decode_responses=True)
1.hset(name,key,value)
往redis的k中添加单条数据
conn.hset('user', 'name', '猫猫')
conn.hset('user', 'hobby', '猫')
2.hmset(name,key,value)弃用
conn.hmset('user',{'hobby':'猫'})
hset(name,key,value)
往redis的k中批量添加数据
conn.hset('maomao', mapping={'name': '秋秋', 'age': '2'})
3.hget(name,key)
通过redis的Key键取里面Key键的值(单条)
res = conn.hget('maomao', 'age')
print(res)
4.hget(name,key,*args)
通过redis的Key键取里面Key键的值(多条)
res = conn.hmget('maomao', ['name', 'age']);{'name': '秋秋', 'age': '2'}
print(res)
5.hgetall(name)[慎用]
通过redis的Key键取里面Key键和值,以字典的形式返回
res = conn.hgetall('maomao')
print(res)
6.hlen(name)
通过redis的Key键,求K键的长度
res = conn.hlen('maomao')
print(res)
7.hkeys(name)
通过redis的Key键取里面Key键,列表的形式返回
res = conn.hkeys('maomao')
print(res)
8.hvals(name)
通过redis的Key键取里面所有的value
res = conn.hvals('maomao')
print(res)
9.hexists(name,key)
通过redis的Key键,判断有没有key,存在就是True,不存在就是False
res = conn.hexists('maomao', 'age')
print(res)
10.hdel(name,*keys)
通过redis的Key键,删除里面的Key,可以删除多个
conn.hdel('maomao', 'age')
conn.hdel('maomao','age','name')
11.hincrby(name,key,amount=1)
通过redis的Key键,让里面key对应的值自增
conn.hincrby('maomao', 'age')
12.hincrbyfloat(name,key,amount=1.0)
带小数的自增
13.hash的字典是无序的
造点数据
for i in range(100):
conn.hset('maomao_num','mao_%s'%i,'猫%s号'%i)
分批获取数据,每次拿20个
res = conn.hscan('maomao_num', cursor=0, count=20)
print(res)
数据量会有差异
hscan_iter()
一次取10个再取10,直到取完为止
res = conn.hscan_iter('maomao_num',count=20)
for i in res:
print(i)
conn.close()