Python 2.7 和 GCP Google BigQuery:CREATE TABLE AS SELECT....(中国电信协会)



我正在使用python 2.7(现在无法更改)和Google Python客户端库v0.28 v0.28我会做a&quot table xx作为从y中选择a,b,c,其中n ='helloworld'&quort。

我们将其称为"创建表"为select'(CTA)在其他数据库中,尽管我不确定最好的方法是通过Python在BQ中进行此操作。

这是一篇有趣的文章,讲述了使用作业,但是我认为BQ Python库只有几种工作,因此我不确定从哪里开始。(LoadJob,copyjob,Extractjob和QueryJob)https://chartio.com/resources/tutorials/how-to-to-create-a-table-from-a-query-in-google-bigquery/

有一个很好的bigquery_client.create_table函数,但我认为我无法设置查询配置字段或其中任何真实的配置字段。

因此,您可以提供的任何帮助或指导将不胜感激。如果您在美国这里,希望您度过一个愉快的感恩节假期。

非常感谢您对我所有的大Query朋友... Rich

开始编辑

可以关闭此操作,下面的帮助和以下链接解决了我的需求,我将代码放在此处,以便其他任何可能想要它的人。

劫持的代码从查询中创建一个表中的表中的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

指导将不胜感激

ddl不受bigquery的支持(希望) - 因此,您应该使用常规方式:jobs.insert与各自的目标表和写入disposition

https://cloud.google.com/bigquery/docs/reference/rest/rest/v2/jobs#configuration.query

最新更新