gpt4 book ai didi

database - 简单数据库 : Insert item only if it doesn't already exist

转载 作者:搜寻专家 更新时间:2023-10-30 19:52:02 24 4
gpt4 key购买 nike

我想在 Amazon SimpleDB 域中添加一个新项目,前提是没有另一个项目具有相同的项目名称。

我知道如何为属性做这件事。但我希望检查项目名称以确保它是唯一的并且不会覆盖现有项目——当然不需要额外的选择查询。

检查属性的示例:

https://sdb.amazonaws.com/
?Action=PutAttributes
&DomainName=MyDomain
&ItemName=JumboFez
&Attribute.1.Name=quantity
&Attribute.1.Value=14
&Attribute.1.Replace=true
&Expected.1.Name=quantity
&Expected.1.Exists=false
&AWSAccessKeyId=[valid access key id]
[...]

根据FAQ这应该是可能的:

“These semantics can also be used to implement functionality such as counters, inserting an item only if it does not already exist […]”

最佳答案

你是对的,你可以使用条件放置来实现这个SimpleDB Docs conditional puts .

基本上,您知道您将拥有的项目的任何属性都可以与条件更新的存在性检查一起使用,以确保只有在该特定 itemName 的属性不存在时才会发生放置。

在使用亚马逊的 java SDK 的 java 中,这看起来像:-

// add a conditional check to ensure that the specified profile does not already
// exist - if there's a timestamp, then this profile already exists
UpdateCondition condition = new UpdateCondition("quantity", null, false);

final PutAttributesRequest request = new PutAttributesRequest().withDomainName("MyDomain");
request.setItemName("JumboFez");
request.setAttributes(attributes);
request.setExpected(condition);

sdb.putAttributes (request);

这基本上减少了您所写的内容,并且只要您永远没有没有“数量”属性的有效项目,就会做您想做的事。

可能值得注意的是,在您的示例中,您已将 replace 设置为 true,如果您使用条件放置来确保该项目是新项目,则您不需要设置 replace=true(根据文档和经验证据,replace=true 比 replace=false 成本更高,花费的时间更长)。

关于database - 简单数据库 : Insert item only if it doesn't already exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4987235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com