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 value of the LastEvaluatedIndex
of previous request to get next set (logical page) of results.
I'm trying to implement the same but I'm using DynamoDBMapper
, which seems to have lot more advantages like tight coupling with data models. So if I wanted to do the above, I'm assuming I would do something like below:
// Mapping of hashkey of the last item in previous query operation
Map<String, AttributeValue> lastHashKey = ..
DynamoDBQueryExpression expression = new DynamoDBQueryExpression();
...
expression.setExclusiveStartKey();
List<Table> nextPageResults = mapper.query(Table.class, expression);
I hope my above understanding is correct on paginating using DynamoDBMapper. Secondly, how would I know that I've reached the end of results. From the docs if I use the following api:
QueryResult result = dynamoDBClient.query((QueryRequest) request);
boolean isEndOfResults = StringUtils.isEmpty(result.getLastEvaluatedKey());
Coming back to using DynamoDBMapper, how can I know if I've reached end of results in this case.
You have a couple different options with the
DynamoDBMapper
, depending on which way you want go.query
- returns aPaginatedQueryList
queryPage
- returns aQueryResultPage
scan
- returns aPaginatedScanList
scanPage
- returns aScanResultPage
The part here is understanding the difference between the methods, and what functionality their returned objects encapsulate.
I'll go over
PaginatedScanList
andScanResultPage
, but these methods/objects basically mirror each other.The
PaginatedScanList
says the following, emphasis mine: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 differentDynamoDBMapperConfig.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:
And the output: