前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >4.Python操作Redis:哈希(H

4.Python操作Redis:哈希(H

作者头像
py3study
发布2020-01-03 16:19:53
4.2K0
发布2020-01-03 16:19:53
举报
文章被收录于专栏:python3python3

Redis 数据库hash数据类型是一个string类型的key和value的映射表,适用于存储对象。Redis 中每个 hash 可以存储 232 - 1 键值对(40多亿)。 Python的redis模块实现了Redis哈希(Hash)命令行操作的几乎全部命令,包括HDEL、HEXISTS、HGET、HGETALL、HINCRBY、HKEYS、HLEN 、HMGET 、HMSET 、HSET 、HSETNX 、HVALS 。但是无法支持HINCRBYFLOAT 、HSCAN 等命令。

函数说明

  1. HDEL: 删除对应哈希(Hash)表的指定键(key)的字段,hdel(self, name, key)
  2. HEXISTS: 检测哈希(Hash)表对应键(key)字段是否存在,返回布尔逻辑,hexists(self, name, key)
  3. HGET: 获取哈希(Hash)指定键(key)对应的值,hget(self, name, key)
  4. HGETALL: 获取哈希(Hash)表的键-值对(key-value pairs),返回python字典类型数据,hgetall(self, name)
  5. HINCRBY: 为哈希表(Hash)指定键(key)对应的值(key)加上指定的整数数值(int,可为负值),参见 [Python操作Redis:字符串(String)],(http://blog.csdn.net/u012894975/article/details/51285733)hincrby(self, name, key, amount=1),Redis 中本操作的值被限制在 64 位(bit)有符号数字。
  6. HKEYS: 返回哈希表(Hash)对应键(key)的数组(Python称之为列表List),hkeys(self, name)
  7. HLEN: 获取哈希表(Hash)中键-值对(key-value pairs)个数,hlen(self, name)
  8. HMGET: 获取哈希表(Hash)中一个或多个给点字段的值,不存在返回nil(Redis命令行)/None(Python),hmget(self, name, keys),其中keys可以为列表(list)
  9. HMSET: 设置对个键-值对(key-value pairs)到哈希表(Hash)中,python输入值(mapping)为字典(dictionary)类型,hmset(self, name, mapping)
  10. HSET: 为哈希表(Hash)赋值,若键(key)存在值(value)则覆盖,不存在则创建,hset(self, name, key, value)
  11. HSETNX:为哈希表(Hash)不存值(value)的键(key)赋值,存在操作无效,对应值(value)无变化,hsetnx(self, name, key, value)
  12. HVALS:返回哈希表(Hash)对应值(value)的列表,hvals(self, name)

代码示例

代码语言:javascript
复制
#!/usr/bin/python
import redis
import time
## Connect local redis service
client =redis.Redis(host='127.0.0.1',port=6379,db=0)
print "Connection to server successfully!"
dicKeys = client.keys("*")
print dicKeys

### Redis hash command part Start ###
# hset: Set key to value with hash name,hset(self, name, key, value)
# hget: Return the value of ``key`` within the hash ``name``, hget(self, name, key)
client.hset('myhash','field1',"foo")
hashVal = client.hget('myhash','field1')
print "Get hash value:",hashVal

# Get none-value
hashVal = client.hget('myhash','field2')
print "None hash value:",hashVal

# hexists: Returns a boolean indicating if ``key`` exists within hash ``name``
keyList= ['field1','field2']
for key in keyList:
    hexists = client.hexists('myhash',key)
    if hexists :
        print "Exist in redis-hash key:",key
    else:
        print "Not exist in redis-hash key:",key

# hgetall: Return a Python dict of the hash's name/value pairs
client.hset('myhash','field2',"bar")
valDict = client.hgetall('myhash')
print "Get python-dict from redis-hash",valDict

# hincrby: Increment the value of ``key`` in hash ``name`` by ``amount``
# default increment is 1,
client.hset('myhash','field',20)
client.hincrby('myhash','field')
print "Get incrby value(Default):",client.hget('myhash','field')
client.hincrby('myhash','field',2)
print "Get incrby value(step: 2):",client.hget('myhash','field')
client.hincrby('myhash','field',-3)
print "Get incrby value(step: -3):",client.hget('myhash','field')

# no method hincrbyfloat

#hkeys: Return the list of keys within hash ``name``
kL = client.hkeys('myhash')
print "Get redis-hash key list",kL

#hlen: Return the number of elements in hash ``name``
lenHash =client.hlen('myhash')
print "All hash length:",lenHash

#hmget: Returns a list of values ordered identically to ``keys``
#hmget(self, name, keys), keys should be python list data structure
val =client.hmget('myhash',['field','field1','field2','field3','fieldx'])
print "Get all redis-hash value list:",val

#hmset:  Sets each key in the ``mapping`` dict to its corresponding value in the hash ``name``
hmDict={'field':'foo','field1':'bar'}
hmKeys=hmDict.keys()
client.hmset('hash',hmDict)
val = client.hmget('hash',hmKeys)
print "Get hmset value:",val

#hdel: Delete ``key`` from hash ``name``
client.hdel('hash','field')
print "Get delete result:",client.hget('hash','field')

#hvals:  Return the list of values within hash ``name``
val = client.hvals('myhash')
print "Get redis-hash values with HVALS",val

#hsetnx: Set ``key`` to ``value`` within hash ``name`` if ``key`` does not exist.
#      Returns 1 if HSETNX created a field, otherwise 0.
r=client.hsetnx('myhash','field',2)
print "Check hsetnx execute result:",r," Value:",client.hget('myhash','field')
r=client.hsetnx('myhash','field10',20)
print "Check hsetnx execute result:",r,"Value",client.hget('myhash','field10')

hashVal = client.hgetall('profile')
print hashVal
#Empty db
client.flushdb()

参考资料: 1、Redis 哈希(Hash) 2、Python redis文档(python交互模式下命令>>>help redis)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 函数说明
  • 代码示例
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档