如何使用Java SDK查看dynamoDB中的更新或插入是否成功?

时间:2016-07-07 15:04:46

标签: java amazon-dynamodb

我有一个更新DynamoDB项目的Java函数。我想处理由于某种原因更新不成功的情况。我的代码看起来像这样:

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

updateItem调用的结果是UpdateItemOutcome对象。所有这一切都是一个getItem()方法,它应该提供更新操作的返回属性,以及一个提供UpdateItemResult对象的getUpdateItemResult()方法。

即使调用成功,

getItem()也会为null。 UpdateItemResult对象似乎没有任何方法可以为我提供有关操作的任何状态或错误。

有谁知道在DynamoDB中检查这样的操作结果的最佳做法是什么?这个问题也与putItem()操作有关。

谢谢!

1 个答案:

答案 0 :(得分:9)

在文档中:http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html

    <?php 
    echo '<p>President has been killed<p><img src = "D:/User Data\Documents/Sheridan/Summer Year 3/Enterprise Java Development/Projects/PhpAssignment/skull.png"/>';
?>

你可以这样做:

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); 将填充字段。

但是,如果更新或放置操作失败,DynamoDB将抛出异常,因此如果您只想检查操作是否成功,则无需获取返回值。