如何使用Python在Redis中存储复杂的嵌套JSON

如何使用Python在Redis中存储复杂的嵌套JSON,python,json,redis,Python,Json,Redis,由于我是redis新手,我需要一些关于如何在redis中存储以下复杂json的指导,以便我们可以从redis访问json元素- "Reservations": [ { "Instances": [ { "Monitoring": { "State": "disabled" },

由于我是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中存在的所有元素


我需要一些关于上述内容的指导。

您不能直接这样做,但幸运的是,有一个名为RedisJSON的新Redis模块,它完全满足您的需要,并且它还有一个很好的Python绑定。您可以启动或使用Redis4.0+,然后/编译并安装RedisJSON,并配置Redis以加载它,它还添加了用于JSON操作的本机命令

它允许您在Redis中存储JSON文档,然后获取或修改文档树中的特定元素,而无需检索(甚至在内部解析)文档。它的Python客户端甚至允许您存储Python dict并自动将其转换为JSON

ReJSON模块:

Python客户端:

例如:

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'))
)

如果不想使用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 {}

从我口中说出这样一句话:)效果很好,不过我有一个问题,如果我想打印JSON的标记元素,我怎么做,我尝试了下面的方法,但它给了我一个错误-
print(r.jsonget('obj',Path('.Reservations.Instances.Tags'))
@rohisarkar它与ReJSON无关,我认为你的路径是错误的。通过查看文档,我可以看到它应该是
.Reservations[0]。Instances[0]。Tags
@Not_a_Golfer-谢谢你的工作,对不起,我对Python和redis有点不在行。它更像是一个JSON的东西,与Python和redis都无关。你不能在redis中修改pickle对象,如果你加载并序列化它,不仅速度慢,但是你的数据很快就会不一致。这里的目标是将数据存储在redis中,而不是动态修改它。我知道你可以使用reJson来实现这一点,只是给出了一个替代方案:)reJson还需要最低的redis要求4.0或更高。链接: