我正在使用python客户端通过SQL创建表,如文档(https://cloud.google.com/bigquery/docs/tables)所述,如下所示:
# from google.cloud import bigquery
# client = bigquery.Client()
# dataset_id = 'your_dataset_id'
job_config = bigquery.QueryJobConfig()
# Set the destination table
table_ref = client.dataset(dataset_id).table('your_table_id')
job_config.destination = table_ref
sql = """
SELECT corpus
FROM `bigquery-public-data.samples.shakespeare`
GROUP BY corpus;
"""
# Start the query, passing in the extra configuration.
query_job = client.query(
sql,
# Location must match that of the dataset(s) referenced in the query
# and of the destination table.
location='US',
job_config=job_config) # API request - starts the query
query_job.result() # Waits for the query to finish
print('Query results loaded to table {}'.format(table_ref.path))
这很好用,除了用于通过SQL查询创建表的客户端函数使用job_config对象,而job_config接收table_ref而不是表对象。
我在以下文档中找到了用于创建表的文档:https://google-cloud-python.readthedocs.io/en/stable/bigquery/usage.html,但这是针对不是通过查询创建的表的。
在为表指定说明时如何通过查询创建表的任何想法?
答案 0 :(得分:1)
由于您要做的不只是将SELECT
结果保存到新表中,所以最好的方法不是在job_config
变量中使用目标表,而是使用CREATE
命令
所以您需要做两件事:
table_ref = client.dataset(dataset_id).table('your_table_id')
job_config.destination = table_ref
#standardSQL
CREATE TABLE dataset_id.your_table_id
PARTITION BY DATE(_PARTITIONTIME)
OPTIONS(
description = 'this table was created via agent #123'
) AS
SELECT corpus
FROM `bigquery-public-data.samples.shakespeare`
GROUP BY corpus;