用于条件保存的 DynamoDBMapper
DynamoDBMapper for conditional saves
我正在使用 DynamoDBMapper 并希望有条件地保存当且仅当散列键和范围键组合不存在时。我知道有一些方法可以使用 UUID 来减少碰撞的可能性,但我想通过使用条件保存来保护自己。
我遇到了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。
关于如何有条件地保存有什么建议吗?或者我可能做错了什么?
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
。
我正在使用 DynamoDBMapper 并希望有条件地保存当且仅当散列键和范围键组合不存在时。我知道有一些方法可以使用 UUID 来减少碰撞的可能性,但我想通过使用条件保存来保护自己。
我遇到了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。
关于如何有条件地保存有什么建议吗?或者我可能做错了什么?
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
。