python-2.7 - 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!
解决方案
如果您不想使用命令行工具 和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
推荐阅读
- javascript - clickNextButton not executing properly when previously clicking on the next button
- pytest - Python 2.6.6 的 Pytest 版本
- r - 如何在 ggplot geom_tile 中正确使用构面,同时保持纵横比不变?
- jquery - 使用 JQuery Validate 更改复选框轮廓颜色
- javascript - JavaScript中array.push的第二个参数是如何工作的?
- jenkins - 如何通过 SSH 发布仅复制更改的文件
- visual-studio - 使 Visual Studio 打开工具窗口浮动
- python - 迭代 DataFrame 行并在匹配后立即添加一个新行
- php - php中的多个MySQL查询不起作用
- node.js - 在 Mac 上降级 NPM 版本