2016-11-08 26 views
0

我有以下功能:BigQuery的查詢響應「jobComplete」:假

def query_big_query(query_data, project_id): 
    credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials_bq.json') 
    bigquery_service = build('bigquery', 'v2', credentials=credentials) 
    try: 
     query_request = bigquery_service.jobs() 
     query_response = query_request.query(projectId=project_id, body=query_data).execute() 
     return query_response 
    except HttpError as error: 
     print ('Error :{}'.format(error.content)) 
     raise error 

但是某些查詢不返回數據,bacause查詢需要多一點的時間,並返回下面的字符串:

{u'kind': u'bigquery#queryResponse', u'jobComplete': False, u'jobReference': {u'projectId': u'od', u'jobId': u'job_5wAuC'}} 

如何等待作業完成(True)?或者我應該要求結果作爲不同的呼叫?

回答

0

試試這個:

response = bigquery_service.jobs().getQueryResults(projectId=my_project_id, jobId=my_job_id).execute() 
while not response['jobComplete']: 
    print "waiting for response..." 
    time.sleep(1) 
    response = bigquery_service.jobs().getQueryResults(projectId=my_project_id, jobId=my_job_id).execute() 
return response