0

There are several articles on how to throw constraint validation exception (ex: DynamoDBMapper save only if object doesn't exist) on if the hash key is duplicate or matching your criteria, was wondering if the same applies to range key.

I tested locally and it does not throw error.

My save expression logic is as below

            DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression();
        Map<String, ExpectedAttributeValue> expectedAttributes =
                ImmutableMap.<String, ExpectedAttributeValue>builder()
                        .put("range-key-column",
                             new ExpectedAttributeValue().withExists(false))
                        .build();
        saveExpression.setExpected(expectedAttributes);
Community
  • 1
  • 1
Superaghu
  • 775
  • 1
  • 5
  • 15

1 Answers1

0

It does throw exception for me. While calling the save, have you added the saveExpression as second argument like below.

dynamoDBMapper.save(trail, saveExpression);

1) Please note that both hash and range keys should be set while saving the data. If either hash key or range key is not provided, you would get an exception.

Facebook_id - Hash key Latitude - Range key

        DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(dynamoDBClient);

    Trail trail = new Trail();
    trail.setFacebook_id(hashKey);
    trail.setLatitude(rangeKey);
    trail.setLongitude("longupdate121");

    DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression();
    Map<String, ExpectedAttributeValue> expected = new HashMap<>();
    expected.put("latitude", 
            new ExpectedAttributeValue().withExists(false));

    saveExpression.setExpected(expected);

    dynamoDBMapper.save(trail, saveExpression);

com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException: The conditional request failed (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ConditionalCheckFailedException; Request ID: 8e6d5842-d4b7-479b-8eb7-0a746e5fac48)

notionquest
  • 37,595
  • 6
  • 111
  • 105
  • Is your hashkey different b/w records when you got the ConditionalCheckFailedException? – Superaghu Jul 07 '16 at 00:01
  • I notice that when hash key + range key is same and the saveExpression has only range key to check, it does throw the validation exception. However when hash key is different but range key is same, it is not. So my understanding is that the save expression checks for combination of hash + range key even if you have instructed to only use range key for comparison. – Superaghu Jul 07 '16 at 00:09