簡體   English   中英

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

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

我有一個更新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()操作有關。

謝謝!

在文檔中: 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將拋出異常,因此如果您只想檢查操作是否成功,則無需獲取返回值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM