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

Rag*_*ath 2 python-3.x google-bigquery

我正在编写 Python 代码以使用 bigquery.Client.query 执行 BigQuery sql 命令。我在 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
Run Code Online (Sandbox Code Playgroud)

我有 query_file 如下 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
Run Code Online (Sandbox Code Playgroud)

提前感谢您的帮助

谢谢,

拉古纳特。

Tam*_*ein 5

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

正如 BigQuery 错误消息中的 stat 一样,在运行插入命令时,您无法在 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
Run Code Online (Sandbox Code Playgroud)

从您的代码将解决您的问题