我想扫描一个大表以获取 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_start
和 row_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 aRowFilter
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/