0

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?

ner
  • 173
  • 1
  • 1
  • 9
  • DynamoDBMapper is part of AWS SDK for Java V1 which is marked Depreciated on the AWS SDK page here: https://github.com/awsdocs/aws-doc-sdk-examples. Using this API is not best practice. – smac2020 Jan 17 '23 at 19:17
  • 1
    Interestingly, https://docs.aws.amazon.com/sdkref/latest/guide/version-support-matrix.html shows it as not deprecated – hunterhacker Jan 17 '23 at 23:18

2 Answers2

0
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.

Romano
  • 57
  • 1
  • 8
0

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

To Scan manually page by page you can use ScanPage

final DynamoDBScanExpression scanPageExpression = new DynamoDBScanExpression()
        .withLimit(limit);
do {
    ScanResultPage<MyClass> scanPage = mapper.scanPage(MyClass.class, scanPageExpression);
    scanPage.getResults().forEach(System.out::println);
    System.out.println("LastEvaluatedKey=" + scanPage.getLastEvaluatedKey());
    scanPageExpression.setExclusiveStartKey(scanPage.getLastEvaluatedKey());

} while (scanPageExpression.getExclusiveStartKey() != null);

Ref Ref

Leeroy Hannigan
  • 11,409
  • 3
  • 14
  • 31
  • Can you answer me if it's possible with DynamoDBScanExpression or another class I can manually type current page, page size and do sorting? Example: `.page(page).pageSize(pageSize) .totalPage((long) responseEntityList.size())).totalItems((long) responseEntityList.size()).build();` – ner Jan 19 '23 at 13:14