Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

Structure miss of an Item using updating an Item in DynamoDB using Python

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"

enter image description here

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:

enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this :)

for incidence in incidences:
    if incidence["M"]["type"]["S"] != "NO_RESPONSE_ON_TIME" and incidence["M"]["status"]["S"] == "RESOLVED":
        incidence["M"]["type"]["S"] = "RESOLVED"

Good Luck


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...