Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I find out if an update or insert was successful in dynamoDB using the Java SDK?

I have a Java function that updates a DynamoDB Item. I want to handle the case where the update is not successful for some reason. My code looks something like this:

Table table = dynamoDB.getTable(tableName);
AttributeUpdate att = new attributeUpdate(fieldName).put(value);
UpdateItemOutcome outcome = table.updateItem(keyFieldName, keyValue, att);

The result of the updateItem call is an UpdateItemOutcome object. All this has is a getItem() method which should provide the returned attributes from the update operation, and a getUpdateItemResult() method which provide an UpdateItemResult object.

getItem() gives me null even when the call succeeds. The UpdateItemResult object doesn't seem to have any method that provides me with any kind of status or error regarding the operation.

Does anyone know what the best practice is for checking the result of operations like this in DynamoDB? The question also pertains to putItem() operations.

Thanks!

like image 470
Yariv Adam Avatar asked Jul 07 '16 15:07

Yariv Adam


People also ask

How long does it take for DynamoDB to update?

How long does it take to update DynamoDB item? It's instant once you commit your save. The API operation completes usually in less than 100ms.

What is the difference between put and update in DynamoDB?

Main Difference Between DynamoDB UpdateItem & PutItemThe putItem function is used to replace an existing item or to create a new item. In contrast, the updateItem function is used to add, edit or delete attributes from an existing item.

How do you update items in DynamoDB table?

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.

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.


1 Answers

In the documentation: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html

Use ReturnValues if you want to get the item attributes as they appeared either before or after they were updated. For UpdateItem, the valid values are:

    NONE - If ReturnValues is not specified, or if its value is NONE, then nothing is returned. (This setting is the default for ReturnValues.)

    ALL_OLD - If UpdateItem overwrote an attribute name-value pair, then the content of the old item is returned.

    UPDATED_OLD - The old versions of only the updated attributes are returned.

    ALL_NEW - All of the attributes of the new version of the item are returned.

    UPDATED_NEW - The new versions of only the updated attributes are returned.

There is no additional cost associated with requesting a return value aside from the small network and processing overhead of receiving a larger response. No Read Capacity Units are consumed.

Values returned are strongly consistent

Type: String

Valid Values: NONE | ALL_OLD | UPDATED_OLD | ALL_NEW | UPDATED_NEW

Required: No

You can do:

UpdateItemSpec updateItemSpec = new UpdateItemSpec();
...
updateItemSpec.withReturnValues(ReturnValue.ALL_NEW);

Then the UpdateItemOutcome will have the fields populated.

However, DynamoDB will throw an exception if the update or put operation fails, so if you only want to check whether the operation succeeded, obtaining the return values is not necessary.

like image 198
gruuuvy Avatar answered Sep 19 '22 15:09

gruuuvy