如果项目已存在,如何在SimpleDB中的项目中添加属性?

时间:2013-08-22 11:11:29

标签: amazon-web-services amazon-simpledb

我只想在我添加的项目尚未存在的情况下添加属性。

据我所知,UpdateCondition构造只允许您根据项目的特定属性是否存在指定的值进行更新。

1 个答案:

答案 0 :(得分:-2)

Here是执行条件看跌期权的帮助链接。

Help链接,用于使用条件放置仅放置属性(如果它不存在)。

已编辑:

如果不运行Amazon SimpleDB Query,则无法检查项目是否存在。您可以查询项目,如果amazon simpledb未在请求中返回该项目,则表示该项目不存在。 检查示例代码 -

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());
        }