【问题标题】:How to put a new key-value pair into a map in DynamoDB? (java)如何将新的键值对放入 DynamoDB 中的映射中? (爪哇)
【发布时间】:2015-06-30 14:34:15
【问题描述】:

我有一个 DynamoDB 属性,其值是从数字到字符串的映射。我正在尝试放入一个新的键值对。从我读过的内容来看,这似乎是可能的,但我不知道如何。

我认为解决方案类似于以下链接中的解决方案:

How to update a Map or a List on AWS DynamoDB document API?

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

谢谢。

编辑:

我不想获取该项目,在本地进行更改并将其放回原处。我正在与可能同时交互的多个客户合作(我假设 dynamo 的更新确保不会出现竞争条件)。

【问题讨论】:

  • 请记住,DynamoDB 是“最终一致的”。同样,即使您对 DynamoDB 进行了更新然后立即阅读,也不能保证更新是可见的。这在使用副本表时更为明显。因此,直接更新并不会真正避免下拉、编辑和再次回写的影响。

标签: java amazon-dynamodb


【解决方案1】:

使用 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)"

【讨论】:

  • 不错的方法,但是如果发电机内部没有先前的map 字段怎么办?如果您尝试更新新记录,它会抛出 The document path provided in the update expression is invalid for update,如果该字段以前不存在,我找不到创建新项目的方法,有什么帮助吗?
  • 以“attribute_exists(map) 和 attribute_not_exists(map.#number)”为条件,然后捕获条件检查失败异常并尝试 SET map = :m, attribute_not_exists(map) 的不同 updateItem 请求.请记住,您始终可以使用空地图预先创建项目!
  • 谢谢。无法向地图添加新属性和值时,我快疯了。它不断将属性添加到表中。然后我看到您在上面的示例中如何指示地图的路径(map.#number)。现在我的代码可以工作了!谢谢。
  • 如果地图尚不存在,您能否使用 ADD 而不是 SET 创建地图?我一直在为其他领域这样做,但不太确定它是否适用于地图。
【解决方案2】:

访问http://www.tryyourskill.com/aws/insert-or-append-key-values-map-in-dynamodb-using-java 可以帮助您使用代码 sn-p 在地图列中添加或附加值

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

【讨论】:

  • 链接失效了,不知道是不是被移动了?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
  • 1970-01-01
相关资源
最近更新 更多