n00*_*00b 14 java amazon-web-services amazon-dynamodb
我正在使用DynamoDBMapper,并且当且仅当hashkey和range键组合不存在时才想有条件地保存.我知道有一些方法可以使用UUID来减少碰撞的可能性,但我想通过使用条件保存来保护自己.
我遇到了这篇使用DynamoDBSaveExpression的文章但是我无法指定条件是"hashkey AND rangekey"不能存在.该API指定一个withConditionalOperator方法,但我不能看到这在我的课.我也在这里使用最新的aws java sdk .
有关如何有条件保存的任何建议?或者我可能做错了什么?
mko*_*bit 27
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
}
Run Code Online (Sandbox Code Playgroud)
这使用了public ExpectedAttributeValue(Boolean exists)内部调用的构造函数setExists.
| 归档时间: |
|
| 查看次数: |
13067 次 |
| 最近记录: |