我有一个更新 DynamoDB 项目的 Java 函数。我想处理由于某种原因更新不成功的情况。我的代码看起来像这样:
Table table = dynamoDB.getTable(tableName);
AttributeUpdate att = new attributeUpdate(fieldName).put(value);
UpdateItemOutcome outcome = table.updateItem(keyFieldName, keyValue, att);
updateItem 调用的结果是一个 UpdateItemOutcome 对象。所有这些都是一个getItem()方法,它应该提供从更新操作返回的属性,以及一个getUpdateItemResult()方法,它提供了一个UpdateItemResult对象。
getItem() 即使调用成功,也会给我 null。对象似乎没有任何方法可以为我提供有关操作的任何类型的状态或错误。
有谁知道在 DynamoDB 中检查此类操作结果的最佳实践是什么?这个问题也与 putItem() 操作有关。
谢谢!
在文档中: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
你可以做:
UpdateItemSpec updateItemSpec = new UpdateItemSpec();
...
updateItemSpec.withReturnValues(ReturnValue.ALL_NEW);
然后,UpdateItemOutcome
将填充字段。
但是,如果更新或放置操作失败,DynamoDB 将引发异常,因此如果您只想检查操作是否成功,则无需获取返回值。