Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Dynamodb scan using ExclusiveStartKey option

Tags:

node.js

For my recent project, I am trying to get data from dynamodb. And it seems everything working fine except I add "exclusiveStartKey" option to my parameters.

Below is my code.

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);
    }
})

}

This keep returning "The provided starting key is invalid." Any advice or help is welcome.

like image 386
Hyunseo Kang Avatar asked Oct 12 '16 02:10

Hyunseo Kang


1 Answers

I found out the problem. It took me almost a week. If there is a sort key with partition key, "ExclusiveStartKey" Options must indicate both partition key and sort key.

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);
    }
})

}

like image 193
Hyunseo Kang Avatar answered Oct 29 '22 23:10

Hyunseo Kang