Dynamo Db与最新数据

时间:2017-05-29 09:01:15

标签: amazon-dynamodb

我正在将parition键作为ID,即VIN(车辆识别号)排序键作为时间戳,我想执行查询并想知道具有最大时间戳的最新记录(仅基于具有最新时间戳的vin的单个记录)

2 个答案:

答案 0 :(得分:2)

您可以使用Query API,ScanIndexForward为false,Limit等于1,以获得结果。

  • ScanIndexForward = false - >表示按降序排列排序键 为了
  • 限制= 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;
            }
相关问题