在 Java 中使用 DynamoDBMapper 更新 DynamoDB 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40152379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update DynamoDB item using DynamoDBMapper in Java
提问by meeza
How can I update DynamoDB item using DynamoDBMapper?
如何使用 DynamoDBMapper 更新 DynamoDB 项目?
I have multiple processes, using the DynamoDB table, thus, get + save will create inconsistency. I can not find the method to update the item using DynamoDBMapper.
我有多个进程,使用 DynamoDB 表,因此,get + save 会产生不一致。我找不到使用 DynamoDBMapper 更新项目的方法。
回答by notionquest
The save()
method will perform the putItem
or updateItem
based on the value set in SaveBehavior. Please refer the below description. There is no update method in DynamoDBMapper class because of this reason. However, there is a separate delete method available.
该save()
方法将根据SaveBehavior 中设置的值执行putItem
或。请参考以下说明。由于这个原因,DynamoDBMapper 类中没有更新方法。但是,有一个单独的删除方法可用。updateItem
Saves an item in DynamoDB. The service method used is determined by the DynamoDBMapperConfig.getSaveBehavior() value, to use either AmazonDynamoDB.putItem(PutItemRequest) or AmazonDynamoDB.updateItem(UpdateItemRequest):
UPDATE (default) :UPDATE will not affect unmodeled attributes on a save operation and a null value for the modeled attribute will remove it from that item in DynamoDB. Because of the limitation of updateItem request, the implementation of UPDATE will send a putItem request when a key-only object is being saved, and it will send another updateItem request if the given key(s) already exists in the table.
UPDATE_SKIP_NULL_ATTRIBUTES :Similar to UPDATE except that it ignores any null value attribute(s) and will NOT remove them from that item in DynamoDB. It also guarantees to send only one single updateItem request, no matter the object is key-only or not.
CLOBBER :CLOBBER will clear and replace all attributes, included unmodeled ones, (delete and recreate) on save. Versioned field constraints will also be disregarded. Any options specified in the saveExpression parameter will be overlaid on any constraints due to versioned attributes.
在 DynamoDB 中保存项目。使用的服务方法由 DynamoDBMapperConfig.getSaveBehavior() 值决定,以使用 AmazonDynamoDB.putItem(PutItemRequest) 或 AmazonDynamoDB.updateItem(UpdateItemRequest):
UPDATE(默认):UPDATE 不会影响保存操作中的未建模属性,并且建模属性的空值会将其从 DynamoDB 中的该项目中删除。由于updateItem 请求的限制,UPDATE 的实现会在保存key-only 对象时发送一个putItem 请求,如果给定的key(s) 已经存在于表中,它将发送另一个updateItem 请求。
UPDATE_SKIP_NULL_ATTRIBUTES :与 UPDATE 类似,除了它忽略任何空值属性并且不会从 DynamoDB 中的该项目中删除它们。它还保证只发送一个 updateItem 请求,无论对象是否为 key-only。
CLOBBER :CLOBBER 将在保存时清除并替换所有属性,包括未建模的属性(删除和重新创建)。版本化字段约束也将被忽略。由于版本化属性,saveExpression 参数中指定的任何选项都将覆盖在任何约束上。
Example usage:-
示例用法:-
DynamoDBMapperConfig dynamoDBMapperConfig = new DynamoDBMapperConfig(SaveBehavior.UPDATE);
UPDATEDynamoDBMapperConfig (aws sdk 1.11.473) constructor seems to be deprecated and the builder should be used instead:
UPDATEDynamoDBMapperConfig (aws sdk 1.11.473) 构造函数似乎已被弃用,应改用构建器:
DynamoDBMapperConfig dynamoDBMapperConfig = new DynamoDBMapperConfig.Builder()
.withConsistentReads(DynamoDBMapperConfig.ConsistentReads.CONSISTENT)
.withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.UPDATE)
.build();
dynamoDBMapper.save(yourObject, dynamoDBMapperConfig);
回答by Shibashis
To have consistency in your dynamo db write operations you will have to choose one option between Optimistic locking and Conditional writes.
要在 dynamo db 写入操作中保持一致性,您必须在乐观锁定和条件写入之间选择一个选项。
Here are the links to AWS documentation which may help you;
以下是 AWS 文档的链接,可能会对您有所帮助;