DynamoDBMapper for conditional savings

I use DynamoDBMapper and would like to conditionally save if and only if the hashkey and range key combination does not exist. I know that there are ways to use UUIDs to reduce the chance of a collision, but I would like to protect myself using conditional savings.

I came across this article which uses DynamoDBSaveExpression however I cannot indicate that the condition "hashkey AND rangekey" cannot exist. The API points to the withConditionalOperator method, but I cannot see this in my class. I use the latest aws java sdk also from here .

Any suggestions on how to conditionally save? Or what can I do wrong?

+7
java amazon-web-services amazon-dynamodb
source share
1 answer
 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 } 

This uses the constructor public ExpectedAttributeValue(Boolean exists) , which only internally calls setExists .

+18
source share

All Articles