2011-10-04 3 views
0

"Favorited"속성과 함께 내 도메인의 항목 목록을 가져 오기 위해 SimpleDb를 쿼리하는 다음 코드가 있습니다. 기본적으로 사용자 데이터베이스를 업데이트하여 각 항목이 "즐겨 찾기"된 가장 최근의 시간을 갖도록하려고합니다.Amazon Simple Db 목록에서 항목 가져 오기

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 
    } 
} 

Dev 가이드가 나를 도울 수 없었습니다. Google을 통해 정보를 검색 할 때마다 내가 찾던 정보가 아닌 ListView에 관한 정보가 있습니다.

답변

0

속성을 반복하여 이름과 값을 찾을 수 있습니다.

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"); 
} 

아마존에 대한 JPA를 구현 SimpleJpa라는 프로젝트는이 더 나은. 나는 그것을 시험해보고 싶다, 누군가는 그것에 경험이있다?

+0

당신의 최고의 솔루션은 다소 비효율적 인 것처럼 보입니다 ... 특히 많은 특성을 갖고 있으며 단지 하나만 잡으려고 할 때 특히 그렇습니다. 그게 유일한 방법입니까? 또한, 나는 (Attribute anAttribute : currentAttributes)에 익숙하지 않다 ... 설명 할 수 있겠는가? – easycheese

+0

속성 목록을 가지고 있으므로 찾아야합니다. 경우에 따라 루프를 찾을 수 있습니다. 그러나 비효율적이지는 않습니다. 특히 SimpleDB의 속도와 비교할 때 List의 루프가 매우 빠르게됩니다. 지루한 일이지만 내가했던 것처럼 당신을 위해이 일을하기위한 몇 가지 방법을 쓰는 것이 낫습니다. 루프는 2004 년 Java 1.5에서 소개 된 [foreach 루프] (http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work)라고합니다. – stivlo

+0

나는 그것을 시험해 보았습니다, 당신 말이 맞습니다. (적어도 작은 데이터베이스에서는) 오래 걸리지 않습니다. 그것은 나를 위해 일할 것이다, 고마워! – easycheese

관련 문제