问题描述
从API文档开始,dynamo db确实支持分页进行扫描和查询操作.这里的要点是将当前请求的ExclusiveStartIndex
设置为上一个请求的LastEvaluatedIndex
的值,以获取下一个结果集(逻辑页面).
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.
我正在尝试实现相同的功能,但是我正在使用DynamoDBMapper
,它似乎具有很多优势,例如与数据模型紧密耦合.因此,如果我想执行上述操作,我假设我将执行以下操作:
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);
我希望以上理解对使用DynamoDBMapper进行分页是正确的.其次,我怎么知道我已经达到了结果的终点.从文档中,如果我使用以下api:
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());
回到使用DynamoDBMapper的方式,我如何知道在这种情况下是否已达到结果的终点.
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.
我将介绍PaginatedScanList
和ScanResultPage
,但是这些方法/对象基本上是相互镜像的.
I'll go over PaginatedScanList
and ScanResultPage
, but these methods/objects basically mirror each other.
PaginatedScanList
指出以下内容,重点是我的内容:
The PaginatedScanList
says the following, emphasis mine:
这表示在您遍历列表时将加载结果.当您浏览第一页时,将自动获取第二页,而无需显式发出另一个请求.延迟加载结果是默认方法,但是如果调用重载方法并为DynamoDBMapperConfig
提供不同的.
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
.
这与ScanResultPage
不同.您会得到一页结果,这将由您自己来处理分页.
This is different from the ScanResultPage
. You are given a page of results, and it is up to do deal with the pagination yourself.
这里是一个快速的代码示例,显示了我使用DynamoDBLocal在5个项目的表中同时运行这两种方法的示例:
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);
输出:
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
这篇关于使用DynamoDBMapper Java AWS开发工具包分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!