問題描述
如何使用 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()
方法會執(zhí)行putItem
或updateItem
基于 SaveBehavior 中設(shè)置的值.請參考以下說明.由于這個原因,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 中保存項目.使用的服務(wù)方法由DynamoDBMapperConfig.getSaveBehavior() 值,使用任一AmazonDynamoDB.putItem(PutItemRequest) 或AmazonDynamoDB.updateItem(UpdateItemRequest):
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 不會影響保存操作和建模屬性的 null 值會將其從該項目中刪除動態(tài)數(shù)據(jù)庫.由于 updateItem 請求的限制,當只有鍵時,UPDATE 的實現(xiàn)將發(fā)送 putItem 請求對象正在被保存,如果它會發(fā)送另一個 updateItem 請求給定的鍵已經(jīng)存在于表中.
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 類似,只是它忽略任何空值屬性,并且不會將它們從該項目中刪除動態(tài)數(shù)據(jù)庫.它還保證只發(fā)送一個 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將清除并替換所有屬性,包括未建模的屬性,(刪除并重新創(chuàng)建)保存.版本化的字段約束也將被忽視.saveExpression 參數(shù)中指定的任何選項由于版本化屬性,將覆蓋在任何約束上.
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) 構(gòu)造函數(shù)似乎已被棄用,應(yīng)該改用構(gòu)建器:
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);
這篇關(guān)于在 Java 中使用 DynamoDBMapper 更新 DynamoDB 項目的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!