python - 如何使用单个 API 调用扫描 HappyBase 中的行集?

标签 python google-cloud-platform bigtable happybase

我想扫描一个大表以获取 ID 列表(或 ID 前缀)(使用 Python HappyBase)。

有什么办法可以在服务器端实现吗?也就是说,我想在一次 API 调用中发送要扫描的开始/停止行列表,而不是执行一长串 API 调用。

这是一个例子。对于 my_big_tables 键:

2019/1
2019/2
2019/3
...
2020/1
2020/2
2020/3
2020/4
..

在一个查询中,我想获取所有年份第 1 个月和第 2 个月的所有记录。结果应该是:

2019/1
2019/2
2020/1
2020/2

最佳答案

与在 Table.scan() 中使用 row_startrow_stop 参数相比,这可能更适合使用 filter 参数正则表达式。

请参阅API reference有关过滤器参数的详细信息:

The keyword argument filter is also supported (beyond column and row range filters supported here). HappyBase / HBase users will have used this as an HBase filter string. (See the Thrift docs for more details on those filters.) However, Google Cloud Bigtable doesn’t support those filter strings so a RowFilter should be used instead.

RowFilter 是 Google Bigtable 库提供的一种类型。 Here are the docs 。假设您引用的 ID 字段是您的行键,我们可以使用 RowKeyRegexFilter按您描述的模式过滤 ID。

我们首先提出一个正则表达式来匹配所需月份的 ID 列表。例如,如果您想过滤 12 月和 1 月的基于年份的 ID,则可以使用此选项(请注意,您必须从最大数字到最短数字) - 请参阅 this link测试正则表达式:

\d\d\d\d\/(12|1)

这里尝试编写一个函数,该函数使用适当的过滤器创建 Google Bigtable HappyBase 扫描调用,其中 table 是 HappyBase 表,months 是整数列表。请注意,我没有测试过这段代码,但希望它至少为您提供了一个起点。

from google.cloud.bigtable.row_filters import RowKeyRegexFilter

def filter_by_months(table, months):
    months_reversed = sorted(months, reverse=True)
    months_strings = [str(month) for month in months_reversed]
    months_joined = "|".join(months_strings)

    key_filter = RowKeyRegexFilter('\d\d\d\d\/({})'.format(months_joined))
    return table.scan(filter=key_filter)

关于python - 如何使用单个 API 调用扫描 HappyBase 中的行集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61461759/

相关文章:

python - 如何使用tix.DirSelectDialog?

python - groupby pandas 期间的异常

python - 惰性延迟列表达到最大递归深度

bigtable - Bigtable 如何处理 Tablet Server 故障?

python - 文本预处理+Python+CSV : Removing special characters from a column of a CSV

docker - 如何将 docker 镜像从私有(private)第三方注册表迁移到 Google Artifactory 注册表?

java - Spring Boot 中的 Google 云部署错误 - 部署失败 : Non zero exit: 1

node.js - 无法使用 NodeJS API 将元数据附加到 Google Cloud Storage

python - python 的可移植 GAE bigtable 抽象?

node.js - 试图在 bigtable 中模拟单元级 TTL,但整个列族数据被垃圾收集删除