dynamodb查询

时间:2013-02-26 09:20:59

标签: amazon-dynamodb

我的发电机数据库表格如下:

HashKey(xyz) ,RangeKey(timestamp)

现在,对于每个哈希键,我都设置了范围键。

现在我想基于一组hashkey进行查询,我只希望获取最新的值对应的内容。我不想在内存排序中然后选择最新版本。

我能以任何方式这样做吗?

用例是我将执行批量传递并传递一组hashkey(比如100),所以我想为每个hashkey获取一条记录

2 个答案:

答案 0 :(得分:0)

您(当前)无法对批量获取设置约束。见http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/API_BatchGetItems.html

但是,对于单个哈希键,您可以使用ScanIndexForward设置方向。有关信息,请参阅http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/API_Query.html

示例java代码:

new QueryRequest().withTableName("table-name")
  .withScanIndexForward(false)
  .withLimit(1)
  .withHashKeyValue(new AttributeValue().withS("hash-key"));

虽然效率不高,因为你需要拨打100次电话。

答案 1 :(得分:0)

Use ScanIndexForward(true for ascending and false for descending) and can also limit the result using setLimit value of Query Expression.

Please find below the code where used QueryPage for finding the single record.

public EventLogEntitySave fetchLatestEvents(String id) {
    EventLogEntitySave entity = new EventLogEntitySave();
    entity.setId(id);

    DynamoDBQueryExpression<EventLogEntitySave> queryExpression = new DynamoDBQueryExpression<EventLogEntitySave>().withHashKeyValues(entity);
    queryExpression.setScanIndexForward(false);
    queryExpression.withLimit(1);
    queryExpression.setLimit(1);

    List<EventLogEntitySave> result = dynamoDBMapper.queryPage(EventLogEntitySave.class, queryExpression).getResults();
    System.out.println("size of records = "+result.size() );
    result.get(0);
}

@DynamoDBTable(tableName = "PROD_EA_Test")
public class EventLogEntitySave {

        @DynamoDBHashKey
        private String id;
        private String reconciliationProcessId;
        private String vin;
        private String source;
}

public class DynamoDBConfig {
    @Bean
    public AmazonDynamoDB amazonDynamoDB() {

            String accesskey = "";
            String secretkey = "";
            //
            // creating dynamo client
            BasicAWSCredentials credentials = new BasicAWSCredentials(accesskey, secretkey);
            AmazonDynamoDB dynamo = new AmazonDynamoDBClient(credentials);
            dynamo.setRegion(Region.getRegion(Regions.US_WEST_2));
            return dynamo;
        }