doctorData
doctorData

Reputation: 41

BigQuery - create an external table in Python

I cannot find any documentation about how to create an external table in BigQuery using Python. I would like to create the table without using autodetect but passing the schema. Does anybody know how to do it? Thank you!

Upvotes: 4

Views: 4565

Answers (1)

VictorGGl
VictorGGl

Reputation: 1916

If you don't want to use the command line tool and os.system(command), you can create a BigQuery table out of an external source using the Python BigQuery library with some code like this:

from google.cloud import bigquery

client = bigquery.Client()
#Define your schema
schemafield_col1 = bigquery.schema.SchemaField("string_col","STRING")
schemafield_col2 = bigquery.schema.SchemaField("int_col","INTEGER")



dataset_ref = client.dataset('<your-dataset>')
table_ref = bigquery.TableReference(dataset_ref, '<your-table-name>')
table = bigquery.Table(table_ref, [schemafield_col1,schemafield_col2])

external_config = bigquery.ExternalConfig('CSV')
source_uris = ['<url-to-your-external-source>'] #i.e for a csv file in a Cloud Storage bucket 
                                              #it would be something like "gs://<your-bucket>/<your-csv-file>"
external_config.source_uris = source_uris
table.external_data_configuration = external_config

client.create_table(table)

Here is the link to the API reference.

Here more info about the ExternalConfig class and its attributes.

Upvotes: 8

Related Questions