无法使用 BigQuery Python API 设置目标表 [英] Cannot set destination table with BigQuery Python API
问题描述
我最近在使用 Python API 时收到以下 BigQuery 错误:
google.api_core.exceptions.BadRequest:无法为脚本设置 400 configuration.query.destinationTable
这是我使用的函数:
def execute_bigquery_sql(query, dataset_id, table_id, use_legacy_sql=True, write_disposition='WRITE_TRUNCATE'):客户端 = bigquery.Client()job_config = bigquery.QueryJobConfig()job_config.use_legacy_sql = use_legacy_sql打印(table_id:{table_id}".格式(table_id=table_id))print("dataset_id: {dataset_id}".format(dataset_id=dataset_id))如果 table_id:table_ref = client.dataset(dataset_id).table(table_id)打印(table_ref:{table_ref}".格式(table_ref=table_ref))job_config.destination = table_refjob_config.write_disposition = write_dispositionjob_config.allow_large_results = Truejob_config.createDisposition = "CREATE_IF_NEEDED"query_job = client.query(query,job_config=job_config)results = query_job.result() # 等待作业完成.
有谁知道可能会发生什么以及解决方法吗?
感谢您的回复,评论确实朝着正确的方向发展.在 BigQuery 脚本中意味着
- 相反,当不使用脚本语句时,您应该看到:
I'm recently getting the following BigQuery error when using the Python API:
google.api_core.exceptions.BadRequest: 400 configuration.query.destinationTable cannot be set for scripts
This is the function I use:
def execute_bigquery_sql(query, dataset_id, table_id, use_legacy_sql=True, write_disposition='WRITE_TRUNCATE'):
client = bigquery.Client()
job_config = bigquery.QueryJobConfig()
job_config.use_legacy_sql = use_legacy_sql
print("table_id: {table_id}".format(table_id=table_id))
print("dataset_id: {dataset_id}".format(dataset_id=dataset_id))
if table_id:
table_ref = client.dataset(dataset_id).table(table_id)
print("table_ref: {table_ref}".format(table_ref=table_ref))
job_config.destination = table_ref
job_config.write_disposition = write_disposition
job_config.allow_large_results = True
job_config.createDisposition = "CREATE_IF_NEEDED"
query_job = client.query(query,job_config=job_config)
results = query_job.result() # Waits for job to complete.
Does anyone knows what might be happening and a workaround?
Thanks for the responses, comments were indeed in the right direction. In BigQuery scripting means https://cloud.google.com/bigquery/docs/reference/standard-sql/scripting.
And that is what is not allowed, and my query had:
DECLARE capital int64 default 10000000;
So, removing the line above was the fix in my case.
Interesting thing is that even in the web interface
- If you use scripts the interface won't allow to save to table:
- In contrast, when not using script statements, you should see:
这篇关于无法使用 BigQuery Python API 设置目标表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!