我使用以下Python代码从BigQuery中的表中删除了所有数据,当我在运行代码几秒钟后检查该表时,发现该表现在为空,因此删除必须成功。但是,作业状态仍然显示它处于RUNNING状态。
from google.cloud import bigquery
import time
client = bigquery.Client()
job_config = bigquery.QueryJobConfig()
job_config.priority = bigquery.QueryPriority.INTERACTIVE
sql = """DELETE FROM <table_name> WHERE TRUE"""
query_job = client.query( sql, job_config= job_config )
query_job = client.get_job( query_job.job_id ) # API request - fetches job
while query_job.state == "RUNNING":
print( "Job {} is currently in state {}".format( query_job.job_id, query_job.state ) )
time.sleep( 5 )
if query_job.errors != None:
print( "Query Failed." )
raise Exception( "Query Failed. Error: [ %s ]." % query_job.error_result )
这里出了什么问题?为什么作业没有进入“完成”状态?如何通过代码检查是否成功执行?
答案 0 :(得分:3)
诀窍在这里:
query_job = client.get_job( query_job.job_id ) # API request - fetches job
while query_job.state == "RUNNING":
print( "Job {} is currently in state {}".format( query_job.job_id, query_job.state ) )
time.sleep( 5 )
get_job
会在某个时间点返回作业状态,但是在作业状态更改时不会自动更新。相反,您需要在循环中调用get_job
:
while True:
query_job = client.get_job( query_job.job_id ) # API request - fetches job
if query_job.state != "RUNNING":
break
print( "Job {} is currently in state {}".format( query_job.job_id, query_job.state ) )
time.sleep( 5 )