简体   繁体   English

Python BigQuery API:如何异步获取数据?

[英]Python BigQuery API: how to get data asynchronously?

I am getting started with the BigQuery API in Python, following the documentation . 根据文档 ,我开始使用Python中的BigQuery API。

This is my code, adapted from an example : 这是我的代码,改编自一个示例

credentials = GoogleCredentials.get_application_default()
bigquery_service = build('bigquery', 'v2', credentials=credentials)

try:
    query_request = bigquery_service.jobs()
    query_data = {
        'query': (
            'SELECT * FROM [mytable] LIMIT 10;"
        )
    }
    query_response = query_request.query(
        projectId=project_id,
        body=query_data).execute()
    for row in query_response['rows']:
        print('\t'.join(field['v'] for field in row['f']))

The problem I'm having is that I keep getting the response: 我遇到的问题是我不断收到响应:

{u'kind': u'bigquery#queryResponse', 
 u'jobComplete': False, 
u'jobReference': {u'projectId': 'myproject', u'jobId': u'xxxx'}}

So it has no rows field. 因此它没有rows字段。 Looking at the docs, I guess I need to take the jobId field and use it to check when the job is complete, and then get the data. 查看文档,我想我需要使用jobId字段并使用它来检查作业何时完成,然后获取数据。

The problem I'm having is that the docs are a bit scattered and confusing, and I don't know how to do this. 我遇到的问题是文档有点分散和混乱,我不知道该怎么做。

I think I need to use this method to check the status of the job, but how do I adapt it for Python? 我认为我需要使用此方法来检查作业的状态,但是如何使它适应Python? And how often should I check / how long should I wait? 我应该多久检查一次/我应该等待多长时间?

Could anyone give me an example? 谁能给我一个例子吗?

There is code to do what you want here . 有代码,做你想要的这里

If you want more background on what it is doing, check out Google BigQuery Analytics chapter 7 (the relevant snippet is available here .) 如果您想了解更多有关其工作背景的信息,请查看Google BigQuery Analytics第7章(有关相关代码段,请参见此处)

TL;DR: TL; DR:

Your initial jobs.query() call is returning before the query completes; 您的初始jobs.query()调用将在查询完成之前返回; to wait for the job to be done you'll need to poll on jobs.getQueryResults() . 为了等待工作完成,您需要对jobs.getQueryResults()进行轮询。 You can then page through the results of that call. 然后,您可以翻阅该呼叫的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM