Skip to content

Instantly share code, notes, and snippets.

@salvadorgascon
Last active February 23, 2024 12:54
Show Gist options
  • Save salvadorgascon/04e429141b98ab94089699785c2fe364 to your computer and use it in GitHub Desktop.
Save salvadorgascon/04e429141b98ab94089699785c2fe364 to your computer and use it in GitHub Desktop.
Create table object in Google Cloud BigQuery
BIGQUERY_PROJECT_NAME = "project-XXXXX"
from google.cloud import bigquery
from google.cloud.exceptions import NotFound
from google.api_core.exceptions import Conflict
from shared.loaders.bigquery_constants import BIGQUERY_PROJECT_NAME
def BigQueryCreateTable(bigquery_client, bigquery_dataset_name, bigquery_table_name, table_schema):
print("Creating table object if not exists " + f'{BIGQUERY_PROJECT_NAME}.{bigquery_dataset_name}.{bigquery_table_name}')
table_definition = bigquery.Table(f'{BIGQUERY_PROJECT_NAME}.{bigquery_dataset_name}.{bigquery_table_name}',
schema = table_schema)
try:
table = bigquery_client.create_table(table_definition)
except Conflict as inst:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment