【发布时间】:2022-01-09 04:47:51
【问题描述】:
我正在尝试使用 HSET 将嵌套字典存储为哈希,但这似乎是不可能的。
不起作用的示例:
from redis import StrictRedis
foo = {
"host_data": {
"hostname": "some_host",
"mac_address": "82:fa:8e:63:40:05",
"root_password": {
"is_crypted": True,
"password": "sha512_password"
},
"network": {
"ip_address": "192.168.0.10/24",
"default_gateway": "192.168.0.1",
"vmnic": "vmnic3",
"vlan": 20
},
"dns_servers": [
"dns01.local",
"dns02.local"
]
},
"installation_type": "esxi",
"image_data": {
"type": "vCenter_contentlib",
"host": "vcenter01",
"credential": {
"username": "some_user",
"password": "some_password"
},
"content_library": "the_content_lib_name",
"image_name": "some_image"
},
"host_short_name": "esxi021"
}
redis_connection = StrictRedis(host='localhost', port=6379, charset="utf-8", decode_responses=True)
redis_connection.hset("test", mapping=foo)
抛出以下错误:
Traceback (most recent call last):
File "/Users/project/dummy.py", line 36, in <module>
redis_connection.hset("test", mapping=thing)
File "/Users/project/venv/lib/python3.9/site-packages/redis/client.py", line 3050, in hset
return self.execute_command('HSET', name, *items)
File "/Users/project/venv/lib/python3.9/site-packages/redis/client.py", line 900, in execute_command
conn.send_command(*args)
File "/Users/project/venv/lib/python3.9/site-packages/redis/connection.py", line 725, in send_command
self.send_packed_command(self.pack_command(*args),
File "/Users/project/venv/lib/python3.9/site-packages/redis/connection.py", line 775, in pack_command
for arg in imap(self.encoder.encode, args):
File "/Users/project/venv/lib/python3.9/site-packages/redis/connection.py", line 119, in encode
raise DataError("Invalid input of type: '%s'. Convert to a "
redis.exceptions.DataError: Invalid input of type: 'dict'. Convert to a bytes, string, int or float first.
有效的示例:
from redis import StrictRedis
foo = {
"installation_type": "esxi",
"host_short_name": "esxi021"
}
redis_connection = StrictRedis(host='localhost', port=6379, charset="utf-8", decode_responses=True)
redis_connection.hset("test", mapping=foo)
总之,我的问题是:
是否可以将嵌套的 dict 存储为 Redis 中的哈希结构?
注意:我知道它可以存储为字符串,然后加载为 json,但我真的想避免它。
编辑: Reids 版本是 6.2.5
【问题讨论】:
-
这能回答你的问题吗? Alternatives to Nested Structures in Redis?