Pagination with DynamoDBMapper Java AWS SDK

前端 未结 1 1701
南笙
南笙 2020-12-04 15:57

From the API docs dynamo db does support pagination for scan and query operations. The catch here is to set the ExclusiveStartIndex of current request to the va

相关标签:
1条回答
  • 2020-12-04 16:37

    You have a couple different options with the DynamoDBMapper, depending on which way you want go.

    • query - returns a PaginatedQueryList
    • queryPage - returns a QueryResultPage
    • scan - returns a PaginatedScanList
    • scanPage - returns a ScanResultPage

    The part here is understanding the difference between the methods, and what functionality their returned objects encapsulate.

    I'll go over PaginatedScanList and ScanResultPage, but these methods/objects basically mirror each other.

    The PaginatedScanList says the following, emphasis mine:

    Implementation of the List interface that represents the results from a scan in AWS DynamoDB. Paginated results are loaded on demand when the user executes an operation that requires them. Some operations, such as size(), must fetch the entire list, but results are lazily fetched page by page when possible.

    This says that results are loaded as you iterate through the list. When you get through the first page, the second page is automatically fetched with out you having to explicitly make another request. Lazy loading the results is the default method, but it can be overridden if you call the overloaded methods and supply a DynamoDBMapperConfig with a different DynamoDBMapperConfig.PaginationLoadingStrategy.

    This is different from the ScanResultPage. You are given a page of results, and it is up to do deal with the pagination yourself.

    Here is quick code sample showing an example usage of both methods that I ran with a table of 5 items using DynamoDBLocal:

    final DynamoDBMapper mapper = new DynamoDBMapper(client);
    
    // Using 'PaginatedScanList'
    final DynamoDBScanExpression paginatedScanListExpression = new DynamoDBScanExpression()
            .withLimit(limit);
    final PaginatedScanList<MyClass> paginatedList = mapper.scan(MyClass.class, paginatedScanListExpression);
    paginatedList.forEach(System.out::println);
    
    System.out.println();
    // using 'ScanResultPage'
    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);
    

    And the output:

    MyClass{hash=2}
    MyClass{hash=1}
    MyClass{hash=3}
    MyClass{hash=0}
    MyClass{hash=4}
    
    MyClass{hash=2}
    MyClass{hash=1}
    LastEvaluatedKey={hash={N: 1,}}
    MyClass{hash=3}
    MyClass{hash=0}
    LastEvaluatedKey={hash={N: 0,}}
    MyClass{hash=4}
    LastEvaluatedKey=null
    
    0 讨论(0)
提交回复
热议问题