首页 > 解决方案 > 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!

标签: python-2.7google-bigquery

解决方案


如果您不想使用命令行工具 和os.system(command),您可以使用 Python BigQuery 库从外部源创建 BigQuery 表,其中包含如下代码:

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)

这是API 参考的链接

这里有更多关于及其属性的信息。ExternalConfig


推荐阅读