Python3中redis模块学习之hash数据类型操作
目录起步hsethsetnxhmsethgethmgethgetallhkeyshvalshscan_iterhlenhstrlenhincrbyhincrbyfloathdelhexists整体代码起步#!/usr/bin/python3# -*- coding: utf-8 -*-"""hash类型操作"""import redispool = redis.Connection...
·
目录
起步
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""hash类型操作"""
import redis
pool = redis.ConnectionPool(host='192.168.1.104', password='hardy9sap', decode_responses=True)
rs = redis.Redis(connection_pool=pool)
is_connected = rs.ping()
hset
hset(name, key, value),在指定的name中设置key-value(相当于Python中name = {key: value}
并且返回1;
如果是覆写返回0;
示例
rsp = rs.hset(name='redis:method:hset', key='name', value='hardy9sap')
print(rsp) # 1
# 0
hsetnx
hsetnx(name, key, value),如果name or key不存在,就执行此操作,并且返回1;
如果key存在,则返回0
示例
rsp = rs.hsetnx(name='redis:method:hset', key='name', value='hardy9sap')
print(rsp) # 0
rsp = rs.hsetnx(name='redis:method:hsetnx', key='name', value='hardy9sap')
print(rsp) # 1
hmset
hmset(name, mapping),一次性在name中设置多个key-value键值对,永远返回True
示例
rsp = rs.hmset(name='redis:method:hmset', mapping={
'name': 'hardy9sap',
'age': 21,
'height': '175',
'weight': '52kg'
})
print(rsp)
hget
hget(name, key),在指定的name中获取key的value;不存在,返回None
示例
rsp = rs.hget(name='redis:method:hmset', key='name')
print(rsp) # hardy9sap
hmget
hmget(name, keys, *args),返回列表,一次性在name中获取多个key的value
示例
rsp = rs.hmget(name='redis:method:hmset', keys=('name', 'age', 'height', 'weight'))
print(rsp) # ['hardy9sap', '21', '175', '52kg']
rsp = rs.hmget('redis:method:hmset', 'name', 'age', 'height', 'weight')
print(rsp) # ['hardy9sap', '21', '175', '52kg']
rsp = rs.hmget('no:exists:hash', 'name')
print(rsp) # [None]
hgetall
hgetall(name),返回字典,获取name中所有的key和value键值对
如果name不存在,返回{}
示例
rsp = rs.hgetall(name='redis:method:hmset')
print(rsp) # {'name': 'hardy9sap', 'age': '21', 'height': '175', 'weight': '52kg'}
rsp = rs.hgetall(name='no:exists:hash')
print(rsp) # {}
hkeys
hkeys(name),返回列表,获取name中所有的key
示例
rsp = rs.hkeys(name='redis:method:hmset')
print(rsp) # ['name', 'age', 'height', 'weight']
rsp = rs.hkeys(name='no:exists:hash')
print(rsp) # []
hvals
hvals(name),获取name中所有的value
示例
rsp = rs.hvals(name='redis:method:hmset')
print(rsp) # ['hardy9sap', '21', '175', '52kg']
rsp = rs.hvals(name='no:exists:hash')
print(rsp) # []
hscan_iter
hscan_iter(name,match=None,count=None):利用yield封装hscan创建生成器,实现分批获取数据
示例
# rs.hmset('hashmore', {'k1': '1', 'k2': '2', 'k3': '3', 'k4': '4', 'k5': '5', 'k6': '6', 'k7': '7', 'k8': '8'})
rsp = rs.hscan_iter(name='hashmore')
for s in rsp:
# ==================================
# ('k1', '1')
# ('k2', '2')
# ('k3', '3')
# ('k4', '4')
# ('k5', '5')
# ('k6', '6')
# ('k7', '7')
# ('k8', '8')
# ===================================
print(s)
hlen
hlen(name),int,统计name中有多少个键值对
示例
rsp = rs.hlen(name='redis:method:hmset')
print(rsp) # 4
hstrlen
hstrlen(name, key),int,返回哈希表中给定key相关联的值的字符串字节长度
示例
rsp = rs.hstrlen(name='redis:method:hmset', key='name')
print(rsp) # 9
hincrby
hincrby(name, key, amount=1),让name中key的value自增amount指定的整型值(value必须为整型,可以为负值),并且返回
如果哈希表不存在,则一个新的哈希表被创建并执行 hincrby 命令
如果指定的键(key)不存在
那么在执行命令前,字段的值先被初始化为0
示例
rsp = rs.hincrby(name='redis:method:hmset', key='height', amount=1)
print(rsp, type(rsp)) # 177 <class 'int'>
hincrbyfloat
hincrbyfloat(name, key, amount=1.0),让name中key的value自增amount指定的数值(整型|浮点型),并且返回
如果哈希表不存在,则一个新的哈希表被创建并执行 hincrby 命令
如果指定的键(key)不存在
那么在执行命令前,字段的值先被初始化为0
示例
rsp = rs.hincrbyfloat(name='redis:method:hmset', key='height', amount=1.5)
print(rsp, type(rsp)) # 178.5 <class 'float'>
hdel
hdel(name, *keys),删除name中的key键值对,返回成功删除的个数。
(如果把key中所有的键值对删除了,那么key也就被回收了)
示例
rsp = rs.hdel('redis:method:hmset', *('name', 'weight', 'height', 'age', 'no:exists:key'))
print(rsp) # 4
hexists
hexists(name, key),检查name中是否存在key,
name or key不存在返回False
示例
rsp = rs.hexists(name='redis:method:hmset', key='name')
print(rsp) # False
整体代码
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""hash类型操作"""
import redis
pool = redis.ConnectionPool(host='192.168.1.104', password='hardy9sap', decode_responses=True)
rs = redis.Redis(connection_pool=pool)
is_connected = rs.ping()
# 1. hset
# ================================================================================
# hset(name, key, value),在指定的name中设置key-value(相当于Python中name = {key: value}
# 并且返回1;
# 如果是覆写返回0;
# ================================================================================
# if is_connected:
# rsp = rs.hset(name='redis:method:hset', key='name', value='hardy9sap')
# print(rsp) # 1
# # 0
# 2. hsetnx
# =========================================================
# hsetnx(name, key, value),如果name不存在,就执行此操作,并且返回1;
# 如果name存在,则返回0
# ==========================================================
# if is_connected:
# rsp = rs.hsetnx(name='redis:method:hset', key='name', value='hardy9sap')
# print(rsp) # 0
#
# rsp = rs.hsetnx(name='redis:method:hsetnx', key='name', value='hardy9sap')
# print(rsp) # 1
# 3. hmset
# =====================================================================
# hmset(name, mapping),一次性在name中设置多个key-value键值对,永远返回True
# =====================================================================
# if is_connected:
# rsp = rs.hmset(name='redis:method:hmset', mapping={
# 'name': 'hardy9sap',
# 'age': 21,
# 'height': '175',
# 'weight': '52kg'
# })
# print(rsp)
# 4. hget
# ===================================================================
# hget(name, key),在指定的name中获取key的value;不存在,返回None
# ===================================================================
# if is_connected:
# rsp = rs.hget(name='redis:method:hmset', key='name')
# print(rsp) # hardy9sap
# 5. hmget
# =================================================================
# hmget(name, keys, *args),返回列表,一次性在name中获取多个key的value
# =================================================================
# if is_connected:
# rsp = rs.hmget(name='redis:method:hmset', keys=('name', 'age', 'height', 'weight'))
# print(rsp) # ['hardy9sap', '21', '175', '52kg']
#
# rsp = rs.hmget('redis:method:hmset', 'name', 'age', 'height', 'weight')
# print(rsp) # ['hardy9sap', '21', '175', '52kg']
#
# rsp = rs.hmget('no:exists:hash', 'name')
# print(rsp) # [None]
# 6. hgetall
# =====================================================
# hgetall(name),返回字典,获取name中所有的key和value键值对
# 如果name不存在,返回{}
# =====================================================
# if is_connected:
# rsp = rs.hgetall(name='redis:method:hmset')
# print(rsp) # {'name': 'hardy9sap', 'age': '21', 'height': '175', 'weight': '52kg'}
#
# rsp = rs.hgetall(name='no:exists:hash')
# print(rsp) # {}
# 7. hkeys
# =================================
# hkeys(name),返回列表,获取name中所有的key
# =================================
# if is_connected:
# rsp = rs.hkeys(name='redis:method:hmset')
# print(rsp) # ['name', 'age', 'height', 'weight']
#
# rsp = rs.hkeys(name='no:exists:hash')
# print(rsp) # []
# 8. hvals
# =================================
# hvals(name),获取name中所有的value
# =================================
# if is_connected:
# rsp = rs.hvals(name='redis:method:hmset')
# print(rsp) # ['hardy9sap', '21', '175', '52kg']
#
# rsp = rs.hvals(name='no:exists:hash')
# print(rsp) # []
# 9. hscan_iter
# =====================================
# hscan_iter(name,match=None,count=None):利用yield封装hscan创建生成器,实现分批获取数据
# =====================================
if is_connected:
# rs.hmset('hashmore', {'k1': '1', 'k2': '2', 'k3': '3', 'k4': '4', 'k5': '5', 'k6': '6', 'k7': '7', 'k8': '8'})
rsp = rs.hscan_iter(name='hashmore')
for s in rsp:
# ==================================
# ('k1', '1')
# ('k2', '2')
# ('k3', '3')
# ('k4', '4')
# ('k5', '5')
# ('k6', '6')
# ('k7', '7')
# ('k8', '8')
# ===================================
print(s)
# 9. hlen
# ========================================
# hlen(name),int,统计name中有多少个键值对
# ========================================
# if is_connected:
# rsp = rs.hlen(name='redis:method:hmset')
# print(rsp) # 4
# 10. hstrlen
# ==================================================================
# hstrlen(name, key),int,返回哈希表中给定key相关联的值的字符串字节长度
# ==================================================================
# if is_connected:
# rsp = rs.hstrlen(name='redis:method:hmset', key='name')
# print(rsp) # 9
# 11. hincrby
# ==========================================================
# hincrby(name, key, amount=1),让name中key的value自增amount指定的整型值(value必须为整型,可以为负值),并且返回
# 如果哈希表不存在,则一个新的哈希表被创建并执行 hincrby 命令
# 如果指定的键(key)不存在
# 那么在执行命令前,字段的值先被初始化为0
# ==========================================================
# if is_connected:
# rsp = rs.hincrby(name='redis:method:hmset', key='height', amount=1)
# print(rsp, type(rsp)) # 177 <class 'int'>
# 11. hincrbyfloat
# ==========================================================
# hincrbyfloat(name, key, amount=1.0),让name中key的value自增amount指定的数值(整型|浮点型),并且返回
# 如果哈希表不存在,则一个新的哈希表被创建并执行 hincrby 命令
# 如果指定的键(key)不存在
# 那么在执行命令前,字段的值先被初始化为0
# ==========================================================
# if is_connected:
# rsp = rs.hincrbyfloat(name='redis:method:hmset', key='height', amount=1.5)
# print(rsp, type(rsp)) # 178.5 <class 'float'>
# 12. hdel
# ==========================================================
# hdel(name, *keys),删除name中的key键值对,返回成功删除的个数。
# (如果把key中所有的键值对删除了,那么key也就被回收了)
# ==========================================================
# if is_connected:
# rsp = rs.hdel('redis:method:hmset', *('name', 'weight', 'height', 'age', 'no:exists:key'))
# print(rsp) # 4
# 13. hexists
# ============================================
# hexists(name, key),检查name中是否存在key,
# name or key不存在返回False
# ============================================
# if is_connected:
# rsp = rs.hexists(name='redis:method:hmset', key='name')
# print(rsp) # False
rs.close()
更多推荐
已为社区贡献2条内容
所有评论(0)