Python hello world
This example is a "hello world" application, written in Python, that illustrates how to do the following:
- Set up authentication.
- Connect to a Bigtable instance.
- Create a new table.
- Write data to the table.
- Read the data back.
- Delete the table.
The Python client library for Bigtable offers two APIs, asyncio
and a synchronous API. If your application is asynchronous, use asyncio
.
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.
Run the sample
This example uses the Bigtable package of the Cloud Client Libraries 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.
Use the Cloud Client Libraries with Bigtable
The sample application connects to Bigtable and demonstrates some operations.
Install and import 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.
Asyncio
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
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.
Connect to Bigtable
Connect to Bigtable using a
bigtable.Client
.
Asyncio
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
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.
Create 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.
Write 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.
Asyncio
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
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.
Create 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.
Asyncio
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
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.
Read a row by its row 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.
Asyncio
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
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.
Scan all table rows
Use Table.read_rows()
to read a range of rows
from a table.
Asyncio
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
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.
Delete a table
Delete a table with Table.delete()
.
Put it all together
Here is the full example without comments.
Asyncio
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
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.