HappyBase API hello world
This example is a very simple "hello world" application, written in Python, that illustrates 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.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
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 Bigtable. Use the HappyBase package if you need to move an existing HBase workload to Bigtable. For new applications, see the "hello world" example that uses the Bigtable package.
To run this sample program, follow the instructions for the sample on GitHub.
Using the HappyBase APIs with Bigtable
The sample application connects to 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.
The modules can then be imported.
Connecting to Bigtable
Connect to Bigtable by passing a
bigtable.Client
to a
happybase.Connection
.
Creating a table
Use Connection.create_table()
to
create a table and its column families.
Writing rows to a table
Get an existing Table
with
Connection.table()
. Use
Table.put()
to write a row to the table.
Reading a row by its key
Get a row directly using its key with Table.row()
.
Scanning all table rows
Use Table.scan()
to get a range of rows.
Deleting a table
Delete a table with
Connection.delete_table()
.
Putting it all together
Here is the full example without comments.