1

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.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
easycheese
  • 5,859
  • 10
  • 53
  • 87

1 Answers1

2

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?

stivlo
  • 83,644
  • 31
  • 142
  • 199
  • Your top solution seems to be rather inefficient...especially if you have a lot of attributes and you are only trying to pull one. Is that really the only way? Also, I'm not familiar with for (Attribute anAttribute : currentAttributes)...could you explain? – easycheese Oct 04 '11 at 15:48
  • Since you've a List of Attribute you've to walk it to find one. You can break out of the loop when you find one, in case. It's not inefficient, though, loop on a List is blazingly fast, especially compared to the speed of SimpleDB. It's tedious though and is better that you write some method to do this for you like I did. As for the loop, is called [foreach loop](http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work) that was introduced in Java 1.5 in 2004. – stivlo Oct 04 '11 at 15:57
  • I tested it out, you are right, it doesn't take long (at least for a small database). It will work for me, thanks! – easycheese Oct 05 '11 at 06:05