2017-05-29 97 views
0

我有作爲身份證,VIN(車輛識別號碼)排序鍵作爲時間戳,我想執行查詢,並希望知道最新的記錄有最大時間戳(只有單記錄基於vin與最新的時間戳)。Dynamo Db與最新數據

回答

2

您可以使用Query API與ScanIndexForward爲假,Limit等於1來實現結果。

  • ScanIndexForward =假 - >裝置以降序 順序排列的排序鍵
  • 極限= 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); 
     }); 
    } 
}); 
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; 
      }