使用ExclusiveStartKey选项进行AWS Dynamodb扫描

时间:2016-10-12 02:28:07

标签: node.js

对于我最近的项目,我正在尝试从dynamodb获取数据。似乎一切正常,除了我添加" exclusiveStartKey"我的参数选项。

以下是我的代码。

function scanDataFromDB(datetime) {
let params = {
    TableName: TABLE_NAME,
    IndexName: "main-index",
    Select: "ALL_ATTRIBUTES",
    ExclusiveStartKey: {
        "message_id": { "S": "20161011175258875925351560"}
    },
    ExpressionAttributeNames: {
        "#f_up": "date_updated"
    },
    ExpressionAttributeValues: {
        ":s_time": "2016-10-11 00:00:00",
        ":e_time": "2016-10-11 23:59:59"
    },
    FilterExpression: "#f_up between :s_time and :e_time",
    ScanIndexForward: "true"
};
console.log(params);
docClient.scan(params, function(err, data) {
    if(err) {
        console.log(JSON.stringify(err, null, 2));
        //callback(err, null);
    } else {
        console.log(JSON.stringify(data, null, 2));
        //callback(null, err);
    }
})

}

继续返回"提供的起始键无效。" 欢迎任何建议或帮助。

1 个答案:

答案 0 :(得分:7)

我发现了问题。我花了差不多一个星期。如果存在带分区键的排序键,则“ExclusiveStartKey”选项必须同时指示分区键和排序键。

function scanDataFromDB(datetime) {
let params = {
    TableName: TABLE_NAME,
    IndexName: "main-index",
    Select: "ALL_ATTRIBUTES",
    ExclusiveStartKey: {
        "message_id": "20161012114321726034249204",
        "date_updated": "2016-10-12 11:44:09"         
    },
    ExpressionAttributeNames: {
        "#f_up": "date_updated"
    },
    ExpressionAttributeValues: {
        ":s_time": "2016-10-11 00:00:00",
        ":e_time": "2016-10-11 23:59:59"
    },
    FilterExpression: "#f_up between :s_time and :e_time",
    ScanIndexForward: "true"
};
console.log(params);
docClient.scan(params, function(err, data) {
    if(err) {
        console.log(JSON.stringify(err, null, 2));
        //callback(err, null);
    } else {
        console.log(JSON.stringify(data, null, 2));
        //callback(null, err);
    }
})

}