jamiet
jamiet

Reputation: 12234

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

I'm following Google's advice for writing a single row of data to Bigtable and then reading it back again.

Hence my code looks like this:

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")

When I run it, this is the output I get:

Successfully wrote row phone#4c410523#20190501.
None

Its the None that is bothering me. Given I am reading/writing the same row_key I would expect to get a row back, but it seems I am not and I don't know why. Can anyone advise?

Upvotes: 0

Views: 308

Answers (1)

Robertocd_98
Robertocd_98

Reputation: 404

Looking at your code I think that the issue of why when you read the response is None is because you are not actually writing in your table.

You need to check what is your column_family_id so you can write the information in the table that you created, for example I made a table for testing and my column_family_id was nf1 and not stats_summary.

Best regards.

Upvotes: 1

Related Questions