Mat*_*son 13 lambda amazon-web-services amazon-dynamodb
我试图编写一个lambda函数来向DynamoDB表添加新数据.从阅读文档:
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property PUT方法:"通过委派给AWS创建新项目或用新项目替换旧项目.DynamoDB.putItem()".
除了在'put'之前检查一个对象之外,当尝试PUT时,是否存在一个设置或标志以使对象失败?
我可以看到
params -> Expected -> Exists (Bool)
Run Code Online (Sandbox Code Playgroud)
但无法看到有关此功能的任何文档.
什么是阻止项目覆盖的最佳架构(或禁止)?
Query the table first and if no item exists then add the item
Run Code Online (Sandbox Code Playgroud)
要么
Attempt to insert the item and on failure because of duplicate entry report this back? (Is there a way to prevent item overwrite?)
Run Code Online (Sandbox Code Playgroud)
not*_*est 17
的ConditionExpression可用于检查键属性值是否已经存在于表中,并执行仅当密钥值不存在于该表中的PUT操作.
运行以下代码时,第一次put操作应该成功.在第二次运行中,put操作应该失败,并且"条件请求失败"异常.
我的电影表有分区和排序键.所以,我在条件表达式中使用了这两个属性.
带条件的示例代码: -
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));
}
});
Run Code Online (Sandbox Code Playgroud)
第二次执行放置操作时出现异常: -
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
}
Run Code Online (Sandbox Code Playgroud)
我看到这个问题与JavaScript语言有关,无论如何,我也将为Java编写代码(也许对某人有用):
DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression();
Map<String, ExpectedAttributeValue> expectedAttributes =
ImmutableMapParameter.<String, ExpectedAttributeValue>builder()
.put("hashKeyAttrName", new ExpectedAttributeValue(false))
.put("rangeKeyAttrName", new ExpectedAttributeValue(false))
.build();
saveExpression.setExpected(expectedAttributes);
saveExpression.setConditionalOperator(ConditionalOperator.AND);
try {
dynamoDBMapper.save(item, saveExpression);
} catch (ConditionalCheckFailedException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
ConditionalCheckFailedException 如果我们将尝试使用DynamoDB中已存在的hashKey和rangeKey对保存项目,则会抛出该错误。
As an addition to the correct answer of notionquest, the recommended way to express a condition that makes a putItem fail in case the item already exists is to assert that the partition key is not present in any potentially existing item:
"ConditionExpression": "attribute_not_exists(pk)",
Run Code Online (Sandbox Code Playgroud)
这显示为“如果该项目在 之前已经存在putItem,请确保它没有分区键”(pk在本例中为分区键)。由于没有分区键就不可能存在项目,因此这实际上意味着“确保该项目不存在”。
另请参阅本页开头的“注意”块:https : //awscli.amazonaws.com/v2/documentation/api/latest/reference/dynamodb/put-item.html
这里有一个例子,用于保证非键列的唯一性:
| 归档时间: |
|
| 查看次数: |
5587 次 |
| 最近记录: |