How do I know if an update or insert was successful in dynamoDB using the Java SDK?

I have a Java function that updates a DynamoDB element. I want to handle the case when the update failed 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 calling updateItem is an UpdateItemOutcome object. All of this is the getItem () method, which should provide the returned attributes from the update operation, and the getUpdateItemResult () method, which provides the UpdateItemResult object.

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

Does anyone know what is best for checking the result of such operations in DynamoDB? This question also applies to putItem () operations.

Thanks!

+5
source share
1 answer

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 UpdateItemOutcome will fill in the fields.

However, DynamoDB throws an exception if the update or transfer operation is not performed, so if you only want to check whether the operation was successful, returning values ​​is not required.

+3
source

All Articles