[英]Update JSON Records in DynamoDB table using Python
我有一個 JSON 記錄,如下所示:
{'ip_address': data['src_ip'],
'first_seen': data['timestamp'],
'last_seen': data['timestamp']
}
記錄以ip_address
作為主鍵存儲在 DynamoDB 表中。
當同樣的 IP 再次出現時,我想更新最后看到的值。 我用以下幾行創建了一個 function:
def update_db(json_result):
ddb_data = json.loads(json.dumps(json_result), parse_float=Decimal)
database = boto3.resource('dynamodb', endpoint_url="https://dynamodb.us-west-2.amazonaws.com")
table = database.Table('IPData')
print("adding :", json_result)
#db_add = table.put_item(Item = ddb_data)
response = table.update_item(
Key = {
'ip_address': json_result['ip_address']
},
UpdateExpression="SET 'last_seen' = :s",
ExpressionAttributeValues = {
':s': json_result['last_seen']
},
ReturnValues="UPDATED_NEW"
)
return response
這會導致錯誤:
{
"errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Syntax error; token: \"'\", near: \"SET 'last_seen\"",
"errorType": "ClientError",
不確定我的語法有什么問題。 有人可以幫我解決這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.