Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDb update item only if new attribute is greater than existing one

I want to update an item in DynamoDB only if the new item has a more recent date than the existing item. Currently, I'm querying for the existing item, doing the comparison in my code, and then writing to db. I was wondering if there was a way to have DynamoDB do the checking for me. I've looked into using Expected, but its comparison operators need to take in a parameter, which defeats the purpose since it means having to query for the existing item anyway.

I'm working with Java 8.

like image 483
alexgbelov Avatar asked Oct 21 '16 22:10

alexgbelov


People also ask

Does DynamoDB support conditional operations?

Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.

Does DynamoDB Update create if not exists?

UpdateItem behaves as an “UPSERT” operation. This means that if you try to update an item that doesn't exist, DynamoDB will automatically create it for you. Like with PutItem , you can add conditions to your UpdateItem API calls to modify this behavior, but there is no way to implement it service-side.

How do I update a DynamoDB item?

To update an existing item in an Amazon DynamoDB table, you use the UpdateItem operation. You must provide the key of the item that you want to update. You must also provide an update expression, indicating the attributes that you want to modify and the values that you want to assign to them.

What is condition expression in DynamoDB?

In Amazon DynamoDB, you use expressions to denote the attributes that you want to read from an item. You also use expressions when writing an item to indicate any conditions that must be met (also known as a conditional update), and to indicate how the attributes are to be updated.


1 Answers

The ConditionExpression can be used to check the condition and update the item if the condition is satisfied. This is similar to the WHERE condition in the SQL statement. The differences are:-

1) DynamoDB requires both Partition key and Range key to update the item. The non key attribute conditions can be given in the ConditionExpression

2) DynamoDB can update only one item at a time.

ConditionExpression — (String) A condition that must be satisfied in order for a conditional update to succeed.

Expected — (map) This is a legacy parameter, for backward compatibility. New applications should use ConditionExpression instead. Do not combine legacy parameters and expression parameters in a single API call; otherwise, DynamoDB will return a ValidationException exception.

Sample code:-

The below code updates the item only if existing value "createdate" attribute is less than the new value (i.e. in other words new value is greater than the existing value in the table).

UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("yearkey", yearKey, "title", title)
        .withReturnValues(ReturnValue.UPDATED_NEW).withUpdateExpression("set #createdate = :val1")
        .withNameMap(new NameMap().with("#createdate", "createdate"))
        .withValueMap(new ValueMap().withString(":val1", createDate))
        .withConditionExpression("createdate < :val1");

UpdateItemOutcome outcome = null;
try {
    outcome = table.updateItem(updateItemSpec); 
} catch (ConditionalCheckFailedException ce) {
    System.out.println("Conditional check failed." + ce.getMessage());
    return false;
}

return true;
like image 50
notionquest Avatar answered Sep 20 '22 18:09

notionquest