How can I retrieve all items from the paginated result of a DynamoDBMapper query()?
amazon-dynamodb, amazon-web-services, java
Solution
The Java code snippet within the Amazon DynamoDB documentation for the DynamoDBMapper Class is a bit unfortunate here (though technologically correct), the AWS SDK for Java API documentation for Class DynamoDBMapper is (naturally) more precise in this regard, see method query():
public <T> PaginatedQueryList<T> query(Class<T> clazz,
DynamoDBQueryExpression queryExpression)
So the returned type is actually a Class PaginatedQueryList:
Implementation of the List interface that represents the results from a query 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. [emphasize mine]
That is, you really do not need to explicitly load anything during normal usage, insofar it is implicitly taken care of by the lazy-loading implementation of `PaginatedQueryList<T>`; however, if so desired for whatever reason, you can trigger it by operations requiring access to the entire collection, with the explicitly mentioned size() method being one of them apparently.
Problem
I am trying to query DynamoDB with help of DynamoDBMapper in Java with both hashKey and rangeKey. But I am not getting all results, it returns only some part of it. My code looks like: ``` queryDynamoDb() { Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.GT.toString()) .withAttributeValueList(new AttributeValue().withS("0")); DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression( new AttributeValue().withS(prefKey)); queryExpression.setRangeKeyCondition(rangeKeyCondition); List<MyObj> myobjs = mapper.query(MyObj.class, queryExpression); return myobjs; } ``` `MyObj` is properly annotated with DynamoDB annotations. So I am able to save the objects, but retrieval returns a partial result only. The documentation of query within DynamoDBMapper says: The query method returns the "lazy-loaded" collection. That is, initially it returns only one page of results. It makes a service call for the next page when needed. Now the question is, how to tell the mapper to make a service call or that a page is needed, so it loads all pages (effectively all entries)?