我有以下代码,在其中查询 SimpleDb 以获取我的域中的项目列表以及“Collection 夹”属性。基本上我正在尝试更新用户数据库以获取每个项目被“Collection ”的最近次数。
itemNames[] 效果非常好。但是,我在提取该属性时遇到问题。我开始设置列表属性,最终成为[{Name: favorited, AlternateNameEncoding: null, Value: 5, AlternateValueEncoding: null, }]。但从这里开始,我不知道如何提取 Value 属性。
我想要的最终结果favoritedValues[i] = 5
另外,我可能会以困难的方式来解决这个问题,有更简单的方法吗?
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
}
}
开发指南无法帮助我。每次我通过 google 搜索信息时,我都会得到有关 ListViews 的信息,但这不是我想要的信息。
最佳答案
您可以循环访问属性并查找它们的名称和值。
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();
}
}
顺便说一句,我发现这种语法相当尴尬。要获取具有特定名称的属性,您必须循环直到找到它。顺便说一句,您也可以多次查找它,因为属性可以有多个值。您还可以在中间找到一些其他属性,因为属性未排序。
为了方便这个过程,我编写了一个小库 AmazonSdbHelper ,允许使用如下语法。
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");
}
更好的是,有一个名为 SimpleJpa 的项目,它为 Amazon 实现了 JPA。我想尝试一下,有人有经验吗?
关于java - Amazon Simple Db 从列表中获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7647238/