所有bq工作的摘要

时间:2012-09-20 06:38:05

标签: google-bigquery

有没有办法在给定的时间范围内使用bq命令行工具列出所有作业ID?我需要做的是遍历所有Id并找出是否有任何错误。

我使用Web界面来了解作业ID,然后使用命令:

bq show -j --format=prettyjson job_id

稍后我会手动复制粘贴输出的“错误”部分。这需要花费大量时间来报告给定日期的工作总结。

2 个答案:

答案 0 :(得分:5)

当然,您可以通过运行来列出您有权访问的项目的最近1,000个作业:

bq  ls -j --max_results=1000 project_number

如果您有超过1,000个工作,您还可以编写一个Python脚本来列出所有工作,方法是批量分配1,000个结果 - 如下所示:

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 = 'XXXXXXXXXXXX'

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,
                          maxResults=1000,
                          pageToken=page_token).execute()

    return jobs_list

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


if __name__ == '__main__':
  main()

答案 1 :(得分:1)

以下shell脚本接近我需要报告的内容。

#!/bin/sh
bq ls  -j `bq show | grep ^Project | awk '{print $2}'` | grep "`date +'%d %b'`" | awk '{print $1}' > tosave.txt

for myjob in `cat tosave.txt`
do
bq ls  -j `bq show | grep ^Project | awk '{print $2}'` | grep $myjob

bq show --format=prettyjson -j $myjob | grep -C2 "message" | head

done