如何将新的键值对放入DynamoDB中的地图中? (Java)的

我有一个DynamoDB属性,其值是从Number到String的映射。 我试图加入一个新的键值对。 从我所读到的,这似乎是可能的,但我不知道如何。

我假设解决方案类似于以下链接中的解决方案:

如何在AWS DynamoDB文档API上更新地图或列表?

但我不相信这个例子是在向地图添加新项目。 有人可以告诉我如何将项目放入地图吗?

谢谢。

编辑:

我不想获取该项目,在本地进行更改,然后将其恢复。 我正与多个可能同时进行交互的客户合作(我假设发电机更新确保没有竞争条件)。

使用以下UpdateItem参数,您可以条件在映射时在#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)" 

访问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 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; }