2015-06-10 34 views
1

我想查詢我的Dynamo表以檢索具有未知屬性鍵值的項目。我能夠成功地使用DynamoDBMapper類,該類使用帶註釋的類從數據庫獲取值,但使用此方法,我需要指定將提前提取的所有屬性。使用Java中的動態屬性查詢Dynamo表

我試圖按照使用Dynamo SDK for Java演練的指南 http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html。下面的代碼是什麼,我想出了,用同樣的導入庫的網站上:

public static void queryTable(){ 
    Table table = dynamoDB.getTable("Task"); 

    String datekey = "20150601"; 

    QuerySpec spec = new QuerySpec() 
      .withKeyConditionExpression("Date = :v_id") 
      .withValueMap(new ValueMap() 
        .withString(":v_date", datekey)); 

    ItemCollection<QueryOutcome> items = table.query(spec); 

    Iterator<Item> iterator = items.iterator(); 
    while (iterator.hasNext()) { 
     System.out.println(iterator.next().toJSONPretty()); 
    } 
} 

在執行過程中出現了錯誤:

java.lang.NoSuchMethodError: com.amazonaws.services.dynamodbv2.model.QueryRequest.withKeyConditionExpression 

切換到爲我工作的方法與映射器,QuerySpec().withHashKey("Date",datekey)給出了類似的錯誤:

java.lang.NoSuchMethodError: com.amazonaws.services.dynamodbv2.model.QueryRequest.withExpressionAttributeNames 

我什至不知道它是如何編制,但我想這是一個不同的問題。有沒有這些方法被刪除或我只是使用它們錯了?

+0

你使用最新的SDK庫? KeyConditionExpression是新的。 –

+0

我正在使用1.9.40,它看起來像從發行說明中包含它。更新到最新(1.10.0)給出了相同的結果 – aaron

回答

1

嘗試somethink這樣,對於我這種工作:

KeyAttribute keyAttribute = new KeyAttribute("hashColumnName", "hash"); 
    Table table = dynamoDB.getTable(T_TABLE_NAME); 
    Index index = table.getIndex("end_date"); // index name 
    RangeKeyCondition rangeKeyCondition = new RangeKeyCondition("end_date").between(dateStart, dateEnd); 

    QuerySpec querySpec = new QuerySpec() 
     .withConsistentRead(true) 
     .withScanIndexForward(true) 
     .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL) 
     .withHashKey(keyAttribute) 
     .withRangeKeyCondition(rangeKeyCondition) 
     .withProjectionExpression 
      ("id, end_date "); 

    ItemCollection<QueryOutcome> items = index.query(querySpec); 
    Iterator<Item> iterator = items.iterator(); 
    ArrayList<TDynamoEntity> tDynamoEntities = new ArrayList<>(); 
    while (iterator.hasNext()) { 
     Item item= iterator.next(); 
     TDynamoEntity tDynamoEntity = new TDynamoEntity(item); //special constructor 
     tDynamoEntities.add(tDynamoEntity); 
    } 
    return tDynamoEntities;