DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

3 1 1

[20 Days of DynamoDB] Day 11 - Using pagination with Query API

Posted: 22/Jan/2024

The Query API returns the result set size to 1 MB. Use ExclusiveStartKey and LastEvaluatedKey elements to paginate over large result sets. You can also reduce page size by limiting the number of items in the result set, with the Limit parameter of the Query operation.

Here is an example:

func paginatedQuery(searchCriteria string, pageSize int32) {

    currPage := 1
    var exclusiveStartKey map[string]types.AttributeValue

    for {
        resp, _ := client.Query(context.Background(), &dynamodb.QueryInput{
            TableName:              aws.String(tableName),
            KeyConditionExpression: aws.String("ForumName = :name"),
            ExpressionAttributeValues: map[string]types.AttributeValue{
                ":name": &types.AttributeValueMemberS{Value: searchCriteria},
            },
            Limit:             aws.Int32(pageSize),
            ExclusiveStartKey: exclusiveStartKey,
        })

        if resp.LastEvaluatedKey == nil {
            return
        }
        currPage++
        exclusiveStartKey = resp.LastEvaluatedKey
    }
}
Enter fullscreen mode Exit fullscreen mode

Recommended reading - Query Pagination

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay