簡體   English   中英

DynamoDB API:如何構建“添加JSON屬性(如果不存在)”更新請求?

[英]DynamoDB API: How can I build an “add JSON attribute if not present” update request?

我正在嘗試使用新的Amazon DynamoDB JSON API在名為“document”的JSON屬性中添加/覆蓋鍵值對。 理想情況下,我想簡單地構造我的寫入調用以發送KV對以添加到屬性,並且如果給定主鍵尚不存在,則讓Dynamo創建屬性。 但是,如果我只用一個簡單的UpdateItemSpec嘗試這個:

PrimaryKey primaryKey = new PrimaryKey("key_str", "mapKey");
ValueMap valuesMap = new ValueMap().withLong(":a", 1234L).withLong(":b", 1234L);
UpdateItemSpec updateSpec = new UpdateItemSpec().withPrimaryKey(primaryKey).withUpdateExpression("SET document.value1 = :a, document.value2 = :b");
updateSpec.withValueMap(valuesMap);
table.updateItem(updateSpec);

我得到com.amazonaws.AmazonServiceException: The document path provided in the update expression is invalid for update ,這意味着DynamoDB找不到要應用更新的名為“document”的給定屬性。

我設法使用以下一系列調用來近似此功能:

try {
    // 1. Attempt UpdateItemSpec as if attribute already exists
} catch (AmazonServiceException e) {
    // 2. Confirm the exception indicated the attribute was not present, otherwise rethrow it
    // 3. Use a put-if-absent request to initialize an empty JSON map at the attribute "document"
    // 4. Rerun the UpdateItemSpec call from the above try block
}       

這可行,但不太理想,因為每次向表中添加新的主鍵時都需要3次調用DynamoDB。 我使用了可以在Update Expressions中使用的attribute_not_exists函數進行了一些實驗,但是無法以我想要的方式使它工作。

任何Dynamo大師都有關於如何/是否可以做到的任何想法?

我收到亞馬遜支持部門的答復,實際上無法通過一次通話完成此操作。 通過在put-if-absent請求中使用所需的JSON映射而不是空映射,他們確實建議在將新主鍵的屬性從3添加到2時減少調用次數。

暫無
暫無

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

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