问题描述
如何使用 DynamoDBMapper 更新 DynamoDB 项目?
How can I update DynamoDB item using DynamoDBMapper?
我有多个进程,使用 DynamoDB 表,因此,get + save 会造成不一致.我找不到使用 DynamoDBMapper 更新项目的方法.
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.
推荐答案
save()
方法会执行putItem
或updateItem
基于 SaveBehavior 中设置的值.请参考以下说明.由于这个原因,DynamoDBMapper 类中没有更新方法.但是,有一个单独的删除方法可用.
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.
在 DynamoDB 中保存项目.使用的服务方法由DynamoDBMapperConfig.getSaveBehavior() 值,使用任一AmazonDynamoDB.putItem(PutItemRequest) 或AmazonDynamoDB.updateItem(UpdateItemRequest):
更新(默认):UPDATE 不会影响保存操作和建模属性的 null 值会将其从该项目中删除动态数据库.由于 updateItem 请求的限制,当只有键时,UPDATE 的实现将发送 putItem 请求对象正在被保存,如果它会发送另一个 updateItem 请求给定的键已经存在于表中.
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 : 与 UPDATE 类似,只是它忽略任何空值属性,并且不会将它们从该项目中删除动态数据库.它还保证只发送一个 updateItem请求,无论对象是否仅键.
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将清除并替换所有属性,包括未建模的属性,(删除并重新创建)保存.版本化的字段约束也将被忽视.saveExpression 参数中指定的任何选项由于版本化属性,将覆盖在任何约束上.
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.
用法示例:-
DynamoDBMapperConfig dynamoDBMapperConfig = new DynamoDBMapperConfig(SaveBehavior.UPDATE);
更新 DynamoDBMapperConfig (aws sdk 1.11.473) 构造函数似乎已被弃用,应该改用构建器:
UPDATE DynamoDBMapperConfig (aws sdk 1.11.473) constructor seems to be deprecated and the builder should be used instead:
DynamoDBMapperConfig dynamoDBMapperConfig = new DynamoDBMapperConfig.Builder()
.withConsistentReads(DynamoDBMapperConfig.ConsistentReads.CONSISTENT)
.withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.UPDATE)
.build();
dynamoDBMapper.save(yourObject, dynamoDBMapperConfig);
这篇关于在 Java 中使用 DynamoDBMapper 更新 DynamoDB 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!