简体   繁体   English

使用Boto3,如果对象上存在某些属性,请获取put_item替换DynamoDB中的对象?

[英]Using Boto3, get put_item to replace an object in DynamoDB if certain attributes exist on the object?

I'm having issues getting Boto3's put_item to replace an object in DynamoDB if certain attributes (some of which are the primary keys of the DynamoDB table, and some of which are not) match up with the new object that I'm inserting into the table. 如果某些属性(其中某些是DynamoDB表的主键,而某些不是)与我要插入的新对象匹配,则我在让Boto3的put_item替换put_item中的对象时遇到问题。表。

For instance, if a DynamoDB table has within it: 例如,如果其中包含DynamoDB表:

user (Partition Key) | ts (Sort Key) | height | weight
__________________________________________________________________________________

'fred'                  01-01-2017     5'10''   190
'george'                01-02-2017     5'08''   200

and I'm trying to add a new row to this table: 并且我正在尝试向此表添加新行:

'fred'                 01-01-2018     5'10''    200

Based on the user fred and the height 5'10'' matching another user already in the table, I'd like to substitute the new entry for the old one. 根据用户fred和身高5'10''匹配表中已有另一个用户的情况,我想用新条目代替旧条目。 The docs are bit unclear for boto3 and AWS put_item -- how do I do so? Boto3和AWS put_item的文档尚不清楚-我该怎么做?

For reference, this is what I have currently: 供参考,这是我目前拥有的:

 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
  )

The already existing entry in the DynamoDB will be replaced by the new entry only if the partition key and the sort key of the new entry matches that of the existing entry. 仅当新条目的分区键和排序键与现有条目的分区键和排序键匹配时,DynamoDB中已经存在的条目才会被新条目替换。 Otherwise a new entry will be created. 否则,将创建一个新条目。

In your case you have to delete the existing entry( by getting the existing entry by scanning and not querying as querying requires both partition and sort key as input parameters and then performing the delete operation) and then creating the new entry. 在您的情况下,您必须删除现有条目(通过扫描而不是查询来获取现有条目,因为查询需要将分区键和排序键都作为输入参数,然后执行删除操作),然后创建新条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM