Example: Python "Hello World" Application via the HappyBase API

This example is a very simple "hello world" application, written in Python, that illustrates how to:

  • Connect to a Cloud Bigtable instance.
  • Create a new table.
  • Write data to the table.
  • Read the data back.
  • Delete the table.

Running the sample

This example uses the HappyBase package of the Google Cloud Client Library for Python, an implementation of the HappyBase APIs, to communicate with Cloud Bigtable. Use the HappyBase package if you need to move an existing HBase workload to Cloud Bigtable. For new applications, see the "hello world" example that uses the Cloud Bigtable package.

To run this sample program, follow the instructions for the sample on GitHub.

Using the HappyBase APIs with Cloud Bigtable

The sample application connects to Cloud Bigtable and demonstrates some simple operations.

Installing and importing the client library

The required Python packages can be installed using PIP into a virtualenv environment. The sample includes a requirements file defining the needed packages.

google-cloud-happybase==0.20.0

The modules can then be imported.

from google.cloud import bigtable
from google.cloud import happybase

Connecting to Cloud Bigtable

Connect to the Cloud Bigtable by passing a bigtable.Client to a happybase.Connection.

# The client must be created with admin=True because it will create a
# table.
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
connection = happybase.Connection(instance=instance)

Creating a table

Use Connection.create_table() to create a table and its column families.

print('Creating the {} table.'.format(table_name))
column_family_name = 'cf1'
connection.create_table(
    table_name,
    {
        column_family_name: dict()  # Use default options.
    })

Writing rows to a table

Get an existing Table with Connection.table(). Use Table.put() to write a row to the table.

print('Writing some greetings to the table.')
table = connection.table(table_name)
column_name = '{fam}:greeting'.format(fam=column_family_name)
greetings = [
    'Hello World!',
    'Hello Cloud Bigtable!',
    'Hello HappyBase!',
]

for i, value in enumerate(greetings):
    # Note: This example uses sequential numeric IDs for simplicity,
    # but this can result in poor performance in a production
    # application.  Since rows are stored in sorted order by key,
    # sequential keys can result in poor distribution of operations
    # across nodes.
    #
    # For more information about how to design a Bigtable schema for
    # the best performance, see the documentation:
    #
    #     https://cloud.google.com/bigtable/docs/schema-design
    row_key = 'greeting{}'.format(i)
    table.put(row_key, {column_name: value})

Reading a row by its key

Get a row directly using its key with Table.row().

print('Getting a single greeting by row key.')
key = 'greeting0'.encode('utf-8')
row = table.row(key)
print('\t{}: {}'.format(key, row[column_name.encode('utf-8')]))

Scanning all table rows

Use the Table.scan() to get a range of rows.

print('Scanning for all greetings:')

for key, row in table.scan():
    print('\t{}: {}'.format(key, row[column_name.encode('utf-8')]))

Deleting a table

Delete a table with Connection.delete_table().

print('Deleting the {} table.'.format(table_name))
connection.delete_table(table_name)

Putting it all together

Here is the full example without comments.


"""Demonstrates how to connect to Cloud Bigtable and run some basic operations.

Prerequisites:

- Create a Cloud Bigtable cluster.
  https://cloud.google.com/bigtable/docs/creating-cluster
- Set your Google Application Default Credentials.
  https://developers.google.com/identity/protocols/application-default-credentials
"""

import argparse

from google.cloud import bigtable
from google.cloud import happybase


def main(project_id, instance_id, table_name):
    client = bigtable.Client(project=project_id, admin=True)
    instance = client.instance(instance_id)
    connection = happybase.Connection(instance=instance)

    try:
        print('Creating the {} table.'.format(table_name))
        column_family_name = 'cf1'
        connection.create_table(
            table_name,
            {
                column_family_name: dict()  # Use default options.
            })

        print('Writing some greetings to the table.')
        table = connection.table(table_name)
        column_name = '{fam}:greeting'.format(fam=column_family_name)
        greetings = [
            'Hello World!',
            'Hello Cloud Bigtable!',
            'Hello HappyBase!',
        ]

        for i, value in enumerate(greetings):
            row_key = 'greeting{}'.format(i)
            table.put(row_key, {column_name: value})

        print('Getting a single greeting by row key.')
        key = 'greeting0'.encode('utf-8')
        row = table.row(key)
        print('\t{}: {}'.format(key, row[column_name.encode('utf-8')]))

        print('Scanning for all greetings:')

        for key, row in table.scan():
            print('\t{}: {}'.format(key, row[column_name.encode('utf-8')]))

        print('Deleting the {} table.'.format(table_name))
        connection.delete_table(table_name)

    finally:
        connection.close()


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('project_id', help='Your Cloud Platform project ID.')
    parser.add_argument(
        'instance_id', help='ID of the Cloud Bigtable instance to connect to.')
    parser.add_argument(
        '--table',
        help='Table to create and destroy.',
        default='Hello-Bigtable')

    args = parser.parse_args()
    main(args.project_id, args.instance_id, args.table)

Send feedback about...

Cloud Bigtable Documentation