Skip to content

Paginate response on DynamoDB-API Gateway with ExclusiveStartKey

0

I'm building an API that retrieves a batch of records from my Dynamo DB. I'm using Scan action.

My request has a FilterExpression around the field "wb_date". If the request is too large, the API response will show the parameter LastEvaluatedKey which allows me to make a subsequent request to retrieve another batch of data.

This is the mapping request:

{
    "TableName": "Test",
    "Limit": 10000,
    "FilterExpression": "wb_date = :val",
    "ExpressionAttributeValues": {
        ":val": {
            "S": "$input.params('wb_date')"
        }
    },
    "ExclusiveStartKey": {
        "salesforce_id": {
            "S": "$input.params('salesforce_id')"
        },
        "type": {
            "S": "$input.params('type')"
        }
    },
    "ReturnConsumedCapacity": "TOTAL"
}

My issue is: I don't know how to make the first request since the API is forcing me to pass something on the ExclusiveStartKey fields (salesforce_id and type)

I wish I could pass 0 on both parameters https://i.imgur.com/ti9KhgA.png. It works but the Scan does not starts on the first record. The Scan seems to start far away from my 1st record.

Is there a wildcard or a workaround to make this first API call go and Scan from the beginning?

Thank you in advance

2 Answers
0
Accepted Answer

Found the Answer after iterating with ChatGpt.

I had to rewrite my mapping request with an If clause.

If salesforce_id = 0, send the request with no ExclusiveStartKey field. Else, send the request with the ExclusiveStartKey fielda.

{
    "TableName": "Test-Table",
    "Limit": 100,
    "FilterExpression": "wb_date = :val",
    "ExpressionAttributeValues": {
        ":val": {
            "S": "$input.params('wb_date')"
        }
    },
    #if($input.params('salesforce_id') == "0")
        "ReturnConsumedCapacity": "TOTAL"
    #else
        "ExclusiveStartKey": {
            "salesforce_id": {
                "S": "$input.params('salesforce_id')"
            },
            "type": {
                "S": "$input.params('type')"
            }
        },
        "ReturnConsumedCapacity": "TOTAL"
    #end
}


answered 2 years ago
0

ExclusiveStartKey isn't a required parameter in the request structure so you should be able to omit it entirely for your initial request.

Reference: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#DDB-Query-request-ExclusiveStartKey

AWS
EXPERT
answered 2 years ago
  • Thank you, but did not worked for me. If I ommit the ExclusiveStartKey in the mapping request the request will not take it into account once I need it.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.