boto3分页:按照确切的数字查询页面内容

时间:2017-11-13 13:21:41

标签: python pagination amazon-dynamodb boto3

尝试使用boto get_paginator进行query操作来实现分页。我找不到正确的方法,如果没有加载前两页的内容,可以说是第3页的页面:

import boto3
from boto3.dynamodb.conditions import Key, Attr

dynamodb = boto3.resource("dynamodb", region_name="us-west-2")
paginator = dynamodb.meta.client.get_paginator("query")

pages = paginator.paginate(TableName="results",
                           IndexName="year-timestamp-index",
                           KeyConditionExpression=Key("year").eq("2017"),      
                           PaginationConfig={"PageSize": 50})

def get_content_of_page(requested_page_num, pages):
  pages_counter = 0
  for page in pages:
    if pages_counter == requested_page_num - 1:
      return page.get("Items")
    pages_counter += 1

get_content_of_page(pages, 3) # will load and skip contents of
                              # 2 pages and return content of 3rd

我认为它太贵了,是否有可能直接获得第3页?

像Mongo的cursor.skip(page_size * (requested_page_num - 1)).limit(page_size)

一样

0 个答案:

没有答案