使用 Boto3,如果对象上存在某些属性,则获取 put_item 以替换 DynamoDB 中的对象?
Using Boto3, get put_item to replace an object in DynamoDB if certain attributes exist on the object?
如果某些属性(其中一些是 DynamoDB table 的主键,而另一些不是),我在使用 Boto3 put_item
替换 DynamoDB 中的对象时遇到问题与我插入到 table.
中的新对象匹配
例如,如果 DynamoDB table 中包含:
user (Partition Key) | ts (Sort Key) | height | weight
__________________________________________________________________________________
'fred' 01-01-2017 5'10'' 190
'george' 01-02-2017 5'08'' 200
并且我正在尝试为此添加一个新行 table:
'fred' 01-01-2018 5'10'' 200
基于用户 fred
和身高 5'10''
匹配 table 中已有的另一个用户,我想用新条目替换旧条目。 boto3 和 AWS 的文档有点不清楚 put_item -- 我该怎么做?
作为参考,这是我目前拥有的:
tracking_result = tracking_dbs[drivetype].put_item(
Item=track,
# insert fields to remove old entry that matches
# up with certain attributes from new entry here
)
只有当新条目的分区键和排序键与现有条目的分区键和排序键匹配时,DynamoDB 中已经存在的条目才会被新条目替换。否则将创建一个新条目。
在您的情况下,您必须删除现有条目(通过扫描而不是查询获取现有条目,因为查询需要分区和排序键作为输入参数,然后执行删除操作),然后创建新条目.
如果某些属性(其中一些是 DynamoDB table 的主键,而另一些不是),我在使用 Boto3 put_item
替换 DynamoDB 中的对象时遇到问题与我插入到 table.
例如,如果 DynamoDB table 中包含:
user (Partition Key) | ts (Sort Key) | height | weight
__________________________________________________________________________________
'fred' 01-01-2017 5'10'' 190
'george' 01-02-2017 5'08'' 200
并且我正在尝试为此添加一个新行 table:
'fred' 01-01-2018 5'10'' 200
基于用户 fred
和身高 5'10''
匹配 table 中已有的另一个用户,我想用新条目替换旧条目。 boto3 和 AWS 的文档有点不清楚 put_item -- 我该怎么做?
作为参考,这是我目前拥有的:
tracking_result = tracking_dbs[drivetype].put_item(
Item=track,
# insert fields to remove old entry that matches
# up with certain attributes from new entry here
)
只有当新条目的分区键和排序键与现有条目的分区键和排序键匹配时,DynamoDB 中已经存在的条目才会被新条目替换。否则将创建一个新条目。
在您的情况下,您必须删除现有条目(通过扫描而不是查询获取现有条目,因为查询需要分区和排序键作为输入参数,然后执行删除操作),然后创建新条目.