Reputation: 884
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!
Upvotes: 13
Views: 9592
Reputation: 2129
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.
Upvotes: 20