0
我有作爲身份證,VIN(車輛識別號碼)排序鍵作爲時間戳,我想執行查詢,並希望知道最新的記錄有最大時間戳(只有單記錄基於vin與最新的時間戳)。Dynamo Db與最新數據
我有作爲身份證,VIN(車輛識別號碼)排序鍵作爲時間戳,我想執行查詢,並希望知道最新的記錄有最大時間戳(只有單記錄基於vin與最新的時間戳)。Dynamo Db與最新數據
您可以使用Query
API與ScanIndexForward
爲假,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);
});
}
});
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;
}