我正在尝试对 DynamoDB 表进行简单的操作。架构非常简单
(哈希键)SummaryId:字符串(排序键)状态:字符串
def put_item(dynamo_table, summary_id, status):
return dynamo_table.put_item(
Item={
'SummaryId': summary_id,
'Status': status
},
ReturnValues="ALL_OLD"
)
def update_item(dynamo_table, summary_id, status):
response = dynamo_table.update_item(
Key={'SummaryId': summary_id},
AttributeUpdates={
'Status': status,
},
ReturnValues="UPDATED_OLD"
)
return response
def initialize_dynamodb_table():
dynamodb = boto3.Session(profile_name=PROFILE_NAME,
region_name=REGION_NAME) \
.resource('dynamodb')
return dynamodb.Table(TABLE_NAME)
def main():
dynamodb_table = initialize_dynamodb_table()
# Update the above item
response = put_item(dynamodb_table, "Id1::Id2::Id4", "IN_PROGRESS")
pprint(response)
response = update_item(dynamodb_table, "Id1::Id2::Id4", "COMPLETE")
pprint(response)
if __name__ == '__main__':
main()
PK“Id1::Id2::Id4”的项目不存在。因此 put_item() 预计会添加此项目。我使用 update_item() api 的目的是它将项目状态从“IN_PROGRESS”更改为“COMPLETE”。相反,update_item() API 在表中创建一个新项目,其中包含 PK“Id1::Id2::Id4”和 RK“COMPLETE”
我如何实现预期的行为?提前致谢!
胡子哥哥
相关分类