由于我是redis的新手,我需要一些关于如何在REDIS中存储以下复杂json的指导,以便我们可以从REDIS访问JSON的元素 -
"Reservations": [
{
"Instances": [
{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "",
"State": {
"Code": 16,
"Name": "running"
},
"EbsOptimized": "false",
"LaunchTime": "xxxxxxxxx",
"PrivateIpAddress": "x.x.x.x",
"ProductCodes": [],
"VpcId": "xxxxx",
"StateTransitionReason": "",
"InstanceId": "i-xxxxxxx",
"EnaSupport": "true",
"ImageId": "ami-xxxxx",
"PrivateDnsName": "ip-xxxxxx.ec2.internal",
"KeyName": "xxxxxxv",
"SecurityGroups": [
{
"GroupName": "xxx",
"GroupId": "sg-xxxx"
},
{
"GroupName": "xxxxxx",
"GroupId": "sg-xxxxx"
},
{
"GroupName": "xxxxx",
"GroupId": "sg-xxxxxx"
},
{
"GroupName": "xxxxx",
"GroupId": "sg-xxxxxx"
}
],
"ClientToken": "xxxxx",
"SubnetId": "subnet-xxxxx",
"InstanceType": "t2.micro",
"NetworkInterfaces": [
{
"Status": "in-use",
"MacAddress": "xxxxxxxx",
"SourceDestCheck": "true",
"VpcId": "vpc-xxxxx",
"Description": "",
"NetworkInterfaceId": "eni-xxxxx",
"PrivateIpAddresses": [
{
"PrivateDnsName": "ip-xx-ec2.internal",
"Primary": "true",
"PrivateIpAddress": "xxxxx"
}
],
"PrivateDnsName": "ip-xxxx-xx.ec2.internal",
"Attachment": {
"Status": "attached",
"DeviceIndex": 0,
"DeleteOnTermination": "true",
"AttachmentId": "eni-attach-xxxxx",
"AttachTime": "2017-0xxxxx"
},
"Groups": [
{
"GroupName": "xx",
"GroupId": "sg-xxxx"
},
{
"GroupName": "xxxx",
"GroupId": "sg-xxx"
},
{
"GroupName": "xxxx",
"GroupId": "sg-xxx"
},
{
"GroupName": "xxxx",
"GroupId": "sg-xxxx"
}
],
"Ipv6Addresses": [],
"OwnerId": "xxx",
"SubnetId": "subnet-xxxx",
"PrivateIpAddress": "1xxxx"
}
],
"SourceDestCheck": "true",
"Placement": {
"Tenancy": "default",
"GroupName": "",
"AvailabilityZone": "us-xxxxxxx"
},
"Hypervisor": "xen",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/xxxxxx",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": "true",
"VolumeId": "vol-xxxxxx",
"AttachTime": "2017-xxxxxxx"
}
}
],
"Architecture": "x86_64",
"RootDeviceType": "ebs",
"IamInstanceProfile": {
"Id": "xxxxxxxx",
"Arn": "arn:aws:iam::xxxxxxx"
},
"RootDeviceName": "/dev/xxxxx",
"VirtualizationType": "hvm",
"Tags": [
{
"Value": "xxxxxx",
"Key": "aws:cloudformation:stack-name"
},
{
"Value": "xxxxxxx",
"Key": "aws:cloudformation:logical-id"
},
{
"Value": "arn:aws:cloudformation:xxxxxx",
"Key": "aws:cloudformation:stack-id"
}
],
"AmiLaunchIndex": 0
}
],
"ReservationId": "r-xxxxx",
"RequesterId": "xxxxx",
"Groups": [],
"OwnerId": "xxxxxx"
}
]
}
我需要以这样的方式存储它,即我查询IP / Hostname / InstanceID以获取JSON中存在的所有元素。
我需要一些指导。
答案 0 :(得分:6)
你不能直接这样做,但幸运的是有一个名为ReJSON的新Redis模块可以完全满足您的需求,而且它还有一个很好的Python绑定。您需要使用redis 4.0,然后编译并安装ReJSON并配置redis来加载它,并为JSON操作添加本机命令。
它允许您将JSON文档存储在redis中,然后获取或修改文档树中的特定元素,而无需检索(或内部解析)文档。它的Python客户端甚至可以让你存储python dicts并自动将它们转换为JSON。
ReJSON模块:http://rejson.io
Python客户端:https://pypi.python.org/pypi/rejson
示例:
from rejson import Client, Path
rj = Client(host='localhost', port=6379)
# Set the key `obj` to some object
obj = {
'answer': 42,
'arr': [None, True, 3.14],
'truth': {
'coord': 'out there'
}
}
rj.jsonset('obj', Path.rootPath(), obj)
# Get something
print 'Is there anybody... {}?'.format(
rj.jsonget('obj', Path('.truth.coord'))
)
答案 1 :(得分:1)
如果您不想使用reJson,可以使用 pickle 模块。
以redis格式设置数据:
def store_dict_data_in_redis(redis_client, key, data, ex=0):
'''
store dict data in redis by pickle dumps
:param redis_client: redis client used to connect to obtain key value
:param key: key name
:param data: dict value
:param ex: expiry
:return: None
'''
if ex > 0:
redis_client.set(key, pickle.dumps(data), ex=ex)
else:
redis_client.set(key, pickle.dumps(data))
从redis获得价值:
def get_dict_data_from_redis(redis_client, key):
'''
obtain dict data from redis
:param redis_client: redis client used to connect to obtain key value
:param key: key name
:return: dict data stored in redis
'''
data = redis_client.get(key)
if data:
try:
return pickle.loads(data)
except:
return eval(data.decode())
return {}