【问题标题】:how to do pagination in python bigtable如何在 python bigtable 中进行分页
【发布时间】:2021-02-06 11:41:45
【问题描述】:

我正在尝试在 bigtable 中使用 python 进行分页,同时读取 TB 的数据,但没有任何想法。能否请您帮忙,或者可以附上bigtable中python分页的示例代码。

【问题讨论】:

    标签: python-3.x google-cloud-bigtable


    【解决方案1】:

    您可以像这样执行scan over your table,read_rows 将为您提供一个迭代器:

    def read_prefix(project_id, instance_id, table_id):
        client = bigtable.Client(project=project_id, admin=True)
        instance = client.instance(instance_id)
        table = instance.table(table_id)
        prefix = "phone#"
        end_key = prefix[:-1] + chr(ord(prefix[-1]) + 1)
    
        row_set = RowSet()
        row_set.add_row_range_from_keys(prefix.encode("utf-8"),
                                        end_key.encode("utf-8"))
    
        rows = table.read_rows(row_set=row_set)
        for row in rows:
            print_row(row)
    
    

    read_rows 返回一个处理重试的PartialRowsData 对象,因此如果您的操作需要一定数量的项目,您可以像这样在 for 循环中添加一个计数器,它应该具有与分页相同的效果:

    count = 0
    page_size = 10
    for row in rows:
        print_row(row)
        count++
        if count % page_size == 0:
            # Do your action based on page size
    

    还有更多例子展示

    【讨论】:

      猜你喜欢
      • 2020-04-02
      • 2015-04-09
      • 1970-01-01
      • 2019-04-23
      • 2019-08-17
      • 2017-10-03
      • 2017-01-20
      • 2012-06-04
      • 2014-05-08
      相关资源
      最近更新 更多