【发布时间】:2021-07-27 07:54:28
【问题描述】:
我在<name>#<date>#<id_value> 中创建了一个带有行键的大表
当我使用如下的行键前缀进行过滤时,我想获得唯一的 ID。
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
prefix = "phone#20190501"
end_key = prefix[:-1] + chr(ord(prefix[-1]) + 1)
# example row keys = ['phone#20190501#<id_value>', 'phone#20190501#<id_value>'...]
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)
id_values = []
for row in rows:
# get last id_value from row key
id_value = str(row.key).replace('phone#20190501#', '')
id_values.append(id_value)
unique_id_list = list(set(id_values))
print('COUNT: %s' % len(unique_id_list))
但是,我想知道如果我读取超过 1 亿行,我认为这种计算唯一 id_value 的方式可能会占用大量内存和 cpu。
有没有更好的方法来计算 Bigtable 中的唯一 ID 或标准 SQL 中的“UNIQUE”之类的函数
【问题讨论】:
标签: google-cloud-bigtable bigtable