【问题标题】:DynamoDB PutItem then UpdateItem consistency with ReturnValues ALL_NEWDynamoDB PutItem 然后 UpdateItem 与 ReturnValues 的一致性 ALL_NEW
【发布时间】:2018-01-25 11:59:44
【问题描述】:

当使用PutItem 添加一个新项目,然后使用返回值设置为ALL_NEW 的UpdateItem 更新它时,是否预期返回值将是高度一致的?

例如放置物品;

{key: 1a a: 1}

然后更新项目;

{key: 1, b: 2}

我希望 ReturnValues: ALL_NEW 返回

{key: 1, a: 1, b: 2}

但看起来情况并非如此?

【问题讨论】:

    标签: amazon-dynamodb consistency eventual-consistency


    【解决方案1】:

    我已经更新了成功执行 put item 的 item 并得到了你期望的结果。

    注意:测试在本地 DynamoDB 上执行。

    ALL_NEW - 返回项目的所有属性,就像它们出现时一样 在 UpdateItem 操作之后。

    示例代码:-

    var docClient = new AWS.DynamoDB.DocumentClient();
    
    var table = "post";
    
    var paramsPut = {
        TableName: table,
        Item: {
            "postId": '16',
            "Pos": {
                "key": "1a", "a": "1"
            }
        }
    };
    
    var paramsUpdate = {
        TableName: "post",
        Key: {
            "postId": "16"
        },
        UpdateExpression: "SET Pos.#key = :keyVal, Pos.b = :keyVal2",
        ExpressionAttributeNames: {
            "#key": "key"
        },
        ExpressionAttributeValues: {
            ":keyVal": "1",
            ":keyVal2": "2"
        },
        ReturnValues: "ALL_NEW"
    };
    
    console.log("Adding a new item...");
    docClient.put(paramsPut, function (err, data) {
        if (err) {
            console.error("Unable to add item. Error JSON:", JSON.stringify(err,
                null, 2));
        } else {
            console.log("Added item:", JSON.stringify(data, null, 2));
    
            console.log("Then Updating the item...");
            docClient.update(paramsUpdate, function (err, data) {
                if (err) {
                    console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
                } else {
                    console.log("UpdateItem succeeded:", JSON.stringify(data));
                }
            });
        }
    });
    

    输出:-

    Adding a new item...
    Added item: {}
    Then Updating the item...
    UpdateItem succeeded: {"Attributes":{"Pos":{"a":"1","b":"2","key":"1"},"postId":
    "16"}}
    

    【讨论】:

    • 这确实是一个一致性问题,所以我认为您不会期望在实际的 DDB 集群中看到相同的结果
    猜你喜欢
    • 2019-09-05
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多