amazon dynamodb -在Java中查询带有动态属性的Dynamo表



我想查询我的Dynamo表来检索具有未知属性键值的项目。我能够成功地使用DynamoDBMapper类,它使用带注释的类从数据库中获取值,但是使用此方法,我需要指定将提前提取的所有属性。

我尝试遵循使用Dynamo SDK for Java演练的指南http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html。下面的代码是我想到的,使用与站点上相同的Import库:

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

我甚至不确定它是如何编译的,但我想这是一个不同的问题。这些方法已被删除,还是我只是使用错误?

试着这样想,对我来说这个工作:

    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;

相关内容

  • 没有找到相关文章

最新更新