我正在将parition键作为ID,即VIN(车辆识别号)排序键作为时间戳,我想执行查询并想知道具有最大时间戳的最新记录(仅基于具有最新时间戳的vin的单个记录)
答案 0 :(得分:2)
您可以使用Query
API,ScanIndexForward
为false,Limit
等于1,以获得结果。
示例代码: -
var params = {
TableName : "yourtablename",
KeyConditionExpression: "#VIN = :vinvalue",
ExpressionAttributeNames:{
"#VIN": "VIN"
},
ExpressionAttributeValues: {
":vinvalue":"somevinvalue"
},
ScanIndexForward : false,
Limit : 1
};
docClient.query(params, function(err, data) {
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
} else {
console.log("Query succeeded.");
data.Items.forEach(function(item) {
console.log(" -", item.year + ": " + item.title);
});
}
});
答案 1 :(得分:0)
public EventLogEntity fetch(String vin) {
EventLogEntity eventLogEntityResult = null;
EventLogEntity entity = new EventLogEntity();
entity.setId(vin);
DynamoDBQueryExpression<EventLogEntity> queryExpression =
new DynamoDBQueryExpression<EventLogEntity>().withHashKeyValues(entity);
//DynamoDBQueryExpression Works only for Partition, Sort and Indexes in Dynamo Db
queryExpression.setScanIndexForward(false); // Will give you result in descending order
queryExpression.withLimit(1);// Will always give you single record
List<EventLogEntity> result = dynamoDBMapper.queryPage(EventLogEntity.class, queryExpression).getResults();
if (result != null && !result.isEmpty()) {
eventLogEntityResult = result.get(0);
}
return eventLogEntityResult;
}