gcp big query creating table and view using python
When creating a table in BigQuery, first we need to create dataset first. Then when we create a table, that table will be associated with this dataset.
First we need to authenticate our local development environment and then we set our environment variables like so
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\nzai\AppData\Roaming\gcloud\application_default_credentials.json"
In my case, I am authehticated using:
gcloud auth application-default login
We can easily create table in google bigquery using the following python code.
from google.cloud import bigquery
# Initialize client
client = bigquery.Client()
dataset_id = "my_dataset"
table_id = f"{client.project}.{dataset_id}.my_table"
## creating dataset
dataset = bigquery.Dataset(f"{client.project}.{dataset_id}")
dataset.location = "US"
dataset = client.create_dataset(
dataset, exists_ok=True, timeout=30
) # Make an API request.
# Confirm dataset created.
print(f"Created dataset {dataset}.{dataset_id}")
schema = [
bigquery.SchemaField("name", "STRING", mode="REQUIRED"),
bigquery.SchemaField("age", "INTEGER", mode="NULLABLE"),
bigquery.SchemaField("created_at", "TIMESTAMP", mode="NULLABLE"),
]
table = bigquery.Table(table_id, schema=schema)
table = client.create_table(table) # API request
print(f"✅ Created table {table.full_table_id}")
Comments