[英]how to update table item in dynamodb from sqs msg payload but not overwrite the table structure?
我有以下鏈:lambda->sns->sqs->ec2 腳本->dynamodb 表。
lambda 的測試事件
{
"body": {
"MessageAttributes": {
"vote": {
"Type": "String",
"StringValue": "b"
},
"voter": {
"Type": "String",
"StringValue": "count"
}
}
}
}
最初該表應該有 1 個分區和 2 個屬性 - a 和 b,使用以下 json 代碼創建:
{
"voter": {
"S": "count"
},
"a": {
"N": "11"
},
"b": {
"N": "20"
}
}
問題出在連續運行的 ec2 腳本中。 它有一個 function update_count(vote) 應該只是將現有的 a 或 b 增加 1。代碼:
def update_count(vote):
logging.info('update count....')
table.update_item(
Key={'voter': 'count'},
UpdateExpression="ADD #vote = #vote + :incr",
ExpressionAttributeNames={'#vote': vote},
ExpressionAttributeValues={':incr': 1}
)
當我發送我的測試事件時,它會覆蓋我的 dynamodb 表並且它具有以下結構:
UpdateItem
永遠不會覆蓋未在UpdateExpression
中設置的屬性。
我會確保您沒有其他代碼無意中覆蓋您的項目,可能是從 Lambda function 監聽 stream,或者另一個 function 在您的應用程序中被不知不覺地調用。
您可以在 Cloudtrail 上啟用 DynamoDB Dataplane 日志以了解發生了什么:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/logging-using-cloudtrail.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.