【问题标题】:How to get detailed Big Query error by using PYTHON如何使用 PYTHON 获取详细的 Big Query 错误
【发布时间】:2019-11-24 03:47:20
【问题描述】:

我希望将此信息视为 python 消息:

但目前,我只能看到第一个/第二个

这是我目前正在使用的

from google.api_core.exceptions import BadRequest

if __name__ == '__main__':
      try:
          upload('XXX','XXX')
      except BadRequest as e: 
          print('ERROR: {}'.format(str(e)))

上传:

def upload(FILE_NAME, TABLE_ID):

    client = bigquery.Client()

    dataset_ref = client.dataset(config.DATASET_ID )
    table_ref = dataset_ref.table(TABLE_ID)
    job_config = bigquery.LoadJobConfig()
    job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
    job_config.autodetect = False

    with open(FILE_NAME, 'rb') as source_file:
        job = client.load_table_from_file(
            source_file,
            table_ref,
            location='EU',  # Must match the destination dataset location.
            job_config=job_config)  # API request

    job.result()  # Waits for table load to complete.

【问题讨论】:

标签: python error-handling google-bigquery google-cloud-stackdriver python-bigquery


【解决方案1】:

此错误不会作为异常引发。您可以在 LoadJob 对象的errors 属性中找到它。这是一个例子:

try:
    job.result()
except BadRequest as e:
    for e in job.errors:
        print('ERROR: {}'.format(e['message']))

输出:

ERROR: Error while reading data, error message: JSON table encountered too many errors, giving up. Rows: 1; errors: 1. Please look into the errors[] collection for more details.
ERROR: Error while reading data, error message: JSON processing encountered too many errors, giving up. Rows: 1; errors: 1; max bad: 0; error percent: 0
ERROR: Error while reading data, error message: JSON parsing error in row starting at position 0: No such field: SourceSystem.

【讨论】:

  • 太好了,谢谢。只是一个挑剔,您可以删除as e,因为a)未使用异常,b)e在循环中重新定义
猜你喜欢
  • 1970-01-01
  • 2012-12-26
  • 2016-08-14
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-07
相关资源
最近更新 更多