【问题标题】:BigQuery job status logicBigQuery 作业状态逻辑
【发布时间】:2014-10-14 16:00:08
【问题描述】:

我正在使用此逻辑来捕获 BigQuery 作业 是成功与否,但有时我是成功的工作 ID,即使由于工作运行 查询但没有插入行。

这主要发生在对表的查询中。

我使用了我在谷歌文档中看到的代码,并为我添加了一些日志。

如果有人能告诉我我做错了什么,那就太好了。

def _wait_for_response(self, bq_api, insert_response, max_wait_time=3600): """获取 bigQuery 作业状态。等待 DONE 并检查错误。 如果存在错误 - 引发异常"""

    start_time = time.time()
    logstr.info(current_module='bq_session',
                current_func='_wait_for_response')

    # sleep interval between retries
    # first, try 8 times every 1 second, then double sleep time until
    # 30 seconds (and stay on 30 until max_wait_time is reached)
    sleep = itertools.chain(itertools.repeat(1, 8), xrange(2, 30, 3),
                            itertools.repeat(30))

    while time.time() - start_time < max_wait_time:

        try:
            job = bq_api.jobs().get(
                projectId=insert_response['jobReference']['projectId'],
                jobId=insert_response['jobReference']['jobId']).execute()

            # on job end
            if job['status']['state'] == 'DONE':
                # if job failed raise error(s)
                if 'errors' in job['status'].keys() and\
                        job['status']['errors']:
                    raise Exception(','.join(
                        [err['message']
                         for err in job['status']['errors']]))
                else:
                    return job

        except apiclient.errors.HttpError, error:
            status = int(error.resp.get('status', 0))
            if status >= 500:
                pass
              # raise Exception(
              #     global_messages.BQ_SERVER_ERROR.format(err=error))
            elif status == 404:
              raise Exception(
                  global_messages.BQ_JOB_NOT_FOUND.format(
                      jobid=insert_response['jobReference']['jobId']))
            else:
              raise Exception(
                  global_messages.BQ_ERROR_GETTING_JOB_STATUS.format(
                                                                err=error))

        time.sleep(sleep.next())

    raise Exception(global_messages.BQ_TIMEOUT.format(
                        time=max_wait_time,
                        jobid=insert_response['jobReference']['jobId']))

【问题讨论】:

  • 请提供有关您遇到的问题的更多信息。
  • 根据我添加的脚本,我知道我的工作是否成功运行的方式。有时我看到该作业没有将数据加载到我的表中,即使我的状态已完成且没有错误。当我将工作 ID 发送给谷歌支持时,他们回答说我的工作由于服务器错误(内部错误)而失败,我应该抓住这个错误。我想知道为什么我添加的脚本逻辑没有捕捉到它。

标签: google-bigquery


【解决方案1】:

如果状态大于 500,脚本的这些行会导致控制失效。

if status >= 500:
    pass
    # raise Exception(
    #     global_messages.BQ_SERVER_ERROR.format(err=error))

这可能会阻止您看到预期的异常。

【讨论】:

  • 这是真的。我是根据谷歌最佳实践来做的,否则我的工作每小时都会因为不重要的内部警告而失败。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-16
  • 2012-07-15
  • 2022-10-25
  • 2018-11-18
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多