2013-07-25 48 views
0

我正在使用Google Python API来处理BigQuery。使用Python遍历所有BigQuery作业

我试图使用jobs().list()jobs().list_next()通过 都在我的项目的作业进行分页。我使用的是发电机下面的代码:

request = service.jobs().list(projectId=project_id, 
           allUsers=True, 
           stateFilter="done", 
          ) 
           # or maxResults=500) 
           # or maxResults=1000) 
           # or maxResults=64000) 
while request is not None: 
    response = request.execute() 
    for x in response["jobs"]: 
     yield x 
    request = service.jobs().list_next(request, response) 

的问题是,这取决于我如何使用maxResults,我得到不同的工作列表。

  • 使用否maxResults自变量我看到9986个作业。
  • 使用maxResults=500我看到8596个工作。
  • 使用maxResults=1000我看到6743个工作。
  • 使用maxResults=64000我看到6743个工作。

我在等待每次作业的数量相同,所以我不确定我是否正确使用了API。

什么是正确的方式来循环通过项目中的所有工作?


(更新周三8月14日十五时30分29秒CDT 2013)

仍在试图弄清楚这一点。我运行代码@Michael Manoochehri友情提供三次,使用不同的maxResults。有关作业数量的各种信息,每一次报告,以及它们如何相互关联的低于:

s1 -> no maxResults 
s2 -> maxResults=500 
s3 -> maxResults=1000 

|s1| -> 10112 
|s2| -> 8579 
|s3| -> 6556 

|s1 intersection s2| -> 8578 
|s2 difference s1| -> 1 
|s1 difference s2| -> 1534 

|s1 intersection s3| -> 6556 
|s3 difference s1| -> 0 
|s1 difference s3| -> 3556 

|s3 intersection s2| -> 6398 
|s2 difference s3| -> 2181 
|s3 difference s2| -> 158 

我还是不能让我为什么没有看到就业岗位一致总数的感觉不管使用maxResults

回答

0

首先,[bigquery_client.py Python模块] [1]是访问来自Python中API的好方法,它建立在与另外的错误处理,寻呼等的原始客户机IIb的顶部:

我不确定您是否正确使用页面令牌?你能证实你正在检查nextPageToken吗?这里有一个我以前用过的例子:

import httplib2 
import pprint 
import sys 

from apiclient.discovery import build 
from apiclient.errors import HttpError 

from oauth2client.client import AccessTokenRefreshError 
from oauth2client.client import OAuth2WebServerFlow 
from oauth2client.client import flow_from_clientsecrets 
from oauth2client.file import Storage 
from oauth2client.tools import run 


# Enter your Google Developer Project number 
PROJECT_NUMBER = 'XXXXXXXXXXXXX' 

FLOW = flow_from_clientsecrets('client_secrets.json', 
           scope='https://www.googleapis.com/auth/bigquery') 



def main(): 

    storage = Storage('bigquery_credentials.dat') 
    credentials = storage.get() 

    if credentials is None or credentials.invalid: 
    credentials = run(FLOW, storage) 

    http = httplib2.Http() 
    http = credentials.authorize(http) 

    bigquery_service = build('bigquery', 'v2', http=http) 
    jobs = bigquery_service.jobs() 

    page_token=None 
    count=0 

    while True: 
    response = list_jobs_page(jobs, page_token) 
    if response['jobs'] is not None: 
     for job in response['jobs']: 
     count += 1 
     print '%d. %s\t%s\t%s' % (count, 
            job['jobReference']['jobId'], 
            job['state'], 
            job['errorResult']['reason'] if job.get('errorResult') else '') 
    if response.get('nextPageToken'): 
     page_token = response['nextPageToken'] 
    else: 
     break 




def list_jobs_page(jobs, page_token=None): 
    try: 
    jobs_list = jobs.list(projectId=PROJECT_NUMBER, 
          projection='minimal', 
          allUsers=True, 
        # You can set a custom maxResults 
          # here 
          # maxResults=500, 
          pageToken=page_token).execute() 

    return jobs_list 

    except HttpError as err: 
    print 'Error:', pprint.pprint(err.content) 



if __name__ == '__main__': 
    main() 


    [1]: https://code.google.com/p/google-bigquery-tools/source/browse/bq/bigquery_client.py#1078 
+0

我想我认为list_next()方法会为我打理pageToken,尽管我没有找到它的文档。 我会按照你所展示的方式尝试它。谢谢! –

+0

我已经用maxResults的不同值试过了你的代码,而且我仍然看到不同的作业总数,这取决于我发送的maxResults值。实际上,没有maxResults,我现在看到数百个作业(如预期的那样它已经有好几天了,我们一直在查询),但是maxResults = 500和maxResults = 1000,我看到的工作和7月25日时一样。这是一个错误吗? –