簡體   English   中英

如何更新 DynamoDB 中的許多屬性

[英]How do I update many attributes in DynamoDB

我已經看到很多使用 UpdateExpression 使用 updateItem 方法更新屬性的示例。 但是,我仍然不明白如何同時動態更新DynamoDB中的多個屬性。

我正在嘗試在同一個 updateItem 調用中更新和重命名多個屬性。 我知道這需要刪除舊名稱和一組新名稱。 我在對象的 hashedId 中有這些名稱,但直到運行時才會有它們。 所以我的問題是如何將 UpdateExpression 用於變量而不是硬編碼的字符串?

我見過的所有示例都使用硬編碼的 UpdateExpressions。

無法更新 DynamoDB 中的項目

Dynamo DB:UpdateItemSpec:多個更新表達式 - 不工作

DynamoDB 更新 Item 多操作

如何重命名 DynamoDB 列/鍵

我在 Java 工作。

對我來說,我找不到這樣的例子似乎很奇怪……這讓我相信我做錯了什么。

謝謝您的幫助!

您必須根據在運行時收到的屬性名稱和值動態構建更新表達式字符串。 我正是這樣做的。 我不是在 Java 中工作,但這里有一些偽代碼(帶有 Ruby 偏見)示例,它們動態構建更新表達式字符串、表達式屬性名稱哈希和表達式屬性值哈希。 然后,您可以將這 3 件事插入 update_item 方法中:


update_exp_set = [] //array of remove expression snippets
update_exp_remove = [] //array of update expression snippets

exp_attribute_names = {} //hash of attribute names
exp_attribute_values = {} //hash of attribute values

// Iterate through all your fields and add things as needed to your arrays and hashes.
// Two examples are below for a field with the name <fieldName> and value <fieldValue>.
// You'll need to convert this to Java and update it to make sense with the AWS Java SDK.

// For a given field that needs to be updated:
update_exp_set << "#<fieldName> = :fieldValue" //add to array of set expression snippets
exp_attribute_names["#<fieldName>"] = "<fieldName>" //add to hash of attribute names
exp_attribute_values[":<fieldValue>"] = "<fieldValue>" //add to hash of attribute values

// For a given field that needs to be removed:
update_exp_remove << "#<fieldName>"
exp_attribute_names["#<fieldName>"] = "<fieldName>" //add to hash of attribute names

// Use your snippets to create your full update expression:
update_exp_set_clause = ""
update_exp_remove_clause = ""
if update_exp_set.length != 0 //check if you have something to set
  update_exp_set_clause = "SET " + update_exp_set.join(',')
end
if update_exp_remove.length != 0 //check if you have something to remove
  update_exp_remove_clause = "REMOVE" + update_exp_remove.join(',')
end
final_update_exp = update_exp_set_clause + " " + update_exp_remove_clause

這有幫助嗎?

暫無
暫無

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

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