【问题标题】:Can 2 result sets be viewed from bigquery.client.query()?可以从 bigquery.client.query() 查看 2 个结果集吗?
【发布时间】:2020-06-02 02:53:26
【问题描述】:

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

如何在 Bigquery API 结果集中获取两个查询结果?我需要在 client.query() 语句中添加一个 jobconfig 吗?

【问题讨论】:

    标签: python python-3.x google-bigquery


    【解决方案1】:

    这是一个遍历脚本的简单示例。在此示例中,您的父作业是脚本类型,它由两个都是 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)
    

    【讨论】:

    • 啊! list_jobs,完美。也非常感谢您的示例脚本。使其易于理解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多