skip to Main Content

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?

2

Answers


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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search