I just started using the Android SDK for AWS SDB, and am encountering an unexpected result doing writes and reads. This is surely a simple problem, so I would appreciate any explanations!
Here’s the problem.
First, I write a record to SDB like this:
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));
I can see the value of myField is correctly written to SDB using the Chrome SdbNavigator.
Now I change the record using the same code, but with a different value for the myField attribute. Again, I can see that the record is written correctly with the new value using SdbNavigator.
Finally, I uninstall the app from the device (i.e., wipe it clean), reinstall the app, and run it again to execute the following code:
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());
}
where getStringValueForAttributeFromList() is defined as
protected String getStringValueForAttributeFromList( String attributeName, List<Attribute> attributes ) {
for ( Attribute attribute : attributes ) {
if ( attribute.getName().equals( attributeName ) ) {
return attribute.getValue();
}
}
return "";
}
The unexpected part is that the getStringValueForAttributeFromList() function returns the first (now incorrect) value of the myField attribute – even though the sdbNavigator shows the record has the second (correct) value.
Any idea what is happening, and how to fix?? Thanks.
SOLVED: The issue I was facing is that SDB allows multiple attributes with the same name in the same record (item), so it was picking up the first value I set for the attribute myField.
If you want to guarantee uniqueness, you have to set a flag on the replaceable attribute as follows:
and then call putAttributesRequest(), as above. Thanks.