I have the following code in which I query SimpleDb to get a list of the items in my domain along with the "Favorited" attribute. Basically I am trying to update the users database to have the most recent amount of times each item has been "Favorited".
itemNames[] works out perfectly. However, I am having trouble pulling out the attribute. I get to setting List Attributes which ends up being [{Name: Favorited, AlternateNameEncoding: null, Value: 5, AlternateValueEncoding: null, }]. From here though, I don't know how to pull out the Value attribute.
End result I want favoritedValues[i] = 5
Also, it is possible that I am going about this the difficult way, is there an easier way to do it?
private void updateFavorites() {
AmazonClientManager clientManager = new AmazonClientManager();
AmazonSimpleDBClient aSDbClient = clientManager.sdb();
String domainName = "domain";
SelectRequest selectRequest2 = new SelectRequest( "select Favorited from `" + domainName + "`" ).withConsistentRead( true );
List values = aSDbClient.select( selectRequest2 ).getItems();
String[] itemNames = new String[ values.size() ];
String[] favoritedValues = new String[ values.size()];
for ( int i = 0; i < values.size(); i++ ) {
itemNames[ i ] = ((Item)values.get( i )).getName();
List attributes = ((Item)values.get( i )).getAttributes();
favoritedValues[i] = No idea what goes here
}
}
The Dev guide wasn't able to help me. Every time I searched for information via google I got information on ListViews which wasn't what I was looking for.
You can loop on the attributes and find their names and values.
List<Item> values;
Item currentItem = buffer.get(i);
itemNames[i] = currentItem.getName();
List<Attribute> currentAttributes = currentItem.getAttributes();
String favorited = "";
for (Attribute anAttribute : currentAttributes) {
if (anAttribute.getName().equals("Favorited")) {
favorited = anAttribute.getValue();
}
}
Incidentally I find this syntax rather awkward. To get an attribute with a certain name you've to loop until you find it. By the way you can as well find it multiple times, because attributes can have multiple values. You can also find some other attributes in the middle, since attributes are not sorted.
To facilitate the process I wrote a small library AmazonSdbHelper, that allows a syntax like the following.
PersistenceInterface store = new AmazonSdbHelper();
store.setDomain("domain");
store.query("SELECT * FROM domain");
while (store.hasNext()) {
String key = store.next();
String address = store.getAttributeAsString("address");
Collection<String> phones = store.getAttributeAsCollection("phones");
int visits = store.getAttributeAsInt("visits");
}
Even better there is a project called SimpleJpa, that implements JPA for Amazon. I would like to try it, anyone has experience with it?