为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕标5832272 2022-12-20 11:08:27
我想使用 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 吗?
查看完整描述

1 回答

?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

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


查看完整回答
反对 回复 2022-12-20
  • 1 回答
  • 0 关注
  • 61 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信