简体   繁体   English

如何使用python解析DynamoDB发送给Lambda的数据结构?

[英]How do I parse the data structure sent by DynamoDB to Lambda using python?

I am trying to link DynamoDB with Amazon Lambda such that when an item is inserted in DynamoDB, it will be sent to my lambda function which will parse the data structure and perfomr operations on the data. 我正在尝试将DynamoDB与Amazon Lambda链接,以便在DynamoDB中插入一个项目时将其发送到我的lambda函数,该函数将解析数据结构和对数据的perfomr操作。 However, the data posted looks like this : 但是,发布的数据如下所示:

{
      "Records": [
        {
          "eventID": "ebefd45a0610c7a54654nb09b491c7c",
          "eventVersion": "1.1",
          "dynamodb": {
            "SequenceNumber": "3300000555554005534567541",
            "Keys": {
              "Id": {
                "S": "13"
              }
            },
            "SizeBytes": 100,
            "NewImage": {
              "url": {
                "S": "http:\/\/test.com\/blog\/tips-on-choosing-a-suitable-company-2\/"
              },
              "db_ref_id": {
                "S": "123"
              },
              "Id": {
                "S": "13"
              }
            },
            "ApproximateCreationDateTime": 1472638500,
            "StreamViewType": "NEW_AND_OLD_IMAGES"
          },
          "awsRegion": "us-west-2",
          "eventName": "INSERT",
          "eventSourceARN": "arn:aws:dynamodb:us-west-2:3454354:table\/urls\/stream\/2016-08-31T07:21:11.412",
          "eventSource": "aws:dynamodb"
        }
      ]
    }

I found this piece of code online that does this : 我在网上发现了这段代码:

for record in event['Records']:
        print(record['eventID'])

However, I modified my code to this, and it doesn't work : 但是,我对此进行了修改,但是它不起作用:

for record in event['Records']:
        print(record['dynamodb']['NewImage']['url']['S'])

Since I am not much aware of python, I'm not sure what is the best way of parsing this data structure. 由于我不太了解python,因此我不确定解析此数据结构的最佳方法是什么。 Any suggestions would really help. 任何建议都会有帮助。

print string['Records'][0]['eventID'] to print the eventID print string['Records'][0]['eventID']以打印eventID

There are many data structures involved here. 这里涉及许多数据结构。 The outer one being a python dictionary whose objects you can receive using keys. 外部是一个python字典,您可以使用键接收其对象。 In our case you have only one mapping in your dictionary so, assuming that the whole data structure you have up there is assigned in variable named event you could do event['Records']. 在我们的例子中,您的词典中只有一个映射,因此,假设您在那里的整个数据结构都分配了名为event的变量,则可以执行event ['Records']。 Later on, you have a list [] whose elements you can only fetch using indexes. 稍后,您将有一个列表[] ,只能使用索引来获取其元素。 (ex. list[1], list[2] etc) (例如list [1],list [2]等)

Since from the format you have I suppose that you might have many records you could: 因为从您的格式来看,我想您可能有很多记录,您可以:

for record in event['Records']: print(record['dynamodb']['NewImage']['url']['S'])

This should print what you want, if the data posted on above is a string the parsing needs to take place using the json python module first. 如果上面发布的数据是字符串,则需要先使用json python模块进行解析,这应该会打印出您想要的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 解析与 INI 结构相似的数据文件? - How do i parse data file with similar structure to INI using python? 如何使用regex / python将顺序组织的多行字符串解析为数据结构? - How do I parse a sequentially organized multiline string into a data structure using regex/python? 使用 Python 和 AWS Lambda 将具有嵌套结构的 pandas dataframe 写入 DynamoDB - writing pandas dataframe with Nested structure to DynamoDB using Python and AWS Lambda 如何在Python中对这种数据结构进行排序? - How do I sort this data structure in Python? 如何使用 Boto3 和 Python 将 lambda 函数订阅到 DynamoDB 流? - How do I use Boto3 and Python to subscribe a lambda function to a DynamoDB stream? 如何使用 append 将列表添加到我的 DynamoDB 表中 Python? - How do I append a list to my DynamoDB table using Python? 我如何在 Lambda python function 中将 dynamodb json 转换为 json - How can i convert dynamodb json to json in a Lambda python function 如何使用 aws Lambda 和 python 将项目放入 aws DynamoDb - how to put an Item in aws DynamoDb using aws Lambda with python 如何使用 Lambda 中的 python 从 dynamoDB 获取/获取某些列? - How to get/fetch certain columns from dynamoDB using python in Lambda? 如何终止java套接字发送给python的数据? - how do I terminate data sent by java socket to python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM