java 如何将新的键值对放入 DynamoDB 中的映射中?(爪哇)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31141206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to put a new key-value pair into a map in DynamoDB? (java)
提问by Denis Li
I have a DynamoDB attribute whose value is a map from a Number to a String. I am trying to put in a new key-value pair. From what I've read, this seems to be possible, but I don't know how.
我有一个 DynamoDB 属性,其值是从数字到字符串的映射。我正在尝试放入一个新的键值对。根据我的阅读,这似乎是可能的,但我不知道如何。
I assume the solution is similar to the one in the link below:
我认为该解决方案类似于以下链接中的解决方案:
How to update a Map or a List on AWS DynamoDB document API?
如何更新 AWS DynamoDB 文档 API 上的地图或列表?
But I do not believe the example is on putting in a new item to a map. Could someone show me how to put in an item to a map?
但我不相信这个例子是在地图上放置一个新项目。有人可以告诉我如何将项目放入地图吗?
Thanks.
谢谢。
EDIT:
编辑:
I do not want to get the item, locally make the changes, and put it back. I am working with multiple clients who might interact concurrently (and I assume the update by dynamo ensures there will be no race conditions).
我不想获取该项目,在本地进行更改,然后将其放回原处。我正在与多个可能同时交互的客户合作(我假设 dynamo 的更新确保不会出现竞争条件)。
回答by Alexander Patrikalakis
With the following arguments to UpdateItem, you can condition adding a map entry at #number when map.#number does not exist in the map already:
使用 UpdateItem 的以下参数,您可以在地图中不存在 map.#number 时,在 #number 处添加地图条目:
UpdateExpression = "SET map.#number = :string"
ExpressionAttributeNames = { "#number" : "1" }
ExpressionAttributeValues = { ":string" : "the string to store in the map at key value 1" }
ConditionExpression = "attribute_not_exists(map.#number)"
回答by Surajit Kundu
Visit http://www.tryyourskill.com/aws/insert-or-append-key-values-map-in-dynamodb-using-javamay help you with the code snippet to add or append values in map column
访问http://www.tryyourskill.com/aws/insert-or-append-key-values-map-in-dynamodb-using-java可能会帮助您使用代码片段在地图列中添加或附加值
public boolean insertKeyValue(String tableName, String primaryKey, String
primaryKeyValue, String updateColumn, String newKey, String newValue) {
//Configuration to connect to DynamoDB
Table table = dynamoDB.getTable(tableName);
boolean insertAppendStatus = false;
try {
//Updates when map is already exist in the table
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey(primaryKey, primaryKeyValue)
.withReturnValues(ReturnValue.ALL_NEW)
.withUpdateExpression("set #columnName." + newKey + " = :columnValue")
.withNameMap(new NameMap().with("#columnName", updateColumn))
.withValueMap(new ValueMap().with(":columnValue", newValue))
.withConditionExpression("attribute_exists("+ updateColumn +")");
table.updateItem(updateItemSpec);
insertAppendStatus = true;
//Add map column when it's not exist in the table
} catch (ConditionalCheckFailedException e) {
HashMap<String, String> map = new HashMap<>();
map.put(newKey, newValue);
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey(primaryKey,primaryKeyValue)
.withReturnValues(ReturnValue.ALL_NEW)
.withUpdateExpression("set #columnName = :m")
.withNameMap(new NameMap().with("#columnName", updateColumn))
.withValueMap(new ValueMap().withMap(":m", map));
table.updateItem(updateItemSpec);
insertAppendStatus = true;
} catch(Exception e) {
e.printStackTrace();
}
return insertAppendStatus;
}