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 Cloud Bigtable package of the Google Cloud Client Library for Python to communicate with Cloud Bigtable. The Cloud Bigtable package is the best choice for new applications. If you need to move an existing HBase workload to Cloud Bigtable, see the "hello world" example that uses the HappyBase package.
To run this sample program, follow the instructions for the sample on GitHub.
Using the Cloud Client Library with Cloud Bigtable
The sample application connects to Cloud Bigtable and demonstrates some simple operations.
Installing and importing the client library
Use PIP to install the required Python packages into a virtualenv environment. The sample includes a requirements file defining the needed packages.
Import the modules.
Connecting to Cloud Bigtable
Connect to Cloud Bigtable using a
bigtable.Client
.
Creating a table
Instantiate a table object using Instance.table()
. Create a column family and set its
garbage-collection policy, then pass the column family to Table.create()
to create the table.
Writing rows to a table
Loop through a list of greeting strings to create some new rows for the table.
In each iteration, use Table.row()
to define a row
and assign it a row key; call Row.set_cell()
to
set a value for the current cell; and append the new row to an array of rows.
Finally, call Table.mutate_rows()
to add
the rows to the table.
Creating a filter
Before you read the data that you wrote, create a filter using row_filters.CellsColumnLimitFilter()
to limit the data that
Cloud Bigtable returns. This filter tells Cloud Bigtable to
return only the most recent version of each value, even if the table contains
older versions that haven't been garbage-collected.
Reading a row by its key
Call the table's Table.read_row()
method to get a reference to
the row with a specific row key, passing in the key and the filter, to get one
version of each value in that row.
Scanning all table rows
Use Table.read_rows()
to read a range of rows
from a table.
Deleting a table
Delete a table with Table.delete()
.
Putting it all together
Here is the full example without comments.