Skip to content

Instantly share code, notes, and snippets.

@jgarland79
Created March 18, 2024 17:07
Show Gist options
  • Save jgarland79/a6213306ec1f7421017d1180a780a7ec to your computer and use it in GitHub Desktop.
Save jgarland79/a6213306ec1f7421017d1180a780a7ec to your computer and use it in GitHub Desktop.
from google.cloud import bigquery
def list_datasets_tables_schemas_data():
client = bigquery.Client()
project_id = client.project
datasets = list(client.list_datasets())
print(f"Datasets in project {project_id}:")
for dataset in datasets:
print(f"\tDataset: {dataset.dataset_id}")
tables = list(client.list_tables(dataset.reference))
print(f"\tTables in dataset {dataset.dataset_id}:")
for table in tables:
print(f"\t\tTable: {table.table_id}")
table_ref = dataset.table(table.table_id)
table = client.get_table(table_ref)
print(f"\t\tSchema of table {table.table_id}:")
for field in table.schema:
print(f"\t\t\t{field.name}: {field.field_type}")
# Print the first few rows of data from the table
print(f"\t\tData sample from {project_id}.{dataset.dataset_id}.{table.table_id}:")
query = f"SELECT * FROM `{project_id}.{dataset.dataset_id}.{table.table_id}` LIMIT 5"
query_job = client.query(query)
results = query_job.result()
for row in results:
print(f"\t\t\t{dict(row)}")
if __name__ == "__main__":
list_datasets_tables_schemas_data()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment