List Tables in a BigQuery Dataset using Python
In this tutorial, we will learn how to retrieve a list of all tables within a specific BigQuery dataset using Python. This is a common operation for managing datasets and auditing resources.
Python Code to List Tables
We will use the list_tables method from the google-cloud-bigquery library.
Python
# Import the packages
from dotenv import load_dotenv
import os
from google.oauth2 import service_account
from google.cloud import bigquery
def main():
# Loads environment variables from a .env file
load_dotenv()
# Use environment variables from .env file
credentials_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
project_name = os.getenv("project_id")
# Get the service account credentials
credentials = service_account.Credentials.from_service_account_file(credentials_path)
# Create the BigQuery client
client = bigquery.Client(credentials=credentials, project=project_name)
# dataset_id = 'your-project.your_dataset'
dataset_id = 'ashishcoder.ds_from_tf'
# List the tables in the dataset
tables = client.list_tables(dataset_id)
if not tables:
print(f"No tables found in dataset {dataset_id}.")
else:
print(f"Tables in dataset {dataset_id}:")
for table in tables:
print(f"Table Name: {table.table_id}")
if __name__ == "__main__":
main() Key Steps Explained
- client.list_tables(dataset_id): Returns an iterator over the tables in the specified dataset.
- table.table_id: Access the identifier of each table object.
Note: Listing tables requires the bigquery.tables.list permission on the dataset.