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.

  1. Install the Google Cloud CLI.

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

  3. 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.

    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.

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.

google-cloud-bigtable==2.35.0
google-cloud-core==2.5.0

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 client libraries.

from google.cloud import bigtable
from google.cloud.bigtable.data import row_filters

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 client libraries.

from datetime import datetime, timezone

from google.cloud import bigtable
from google.cloud.bigtable import column_family
from google.cloud.bigtable import row_filters

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 client libraries.

client = bigtable.data.BigtableDataClientAsync(project=project_id)
table = client.get_table(instance_id, table_id)

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 client libraries.

# 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)

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.

print("Creating the {} table.".format(table_id))
table =