python - 使用 Python 更新 DynamoDB 中的项目的项目结构缺失
问题描述
我正在寻求帮助来解决我遇到的问题。
我正在使用 DynamoDB 和本文档来使用 Python 更新项目:
我想更新 2 个字段“状态”和“已解决”。
我原来的结构是这样的:incidents 是一个 List,里面是一个 Map,所以我只想更新 2 个值“status”和“resolved”
当我更新字段时,问题就来了,结构不一样,下一个是项目的结构 - 一个列表,在一个地图内,然后是一个地图:
我的代码是下一个:
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"
你们能帮帮我吗?
我的观点是代码中的问题:
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
谢谢!
解决方案
尝试这个 :)
for incidence in incidences:
if incidence["M"]["type"]["S"] != "NO_RESPONSE_ON_TIME" and incidence["M"]["status"]["S"] == "RESOLVED":
incidence["M"]["type"]["S"] = "RESOLVED"
祝你好运
推荐阅读
- direct3d12 - 是否可以更新 Direct3D 12 描述符范围或根签名?
- python - Selenium Python Scrape - 无法将抓取的数据循环到 Excel
- reactjs - 平面列表测量项目 X 位置
- c++ - 高效的函子调度器
- postgresql - 如何在 docker 容器中使用 pg_checksums?
- asp.net-mvc - 如何将整个 ASP.net mvc 站点转换为 https
- c# - 具有多态数组属性的 ASP.NET MVC 绑定传入对象
- r - 如何根据变量匹配检查条件
- java - 将 x.509 DER 与 Base 64 证书添加到 cacerts
- maven - 版本更新后 Swagger 代码生成错误 NoSuchMethodError:hasExplicitTimeZone()