本文介绍了使用 DynamoDBMapper Java AWS 开发工具包进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 API docs 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.

推荐答案

DynamoDBMapper,取决于你想走的路.

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

这里的部分是理解方法之间的区别,以及它们返回的对象封装了什么功能.

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

我将介绍 PaginatedScanListScanResultPage,但这些方法/对象基本上是相互镜像的.

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

PaginatedScanList 说明如下,强调我的:

The PaginatedScanList says the following, emphasis mine:

实现表示 AWS DynamoDB 中扫描结果的 List 接口.当用户执行需要它们的操作时,会按需加载分页结果. 某些操作,例如 size(),必须获取整个列表,但结果会尽可能地逐页获取.

这表示在您遍历列表时会加载结果.当您浏览第一页时,会自动获取第二页,而无需您明确提出另一个请求.延迟加载结果是默认方法,但如果您调用重载方法并为 DynamoDBMapperConfig 提供不同的 DynamoDBMapperConfig.PaginationLoadingStrategy.

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 you to 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 开发工具包进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 17:15