DynamoDB中PaginatedQueryList与QueryResultPage的区别及Java使用疑问
Let me walk through each of your questions with practical insights from working with DynamoDBMapper:
1. Does QueryResultPage load the next page when iterating?
Nope, QueryResultPage is a single-page result set by design. It only loads the initial 1MB (or up to 1000 items, whichever comes first) of data, and iterating over it won't trigger any additional calls to DynamoDB. If you need to fetch subsequent pages, you have to manually use the getLastEvaluatedKey() from the current QueryResultPage to build a new DynamoDBQueryExpression (set the setExclusiveStartKey parameter) and call queryPage() again.
2. Can you get LastEvaluatedKey from PaginatedQueryList? Do you have to use QueryResultPage?
Short answer: You can't get LastEvaluatedKey directly from PaginatedQueryList via its public API. The whole point of PaginatedQueryList is to abstract away the pagination logic—it automatically fetches the next page in the background when you iterate past the initial 1MB of data. Since it hides the pagination details, it doesn't expose the LastEvaluatedKey to the caller.
If you explicitly need the LastEvaluatedKey (for example, to resume the query later from a specific point), then yes, you must use QueryResultPage. There are hacky workarounds like using reflection to access internal fields of PaginatedQueryList, but those are not recommended—they're fragile and could break with future SDK updates.
3. Does calling size() or using stream() on a PaginatedQueryList load all matching data?
First, note that when you assign PaginatedQueryList<Data> to a List<Data> variable, you're still holding a reference to the PaginatedQueryList instance (since it implements the List interface). Here's how the operations behave:
data.size(): This will absolutely load all matching data from DynamoDB. To calculate the total size,PaginatedQueryListhas to keep fetching pages until there's no more data (i.e., untilLastEvaluatedKeyis null). This can be expensive if your query matches a large number of items.stream(): It depends on the terminal operation you use with the stream:- If you use a short-circuit operation like
findFirst()oranyMatch(), it will only load the minimal number of pages needed to get the result. For example, if the first matching item is in the initial page, no additional calls are made. - If you use a terminal operation that processes all items (like
forEach(),count(), orcollect(Collectors.toList())), then it will load all matching data just likesize()does.
- If you use a short-circuit operation like
内容的提问来源于stack exchange,提问作者H Athukorala

