問題描述
我正在使用 DynamoDBMapper,并且希望有條件地保存當且僅當 hashkey 和 range 鍵組合不存在時.我知道有一些方法可以使用 UUID 來減少發(fā)生沖突的可能性,但我想通過使用條件保存來保護自己.
I'm using DynamoDBMapper and would like to conditionally save if and only if the hashkey and range key combination does not exist. I know there are ways to use UUIDs to reduce the possibility of a collision but I would like to protect myself by using conditional saves.
我遇到了這篇文章 使用 DynamoDBSaveExpression 但是我無法指定條件是hashkey AND rangekey"不能存在.API 指定withConditionalOperator
方法,但我在課堂上看不到這個.我也在使用來自 here 的最新 aws java sdk.
I came across this article that uses DynamoDBSaveExpression however I'm not able to specify that the condition is "hashkey AND rangekey" cannot exist. The API specifies a withConditionalOperator
method but I'm not able to see this in my class. I am using the latest aws java sdk also from here.
關于如何有條件地保存的任何建議?或者我可能做錯了什么?
Any suggestions on how to conditionally save? Or what I may be doing incorrectly?
推薦答案
DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression();
Map<String, ExpectedAttributeValue> expectedAttributes =
ImmutableMap.<String, ExpectedAttributeValue>builder()
.put("hashKey", new ExpectedAttributeValue(false))
.put("rangeKey", new ExpectedAttributeValue(false))
.build();
saveExpression.setExpected(expectedAttributes);
saveExpression.setConditionalOperator(ConditionalOperator.AND);
try {
dynamoDBMapper.save(objectToSave, saveExpression);
} catch (ConditionalCheckFailedException e) {
//Handle conditional check
}
這使用 public ExpectedAttributeValue(Boolean exists)
構造函數,它只是在內部調用 setExists
.
This uses the public ExpectedAttributeValue(Boolean exists)
constructor, which just internally calls setExists
.
這篇關于用于條件保存的 DynamoDBMapper的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!