使用 Google 的规范示例无法从 Bigtable 读取数据

Failed to read data from Bigtable using Google's canonical example

我正在遵循 Google 对 writing a single row of data to Bigtable and then 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")

当我 运行 它时,这是我得到的输出:

Successfully wrote row phone#4c410523#20190501.
None

困扰我的是None。考虑到我 reading/writing 和 row_key 一样,我希望能重新获得一排,但似乎我不是,我也不知道为什么。谁能给点建议?

查看您的代码,我认为为什么当您阅读响应时 None 的问题是因为您实际上并没有在 table.

中写入

你需要检查你的 column_family_id 是什么,这样你就可以在你创建的 table 中写入信息,例如我制作了一个 table 用于测试和我的 column_family_idnf1 而不是 stats_summary

此致。