[英]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.