Python 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 from 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
-
Create local authentication credentials for your Google Account:
gcloud auth application-default login
For more information, see Set up authentication for a local development environment.
Running the sample
This example uses the Bigtable package of the Google Cloud Client Library for Python to communicate with Bigtable. The Bigtable package is the best choice for new applications. If you need to move an existing HBase workload to 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 Bigtable
The sample application connects to 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.
Async Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Sync Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Connecting to Bigtable
Connect to Bigtable using a
bigtable.Client
.
Async Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Sync Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
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.
Async Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Sync Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Creating a filter
Before you read the data that you wrote, create a filter using row_filters.CellsColumnLimitFilter()
to limit the data that
Bigtable returns. This filter tells Bigtable to
return only the most recent cell in each column, even if the table contains
older cells that haven't been removed yet during garbage collection.
Async Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Sync Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
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.
Async Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Sync Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Scanning all table rows
Use Table.read_rows()
to read a range of rows
from a table.
Async Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Sync Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Deleting a table
Delete a table with Table.delete()
.
Putting it all together
Here is the full example without comments.
Async Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Sync Client
To learn how to install and use the client library for Bigtable, see Bigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.