python - bigquery.client.query()可以查看2个结果集吗?

标签 python python-3.x google-bigquery

我想使用 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 中运行相同的查询,我将能够同时查看这两个结果。

Image from BQ Query Editor

我如何才能在 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/

相关文章:

google-bigquery - 在大查询中加入大表

google-bigquery - BigQuery 审核日志应该生成什么?

python - 仅计算数据框中没有 NaN 值的数据行的平均值

python - 如何向keras添加自定义指标? (平均绝对误差百分比)

python - tkinter messagebox 中的格式化方法

python-3.x - 如何使用多个数据集训练 LSTM?

python - 在 Fabric 任务中使用环境相关的 Django 设置

python - PyQt5 - 无法加载平台插件 "windows"。可用平台最小为 : windows,

python-3.x - python3-numpy : Appending to a file using numpy savetxt

firebase - 来自 Firebase Analytics 的事件未显示在 Big Query 中