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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
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.
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.
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.
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.
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.