I am looking for help to solve a problem that I am stack on.
I am using DynamoDB and this documentation to Update an item using Python:
I want to update 2 fields "status" and "resolved".
The original structure that I have is this one: incidences is a List and inside that a Map, so I just want update the 2 values "status" and "resolved"
The problem come when I am updating the fields, the structure is not the same, the structure of the Item is the next - A List and inside a Map and again a Map:
My code is the next:
import json
import json
import boto3
import random
from boto3.dynamodb.conditions import Key
def handle_modify(record):
print("Handling MODIFY Event")
print(record)
incidences = record['dynamodb']['NewImage']['incidences']['L']
print(record)
dynamodb = boto3.resource('dynamodb', endpoint_url="https://dynamodb.eu-west-1.amazonaws.com")
incidences2 = []
for incidence in incidences:
if incidence["M"]["type"]["S"] != "NO_RESPONSE_ON_TIME" and incidence["M"]["status"]["S"] == "RESOLVED":
for incidence2 in incidences:
incidence2["M"]["status"]["S"] = "RESOLVED"
incidence2["M"]["resolved"]["BOOL"] = True
incidences2.append(incidence2)
break
if len(incidences2) > 0:
response = table.update_item(
Key={
'order_id': record['dynamodb']['NewImage']['order_id']['S']
},
UpdateExpression="set incidences=:incidences, last_status=:last_status, lastStatus=:lastStatus",
ExpressionAttributeValues={
':incidences': incidences2
},
ReturnValues="UPDATED_NEW"
)
def lambda_handler(event, context):
try:
for record in event['Records']:
if record['eventName'] == 'MODIFY':
handle_modify(record)
return "Success!"
except Exception as e:
print(e)
return "Error"
Can you guys help me please?
My opinion is that the problem in the code:
for incidence in incidences:
if incidence["M"]["type"]["S"] != "NO_RESPONSE_ON_TIME" and incidence["M"]["status"]["S"] == "RESOLVED":
for incidence2 in incidences:
incidence2["M"]["status"]["S"] = "RESOLVED"
incidence2["M"]["resolved"]["BOOL"] = True
incidences2.append(incidence2)
break
Thanks!
question from:
https://stackoverflow.com/questions/65843994/structure-miss-of-an-item-using-updating-an-item-in-dynamodb-using-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…