【发布时间】:2018-05-06 10:31:56
【问题描述】:
我正在使用 python 2.7(现在无法更改)和 google.cloud.bigquery 的 Google python 客户端库 v0.28,并且我正在尝试弄清楚如何执行“创建表” XX as select a,b,c from Y where n='helloworld'".
我们在其他数据库中将此称为“CREATE TABLE AS SELECT”(CTAS),但我不确定在 bq 中通过 python 执行此操作的最佳方法是什么。
这是一篇关于使用作业的有趣文章,但我认为 bq python 库只有几种作业类型,所以我不知道从哪里开始。 (LoadJob、CopyJob、ExtractJob 和 QueryJob) https://chartio.com/resources/tutorials/how-to-create-a-table-from-a-query-in-google-bigquery/
有一个 bigquery_client.create_table 函数很好,但我不认为我可以设置查询配置字段,或任何真正的配置字段。
因此,您可以提供任何帮助或指导,我们将不胜感激。如果你在美国,我希望你有一个愉快的感恩节假期。
非常感谢您,并向我所有的 bigquery 朋友致以最诚挚的问候...丰富
开始编辑
这个可以关闭,下面的帮助和下面的链接解决了我需要的东西,我把我的代码放在这里给任何可能想要的人。
劫持的代码 Create a table from query results in Google BigQuery
def create_table_as_select(dataset_name, table_name, sqlQuery, project=None):
try:
job_config = bigquery.QueryJobConfig()
# Set configuration.query.destinationTable
dataset_ref = bigquery_client.dataset(dataset_name)
table_ref = dataset_ref.table(table_name)
job_config.destination = table_ref
# Set configuration.query.createDisposition
job_config.create_disposition = 'CREATE_IF_NEEDED'
# Set configuration.query.writeDisposition
job_config.write_disposition = 'WRITE_APPEND'
# Start the query
job = bigquery_client.query(sqlQuery, job_config=job_config)
# Wait for the query to finish
job.result()
returnMsg = 'Created table {} .'.format(table_name)
return returnMsg
except Exception as e:
errorStr = 'ERROR (create_table_as_select): ' + str(e)
print(errorStr)
raise
【问题讨论】:
-
考虑注册preview of the new BigQuery UI,它还可以让您提前访问 BigQuery 中的 DDL 语句。
-
@ElliottBrossard - 我收到了 -
DDL statements are not supported(来自 Alpha UI) -
我认为您会收到有关 DDL alpha 的单独通知,但它应该适用于同一组。我会戳我们的 PM 看看。
-
好的,您应该在一天左右的时间内获得访问权限。 CTAS 目前不受支持,但很快就会推出。
-
一如既往:o) - 谢谢@ElliottBrossard!
标签: python-2.7 google-bigquery