HappyBase API hello world

Alpha

This product is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of theService Specific Terms. Pre-GA products are available "as is" and might have limited support. For more information, see thelaunch stage descriptions.

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

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

Set up authentication

To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.

    Install the Google Cloud CLI.

    If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    If you're using a local shell, then create local authentication credentials for your user account:

    gcloudauthapplication-defaultlogin

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

For more information, see Set up authentication for a local development environment.

Running the sample

This example uses theHappyBase package of theGoogle Cloud Client Library for Python, an implementation oftheHappyBase APIs, to communicate with Bigtable. Usethe HappyBase package if you need to move an existing HBase workload toBigtable. For new applications, see the"hello world" examplethat uses the Bigtable package.

To run this sample program, follow theinstructions for the sample onGitHub.

Using the HappyBase APIs with Bigtable

The sample application connects to Bigtable and demonstrates somesimple operations.

Installing and importing the client library

The required Python packages can be installed usingPIP into avirtualenv environment. The sample includes arequirementsfile defining the needed packages.

google-cloud-happybase==0.33.0six==1.17.0 # See https://github.com/googleapis/google-cloud-python-happybase/issues/128

The modules can then be imported.

fromgoogle.cloudimportbigtablefromgoogle.cloudimporthappybase

Connecting to Bigtable

Connect to Bigtable by passing abigtable.Client to ahappybase.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

UseConnection.create_table() tocreate a table and its column families.

Note: Columns that are related to one another are typically grouped into a column family. For more information about column families, see theBigtable storage model.
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 existingTable withConnection.table(). UseTable.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!",]fori,valueinenumerate(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-designrow_key="greeting{}".format(i)table.put(row_key,{column_name.encode("utf-8"):value.encode("utf-8")})

Reading a row by its key

Get a row directly using its key withTable.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

UseTable.scan() to get a range of rows.

print("Scanning for all greetings:")forkey,rowintable.scan():print("\t{}:{}".format(key,row[column_name.encode("utf-8")]))

Deleting a table

Delete a table withConnection.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"""importargparsefrom..utilsimportwait_for_tablefromgoogle.cloudimportbigtablefromgoogle.cloudimporthappybasedefmain(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.)wait_for_table(instance.table(table_name))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!",]fori,valueinenumerate(greetings):row_key="greeting{}".format(i)table.put(row_key,{column_name.encode("utf-8"):value.encode("utf-8")})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:")forkey,rowintable.scan():print("\t{}:{}".format(key,row[column_name.encode("utf-8")]))finally:print("Deleting the{} table.".format(table_name))connection.delete_table(table_name)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)

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.