如何解决无法使用 Google 的规范示例从 Bigtable 读取数据
我遵循 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","os_build","PQ2A.190405.003",timestamp)
row.commit()
print('Successfully wrote row {}.'.format(row_key))
def read_row(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,我希望得到一行,但似乎我不是,我不知道为什么。谁能给点建议?
解决方法
查看您的代码,我认为为什么当您阅读响应时 None
的问题是因为您实际上并没有在表格中写入。
您需要检查您的 column_family_id
是什么,以便您可以将信息写入您创建的表中,例如我制作了一个用于测试的表,而我的 column_family_id
是 nf1
和不是stats_summary
。
最好的问候。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。