【发布时间】:2021-07-05 16:24:28
【问题描述】:
我遵循 Google 对writing a single row of data to Bigtable 和reading it back again 的建议。
因此我的代码如下所示:
import datetime
from google.cloud import bigtable
def write_simple(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
timestamp = datetime.datetime.utcnow()
column_family_id = "stats_summary"
row_key = "phone#4c410523#20190501"
row = table.direct_row(row_key)
row.set_cell(column_family_id,
"connected_cell",
1,
timestamp)
row.set_cell(column_family_id,
"connected_wifi",
1,
timestamp)
row.set_cell(column_family_id,
"os_build",
"PQ2A.190405.003",
timestamp)
row.commit()
print('Successfully wrote row {}.'.format(row_key))
def read_row(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
row_key = "phone#4c410523#20190501"
row = table.read_row(row_key)
print(row)
def print_row(row):
print("Reading data for {}:".format(row.row_key.decode('utf-8')))
for cf, cols in sorted(row.cells.items()):
print("Column Family {}".format(cf))
for col, cells in sorted(cols.items()):
for cell in cells:
labels = " [{}]".format(",".join(cell.labels)) \
if len(cell.labels) else ""
print(
"\t{}: {} @{}{}".format(col.decode('utf-8'),
cell.value.decode('utf-8'),
cell.timestamp, labels))
print("")
write_simple(
project_id="msm-groupdata-datalake-dev",
instance_id="jamiet-dp-tf-instance",
table_id="user-agent")
read_row(
project_id="myproject",
instance_id="myinstance",
table_id="mytable")
当我运行它时,这是我得到的输出:
成功写行电话#4c410523#20190501。
无
困扰我的是无。鉴于我正在读/写相同的 row_key,我希望得到一行回来,但似乎我不是,我不知道为什么。谁能给点建议?
【问题讨论】:
标签: python google-cloud-bigtable bigtable