在 Java 中使用 DynamoDBMapper 更新 DynamoDB 项目
Update DynamoDB item using DynamoDBMapper in Java
如何使用 DynamoDBMapper 更新 DynamoDB 项目?
我有多个进程,使用 DynamoDB table,因此,get + save 会造成不一致。我找不到使用 DynamoDBMapper 更新项目的方法。
save()
方法将根据 SaveBehavior[=] 中设置的值执行 putItem
或 updateItem
26=]。请参考以下说明。由于这个原因,DynamoDBMapper class 中没有更新方法。但是,有一个单独的删除方法可用。
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.
用法示例:-
DynamoDBMapperConfig dynamoDBMapperConfig = new DynamoDBMapperConfig(SaveBehavior.UPDATE);
更新 DynamoDBMapperConfig (aws sdk 1.11.473) 构造函数似乎已被弃用,应该改用构建器:
DynamoDBMapperConfig dynamoDBMapperConfig = new DynamoDBMapperConfig.Builder()
.withConsistentReads(DynamoDBMapperConfig.ConsistentReads.CONSISTENT)
.withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.UPDATE)
.build();
dynamoDBMapper.save(yourObject, dynamoDBMapperConfig);
要使 dynamo 数据库写入操作保持一致,您必须在乐观锁定和条件写入之间选择一个选项。
以下是可能对您有所帮助的 AWS 文档链接;
如何使用 DynamoDBMapper 更新 DynamoDB 项目?
我有多个进程,使用 DynamoDB table,因此,get + save 会造成不一致。我找不到使用 DynamoDBMapper 更新项目的方法。
save()
方法将根据 SaveBehavior[=] 中设置的值执行 putItem
或 updateItem
26=]。请参考以下说明。由于这个原因,DynamoDBMapper class 中没有更新方法。但是,有一个单独的删除方法可用。
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.
用法示例:-
DynamoDBMapperConfig dynamoDBMapperConfig = new DynamoDBMapperConfig(SaveBehavior.UPDATE);
更新 DynamoDBMapperConfig (aws sdk 1.11.473) 构造函数似乎已被弃用,应该改用构建器:
DynamoDBMapperConfig dynamoDBMapperConfig = new DynamoDBMapperConfig.Builder()
.withConsistentReads(DynamoDBMapperConfig.ConsistentReads.CONSISTENT)
.withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.UPDATE)
.build();
dynamoDBMapper.save(yourObject, dynamoDBMapperConfig);
要使 dynamo 数据库写入操作保持一致,您必须在乐观锁定和条件写入之间选择一个选项。
以下是可能对您有所帮助的 AWS 文档链接;