2013-07-25 3 views
0

저는 BigQuery를 사용하기 위해 Google Python API를 사용하고 있습니다.파이썬으로 모든 BigQuery 작업을 반복하십시오.

jobs().list()jobs().list_next()을 사용하여 내 프로젝트의 모든 작업을 까지 페이지 매김했습니다. 나는 다음과 같은 코드로 발전기를 사용하고 있습니다 : 나는 maxResults을 사용하는 방법에 따라, 나는 다른 작업 목록을 얻을

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 인수를 사용하지 않고 9986 개의 작업을 봅니다.
  • maxResults=500을 사용하면 8596 개의 작업을 봅니다.
  • maxResults=1000을 사용하면 6743 작업을 볼 수 있습니다.
  • maxResults=64000을 사용하면 6743 작업을 볼 수 있습니다.

내가 때마다 동일하게 작업의 수를 예상하고있어, 그래서 제대로 API를 사용하고 경우 잘 모르겠어요.

프로젝트의 모든 작업을 반복하는 올바른 방법은 무엇입니까?


아직도이 알아 내려고 일 (수 8월 14일 중부 서머 타임 15시 30분 29초 2013 년 업데이트). 나는 코드을 사용하여 @Michael Manoochehri이 친절하게 3 번 제공 한 코드를 실행했습니다. 작업의 수에 대한 다양한 정보는 각 시간을보고 서로 어떻게 관련되는지 다음과 같습니다 :

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 파이썬 모듈은] [1]이 추가 오류 처리, 페이징 등으로 원시 클라이언트 LIB의 상단에 빌드 파이썬에서 API에 액세스 할 수있는 좋은 방법입니다 페이지 토큰을 제대로 사용하고 있는지 확실하지 않습니다. 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 일과 동일한 수의 작업이 표시됩니다. 버그입니까? –

관련 문제