0

我刚开始使用适用于 AWS SDB 的 Android 开发工具包,在进行写入和读取时遇到了意外结果。这肯定是一个简单的问题,所以我将不胜感激任何解释!

这就是问题所在。

首先,我向 SDB 写入一条记录,如下所示:

sdb.createDomain(new CreateDomainRequest("myDomain"));

List<ReplaceableAttribute> attributes = new ArrayList<ReplaceableAttribute>(1);
attributes.add(new ReplaceableAttribute().withName("myField").withValue(myField));

sdb.putAttributes(new PutAttributesRequest("myDomain", itemName, attributes));

我可以看到 myField 的值已使用 Chrome SdbNavigator 正确写入 SDB。

现在我使用相同的代码更改记录,但 myField 属性的值不同。同样,我可以看到使用 SdbNavigator 使用新值正确写入了记录。

最后,我从设备上卸载了该应用程序(即,将其擦除干净),重新安装该应用程序,然后再次运行它以执行以下代码:

String s = "select * from `myDomain`";
SelectRequest selectRequest = new SelectRequest(s).withConsistentRead(true);
List items = sdb.select(selectRequest).getItems();

int count = items.size();

for (int i=0; i<count; i++) {
    Item item = (Item)(items.get(i));
    String itemName = item.getName();

    myField = getStringValueForAttributeFromList("myField", item.getAttributes());
}

其中 getStringValueForAttributeFromList() 定义为

protected String getStringValueForAttributeFromList( String attributeName, List<Attribute> attributes ) {
    for ( Attribute attribute : attributes ) {
        if ( attribute.getName().equals( attributeName ) ) {
            return attribute.getValue();
        }
    }
    return "";      
}

意想不到的部分是 getStringValueForAttributeFromList() 函数返回 myField 属性的第一个(现在不正确的)值 - 即使 sdbNavigator 显示记录具有第二个(正确的)值。

知道发生了什么,以及如何解决?谢谢。

4

1 回答 1

0

已解决:我面临的问题是 SDB 允许在同一记录(项目)中具有相同名称的多个属性,因此它选择了我为属性 myField 设置的第一个值。

如果要保证唯一性,则必须在可替换属性上设置标志,如下所示:

ReplaceableAttribute ra = new ReplaceableAttribute().withName("myField").withValue(myField);
ra.setReplace(true); attributes.add(ra);

然后调用 putAttributesRequest(),如上。谢谢。

于 2012-09-02T20:00:42.943 回答