1 回答
TA贡献1775条经验 获得超8个赞
状态跟踪作业进度,如果你需要成功/失败,你要考虑的信息errorResult中的响应。对于成功的工作,这将是None,对于取消的工作,您将得到{u'reason': u'stopped', u'message': u'Job execution was cancelled: User requested cancellation'}。我用来测试这个的代码:
from google.cloud import bigquery
client = bigquery.Client()
project = "[PROJECT-ID]"
states = ["RUNNING", "PENDING", "SUCCESSFUL", "CANCELLED", "FAILED"]
def returnState(job):
if job.state == "DONE":
if job.error_result is None:
return "SUCCESSFUL"
elif job.error_result['reason'] == u'stopped':
return "CANCELLED"
else:
return "FAILED"
else:
return job.state
jobs = [job for job in client.list_jobs(project=project, max_results=10)]
for state in states:
matching_jobs = [job for job in jobs if returnState(job) == state]
for job in matching_jobs:
print "Job ID: {0}, State: {1}, Error Result: {2}".format(job.job_id, state, job.error_result)
这将打印出如下内容:
$ python bq-status.py
Job ID: bquijob_..., State: SUCCESSFUL, Error Result: None
Job ID: bquijob_..., State: SUCCESSFUL, Error Result: None
Job ID: job_..., State: SUCCESSFUL, Error Result: None
Job ID: job_..., State: SUCCESSFUL, Error Result: None
Job ID: job_..., State: SUCCESSFUL, Error Result: None
Job ID: job_..., State: SUCCESSFUL, Error Result: None
Job ID: scheduled_query_..., State: SUCCESSFUL, Error Result: None
Job ID: bquijob_..., State: SUCCESSFUL, Error Result: None
Job ID: bquijob_..., State: CANCELLED, Error Result: {u'reason': u'stopped', u'message': u'Job execution was cancelled: User requested cancellation'}
Job ID: bquijob_..., State: FAILED, Error Result: {u'reason': u'invalidQuery', u'message': u'Syntax error: Illegal input character "\\\\" at [2:18]', u'location': u'query'}
请记住,负载的工作可能是成功的,但允许一些maxBadRecords,这样errorResult将不为空,等等。
添加回答
举报