【问题标题】:Can we insert in multiple tables using bigtable我们可以使用 bigtable 在多个表中插入吗
【发布时间】:2020-10-26 04:51:27
【问题描述】:
我有一个用例,我必须在 python 中使用 bigtable 将事件数据写入多个表中。在 bigtable 中是否有可能,或者我们不能在 bigtable 中做到这一点。
当我尝试在同一代码中的多个表中写入数据时,就会出现此问题。
google.cloud.bigtable.table.TableMismatchError:
请确认是否可以使用 bigtable。
【问题讨论】:
标签:
python
google-cloud-bigtable
【解决方案1】:
Bigtable 不支持写入多个表,因此您需要为每个表建立一个连接,然后按表对写入进行分组。你可以这样做:
table1 = instance.table(table_id1)
table2 = instance.table(table_id2)
timestamp = datetime.datetime.utcnow()
column_family_id = "stats_summary"
rows1 = [table.direct_row("tablet#a0b81f74#20190501"),
table.direct_row("tablet#a0b81f74#20190502")]
rows1[0].set_cell(...)
rows1[0].set_cell(...)
rows1[1].set_cell(...)
rows1[1].set_cell(...)
rows2 = [table.direct_row("tablet#20190501#a0b81f74"),
table.direct_row("tablet#20190502#a0b81f74")]
rows2[0].set_cell(...)
rows2[0].set_cell(...)
rows2[1].set_cell(...)
rows2[1].set_cell(...)
response1 = table1.mutate_rows(rows)
response2 = table2.mutate_rows(rows)
还有更多关于如何执行各种writes in the Bigtable documentation 的示例,因此一旦您创建了其他 Bigtbale 表连接,您就可以按照这些示例进行操作。