python - BigQuery : Unexpected behaviour using bigquery job when writing query result

标签 python google-bigquery

我目前正在使用 Bigquery 作业来调整一些数据并将其加载到其他表中。

我的 bigquery 作业使用查询作业从表中读取,然后将其写入其他表中。作业执行成功,作业状态已完成,但已加载任何行。

这是代码:

table_id_from = "table_from"

table_ref_to = bigquery_client.dataset('format').table("table_to")

job_config = bigquery.LoadJobConfig()

job_config.create_disposition = 'NEVER'
job_config.destination = table_ref_to_format
job_config.write_disposition = 'WRITE_APPEND'
job_config.use_legacy_sql = False

# Start the query, passing in the extra configuration.
query = """SELECT id, name, short_name,
    subdomain, address, address2, department, state, zip
    from staging.%s;""" %(table_id_from)

query_job = bigquery_client.query(query, job_config=job_config)

rows_from_staging = list(query_job)  # Waits for the query to finish
print(len(rows_from_staging))
# assert query_job.state == 'RUNNING'
# assert query_job.job_type == 'query'

iterator = bigquery_client.list_rows(
    table_ref_to_format, selected_fields=[bigquery.SchemaField('id', 'INTEGER')])
rows = list(iterator)
print(len(rows))
print(query_job.state)
query_job.result()

第一部分的结果,当从表中读取时,打印 len 3。另一方面,当查询目标表时,它不读取任何内容并打印 0 作为行的 len。

3
0
DONE

发生了什么事?我原以为如果出现问题就会抛出错误,但它运行成功了。有什么帮助吗?

最佳答案

您正在使用 LoadJobConfig() 而不是 QueryJobConfig()。如果你改变它,这会工作得很好。

关于python - BigQuery : Unexpected behaviour using bigquery job when writing query result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48659440/

相关文章:

python - 无法使用 Python 通过 Robot Framework 连接到 localhost :xxxx from session not created: Chrome version must be >= 69. 0.3497.0 上的 chrome

python - 大查询 - 本地计算机 - ImportError : DLL load failed: The specified procedure could not be found

python - 使用受 SSL 保护的 Redis 配置 Django channel

python - 通过conda安装tensorflow失败

python - 如何将 csv 转换为 apache beam 数据流中的字典

google-bigquery - 大查询分析函数提高查询性能

javascript - 参数化 Bigquery 和字符串值数组

google-bigquery - 我何时应该选择批处理分析而不是交互式分析?

python - ipython : access notebook server remotely via a web browser

python - 删除从文件读取的列表中的换行符