使用 Boto 在 Amazon DynamoDB 中进行分页

2022-01-15 00:00:00 python boto amazon-dynamodb

问题描述

如何使用 Boto python 库对来自 DynamoDB 的结果进行分页?从 Boto API 文档中,我无法确定它是否支持分页,尽管 DynamoDB API 确实支持分页.

How do I paginate my results from DynamoDB using the Boto python library? From the Boto API documentation, I can't figure out if it even has support for pagination, although the DynamoDB API does have pagination support.


解决方案

Boto 确实支持使用ExclusiveStartKey"和Limit"组合的分页"行为.例如,对 Scan 进行分页.

Boto does have support for "pagination" like behavior using a combination of "ExclusiveStartKey" and "Limit". For example, to paginate Scan.

这是一个应该按 10 个块解析整个表的示例

Here is an example that should parse a whole table by chunks of 10

esk = None

while True:
    # load this batch
    scan_generator = MyTable.scan(max_results=10, exclusive_start_key=esk)

    # do something usefull
    for item in scan_generator:
        pass  # do something usefull
    # are we done yet ?
    else:
        break;

    # Load the last keys
    esk = scan_generator.kwargs['exclusive_start_key'].values()

正如@garnaat 所指出的,我可能误解了您的实际目标.上述建议允许您提供分页,例如 SO 对问题所做的那样.每页不超过 15 个.

As pointed out by @garnaat, it is possible that I misunderstood your actual goal. The above suggestion allows you to provide pagination like SO does for questions for example. No more than 15 per pages.

如果您只需要一种方法来加载由给定 Scan 生成的整个结果集,Boto 是一个很棒的库,并且已经为您抽象了它,而无需像我的回答中那样使用黑魔法.在这种情况下,您应该遵循他 (@garnaat) 的建议.顺便说一句,他是 Boto 的作者,因此是 Boto 相关问题的一个很好的参考:)

If you just need a way to load the whole result set produced by a given Scan, Boto is a great library and already abstracts this for you with no need for black magic like in my answer. In this case, you should follow what he (@garnaat) advises. Btw, he is the author of Boto and, as such, a great reference for Boto related questions :)

相关文章