Home > Net >  How to do pagination with DynamoDBMapper?
How to do pagination with DynamoDBMapper?

Time:01-18

I'm developing an application in Quarkus that integrates with the DynamoDB database. I have a query method that returns a list and I'd like this list to be paginated, but it would have to be done manually by passing the parameters.

I chose to use DynamoDBMapper because it gives more possibilities to work with lists of objects and the level of complexity is lower.

Does anyone have any idea how to do this pagination manually in the function?

CodePudding user response:

At this point, you shouldn’t be using V1 of the AWS SDK for Jave. Look into upgrading to the latest version (AWS SDK for Java v2) and consider using the Enhanced Client.It’s always best to go with the latest version of the SDK.

CodePudding user response:

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()
        .withLimit(pageSize)
        .withExclusiveStartKey(paginationToken);
PaginatedScanList<YourModel> result = mapper.scan(YourModel.class, scanExpression);
String nextPaginationToken = result.getLastEvaluatedKey();

You can pass the pageSize and paginationToken as parameters to your query method. The nextPaginationToken can be returned along with the results, to be used for the next page.

CodePudding user response:

DynamoDB Mapper paginates by iterating over the results, by lazily loading the dataset:

By default, the scan method returns a "lazy-loaded" collection. It initially returns only one page of results, and then makes a service call for the next page if needed. To obtain all the matching items, iterate over the result collection.

Ref

For example:

List<Customer> result = mapper.scan(Customer.class, scanExpression);

for ( Customer cust : result ) {
    System.out.println(cust.getId());
}
  • Related