Get BigQuery Table Metadata using Python
In this tutorial, we will learn how to access detailed metadata for a BigQuery table using Python. This metadata includes table schema, creation time, row count, and more.
Python Code to Get Table Metadata
We will use the get_table 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'
table_id = 'ashishcoder.ds_from_tf.table_from_tf'
# Get the table object
table = client.get_table(table_id)
# Print table metadata
print(f"Table ID: {table.table_id}")
print(f"Full Table Name: {table.full_table_id}")
print(f"Table Description: {table.description}")
print(f"Table Creation Time: {table.created}")
print(f"Table Row Count: {table.num_rows}")
print(f"Project name: {table.project}")
print(f"Table Schema: {table.schema}")
print(f"Table Schema (First field): {table.schema[0].name}")
if __name__ == "__main__":
main() Key Metadata Properties
- table.created: The date and time the table was created.
- table.num_rows: The total number of rows currently in the table.
- table.schema: A list of SchemaField objects representing the table structure.
Note: Metadata is useful for validating table structures before running automated data pipelines.