如果条目已经存在,如何防止DynamoDB项被覆盖 [英] How to prevent a DynamoDB item being overwritten if an entry already exists

查看:321
本文介绍了如果条目已经存在,如何防止DynamoDB项被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个lambda函数,以将新数据添加到DynamoDB表中. 通过阅读以下文档:

Im trying to write a lambda function to add new data to a DynamoDB Table. From reading the docs at:

http://docs.aws. amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property PUT方法:通过委派到AWS.DynamoDB.putItem()创建新项目,或用新项目替换旧项目."

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property The PUT method: "Creates a new item, or replaces an old item with a new item by delegating to AWS.DynamoDB.putItem()."

除了在放入"之前检查对象之外,是否还有设置或标志使尝试PUT时对象失败?

Other than doing a check for an object before 'putting' is there a setting or flag to fail the object exists when the PUT is attempted?

我可以在

params -> Expected -> Exists (Bool)

但是看不到任何有关此功能的文档.

but can't see any documentation on what this does.

防止项目被覆盖的最佳架构(或禁食)是什么?

What would be the best architecture (or fasted) to prevent an item overwrite?

Query the table first and if no item exists then add the item

Attempt to insert the item and on failure because of duplicate entry report this back? (Is there a way to prevent item overwrite?)

推荐答案

ConditionExpression可用于检查表中是否已存在键属性值,并且仅当键值不存在于表中时才执行PUT操作.桌子.

The ConditionExpression can be used to check whether the key attribute values already exists in table and perform the PUT operation only if the key values are not present in the table.

运行以下代码时,第一次放置操作应该成功.在第二次运行中,放置操作应失败,并出现条件请求失败" 例外.

When you run the below code, first time the put operation should be successful. In the second run, the put operation should fail with "Conditional request failed" exception.

我的电影表同时具有分区键和排序键.因此,我在条件表达式中都使用了这两个属性.

My movies table has both partition and sort keys. So, I have used both the attributes in conditional expression.

有条件看跌的示例代码:-

var table = "Movies";

var year = 1502;
var title = "The Big New Movie";

var params = {
    TableName:table,
    Item:{
        "yearkey": year,
        "title": title,
        "info":{
            "plot": "Nothing happens at all.",
            "rating": 0
        }
    },
    ConditionExpression: "yearkey <> :yearKeyVal AND #title <>  :title",
    ExpressionAttributeNames: { 
        "#title" : "title" 
     },
    ExpressionAttributeValues: {
        ":yearKeyVal" : year,
        ":title": {"S": title}
    }
};

console.log("Adding a new item...");
docClient.put(params, 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));
    }
});

第二次执行放置操作时的异常:-

Unable to add item. Error JSON: {
  "message": "The conditional request failed",
  "code": "ConditionalCheckFailedException",
  "time": "2017-10-02T18:26:26.093Z",
  "requestId": "7ae3b0c4-3872-478d-908c-94bc9492a43a",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 0
}

这篇关于如果条目已经存在,如何防止DynamoDB项被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆