DynamoDB sort and pagination

Viewed 1611

I'm trying to get data from a dynamoDB table sorted but also I want pagination because there are many many rows. This is my query params, control_id is GSI with timestamp as sortkey and id is primary key. I think what I need is to use both indexes in same query because right now this is what I get ValidationException: The provided starting key is invalid and if I remove ExclusiveStartKey it works.

params = {
 TableName: tableName,
 IndexName: 'control_id',
 KeyConditionExpression: 'control_id = :ci',
 ExpressionAttributeValues: {
  ':ci': 1
 },
 ScanIndexForward: false,
 Limit: 250,
 ExclusiveStartKey: {
  'id': lastKey
 }
}
1 Answers

If you you have both a partition key and sort key as your primary key, you need to supply both to the ExclusiveStartKey. Dynamo gives you two options when building a primary key, a partition key, or a composite key (made up of the partition key and sort key). When using a composite key, always need to provide both the partition key and sort key when referencing a single record in the table

From the docs (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/API_Query_v20111205.html) -

ExclusiveStartKey - Primary key of the item from which to continue an earlier query. An earlier query might provide this value as the LastEvaluatedKey if that query operation was interrupted before completing the query; either because of the result set size or the Limit parameter. The LastEvaluatedKey can be passed back in a new query request to continue the operation from that point.

Type: HashKeyElement, or HashKeyElement and RangeKeyElement for a composite primary key.

here, HashKeyElement refers to the partition key, and RangeKeyElement refers to the sort key

try

ExclusiveStartKey: {
  'id': lastKey
  'created_at': <last key's created_at value>
 }

or it might actually be -

ExclusiveStartKey: lastkey

depending on what you have as lastKey, it might already be the composite key

Related