Amazon web services 仅当SimpleDB中的项已存在时,如何向该项添加属性?
我只想在添加属性的项目不存在时添加属性 据我所知,UpdateCondition构造只允许您根据是否存在具有指定值的项的特定属性进行更新。是执行条件Put的帮助链接 链接,用于使用条件puts仅在属性不存在时放置属性 编辑: 如果不运行Amazon SimpleDB查询,则无法检查项目是否存在。您可以查询一个项目,如果amazon simpledb没有在请求中返回该项目,则表示该项目不存在。 检查示例代码-Amazon web services 仅当SimpleDB中的项已存在时,如何向该项添加属性?,amazon-web-services,amazon-simpledb,Amazon Web Services,Amazon Simpledb,我只想在添加属性的项目不存在时添加属性 据我所知,UpdateCondition构造只允许您根据是否存在具有指定值的项的特定属性进行更新。是执行条件Put的帮助链接 链接,用于使用条件puts仅在属性不存在时放置属性 编辑: 如果不运行Amazon SimpleDB查询,则无法检查项目是否存在。您可以查询一个项目,如果amazon simpledb没有在请求中返回该项目,则表示该项目不存在。 检查示例代码- try { BasicAWSCredentials basicA
try {
BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials("<accessKey>", "<secretkey>");
AmazonSimpleDBClient amazonSimpleDBClient = new AmazonSimpleDBClient(basicAWSCredentials);
amazonSimpleDBClient.setEndpoint("sdb.amazonaws.com");
SelectRequest selectRequest = new SelectRequest("select * from `<domainName>` where itemName()='<itemName>'");
SelectResult selectResult = amazonSimpleDBClient.select(selectRequest);
List<Item> itemList = selectResult.getItems();
if (itemList.size() == 0) {
System.out.println("Specified item does not exist.");
List<ReplaceableAttribute> list = new ArrayList<ReplaceableAttribute>();
ReplaceableAttribute replaceableAttribute = new ReplaceableAttribute("<attribute>", "<value>", Boolean.TRUE);
list.add(replaceableAttribute);
PutAttributesRequest attributesRequest = new PutAttributesRequest("<domainName>", "<itemName>", list);
amazonSimpleDBClient.putAttributes(attributesRequest);
} else {
System.out.println("Specified item exist. Do Nothing.");
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
ConditionalPut仅根据是否已存在具有指定值的属性而工作。如果在执行select语句后,该项被另一个进程删除,该怎么办?@ThomShutt Amazon SimpleDB在将属性放入域中时,如果该项不存在,则不会返回或引发任何异常。因此,每次在将属性放入案例之前,您都需要进行查询。我的意思是,如果在select语句完成后,该项被另一个进程删除,则代码中会存在竞争条件。@ThomShutt这与您所问的上述问题不同。正如我对您的原始问题所作的回答——您可以通过运行SELECT查询来检查该项的存在,若Amazon SimpleDB并没有返回该项,那个么您可以请求put属性。仅供参考-您可以同步AmazonSimpleDBClient的对象,以避免竞相条件。