我的发电机数据库表如下:
HashKey(xyz) ,RangeKey(timestamp)
现在,对于每个散列键,我都有一组范围键。
现在我想根据hashkey集进行查询,只想获取与之对应的最新值。我不想在内存中进行排序,然后选择最新版本。
我能用什么方法做到这一点吗?
用例是,我将做一个bulkget并传递一组hashkey(比如100(,所以我想为每个hashkey 获得一条记录
您(目前(无法对批获取设置约束。看见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次这个电话。
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;
}