我想使用 bigquery API 运行 2 个 Select 参数。 例如,如果我运行以下查询
SELECT 1;
SELECT 2;
当我使用以下 python 脚本运行它时,我只获得了第二个查询的结果。
def runquery();
bqclient = bigquery.client()
query = """ SELECT 1;
SELECT 2;"""
query_job = bqclient.query(query)
data = query_job.result()
rows = list(data)
print(rows)
结果:
[Row((2,), {'f0_': 0})]
但是,如果我在 bigquery query composer 中运行相同的查询,我将能够同时查看这两个结果。
我如何才能在 Bigquery API 结果集中获得这两个查询结果?我需要在 client.query() 语句中添加 jobconfig 吗?
最佳答案
这是一个遍历脚本的简单示例。在此示例中,您的父作业属于脚本类型,它由两个都是 select 语句的子作业组成。父作业完成后,您可以使用父过滤器调用 list_jobs
来查找子作业并询问它们的结果。子作业不嵌套,因此您只需担心父作业下一级的子作业。
def multi_statement_script():
from google.cloud import bigquery
bqclient = bigquery.Client()
query = """ SELECT 1;
SELECT 2;
"""
parent_query = bqclient.query(query)
# wait for parent job to finish (which completes when all children are done)
parent_query.result()
print("parent job {}".format(parent_query.job_id))
children = bqclient.list_jobs(parent_job=parent_query.job_id)
# note the jobs are enumerated newest->oldest, so the reverse
# ordering specified in the script
for child in children:
print("job {}".format(child.job_id))
rows = list(child.result())
print(rows)
关于python - bigquery.client.query()可以查看2个结果集吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62144039/