Ale*_*exK 2 python redis redis-py
我正在尝试使用 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)
Run Code Online (Sandbox Code Playgroud)
抛出以下错误:
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.
Run Code Online (Sandbox Code Playgroud)
一个有效的例子:
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)
Run Code Online (Sandbox Code Playgroud)
总而言之,我的问题是:
是否可以将嵌套字典存储为Redis 中的哈希结构?
注意:我知道它可以存储为字符串,然后加载为 json,但我真的想避免它。
编辑:里德版本是 6.2.5
Redis 本身并不将嵌套结构存储在哈希中。关于如何在这里解决问题有一个很好的答案。
但是,有一个RedisJSON模块支持此功能,并且python redis 客户端包含对此模块的支持。
在redis服务器中加载模块:
redis-server --loadmodule ../RedisJSON/target/release/librejson.dylib
Run Code Online (Sandbox Code Playgroud)
在 python 中,您可以设置和检索嵌套字典或路径的一部分。
import redis
from redis.commands.json.path import Path
redis_connection = StrictRedis(host='localhost', port=6379, charset="utf-8", decode_responses=True)
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.json().set("test", Path.rootPath(), foo)
print(redis_connection.json().get("test", '.host_data.hostname'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4970 次 |
| 最近记录: |