无法在BigQuery中使用DML语句的作业中设置目标表

时间:2019-04-04 08:32:04

标签: python-3.x google-bigquery

我正在研究使用bigquery.Client.query执行BigQuery sql命令的Python代码。我收到无法在具有DML语句的作业中设置目标表

下面是我正在使用的Python代码

if query_file_name:
    with open(query_file_name, mode="r") as query_file:
        query = query_file.read()

job_config = bigquery.QueryJobConfig()
job_config.use_legacy_sql = use_legacy_sql

if destination:
    if destination.partitioned_field:
        job_config.time_partitioning = TimePartitioning(type_=TimePartitioningType.DAY, 
                                         field=destination.partitioned_field)
google_bq_table = self.fetch_table_reference(destination)
job_config.destination = google_bq_table

job_config.write_disposition = WriteDisposition.WRITE_APPEND

query_job = self.google_client.query(query, job_config=job_config)  # API request - starts the query asynchronously

我的查询文件如下BigQuery sql

INSERT mydataset.target_table
        (col1, col2, col3, created_date)
WITH T AS (SELECT col1, col2, col3, CURRENT_DATE() as created_date
            from mydataset.temp_table
           )
SELECT col1, col2, col3, created_date FROM T

提前感谢您的帮助

谢谢

Raghunath。

2 个答案:

答案 0 :(得分:1)

BigQuery应该低于1,而不是问题中的1。通过以下查询,我能够将数据成功加载到目标表。

WITH T AS (SELECT col1, col2, col3, CURRENT_DATE() as created_date
            from mydataset.temp_table
           )
SELECT col1, col2, col3, created_date FROM T

答案 1 :(得分:1)

  

无法在具有DML语句的作业中设置目标表

作为BigQuery错误消息中的统计信息,您无法在运行插入命令时在python代码目标对象中进行设置。

删除此行

if destination:
    if destination.partitioned_field:
        job_config.time_partitioning = TimePartitioning(type_=TimePartitioningType.DAY, 
                                         field=destination.partitioned_field)
google_bq_table = self.fetch_table_reference(destination)
job_config.destination = google_bq_table

通过您的代码可以解决您的问题