在bigquery中重试更新似乎不起作用

时间:2018-04-19 17:04:57

标签: python google-bigquery

当我运行以下代码时:

bq_client = google.cloud.bigquery.Client(project='project')
query1 = """
UPDATE dataset.table SET col = 1 where id = 'a' 
"""
query2 = """
UPDATE dataset.table SET col = 2 where id = 'b' 
"""
p = google.api_core.retry.if_exception_type(Exception)
r = google.api_core.retry.Retry(predicate=p)
bq_client.query(query1)
bq_client.query(query2, retry=r)

第一个查询作业成功但第二个查询作业失败(因为bigquery不支持同时更新)并且似乎永远不会重试。但是,我传递了一个“重试”对象,该对象捕获了我的第二个查询作业的所有异常。我不明白为什么不重试第二个查询作业。

2 个答案:

答案 0 :(得分:4)

I think the reason is that the retry option retries the API call to insert the job, but it doesn't actually retry the job for you.

In this case the API "succeeds" (at least as far as HTTP error codes are concerned), but the response body says the job failed.

答案 1 :(得分:2)

python中的解决方法:

max_tries = 7
def myJob():
   for n in range(max_tries):
       try:
           myQueryJob()
       except Exception:
           if n == max_tries - 1:
               raise Exception
           time.sleep(2**n)

它会通过一种指数退避来尝试QueryJob最多7次。