DynamoDBMapper for conditional saves

前端 未结 1 1933
悲哀的现实
悲哀的现实 2020-12-15 07:08

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 red

相关标签:
1条回答
  • 2020-12-15 07:23
    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 public ExpectedAttributeValue(Boolean exists) constructor, which just internally calls setExists.

    0 讨论(0)
提交回复
热议问题