英文:
GCP Bigquery exception handling in Python operator
问题
I am trying to catch any error, when I ran query in Bigquery through Python Operator in airflow. But I am not able to get the expected behaviour. Its not throwing any exception.
This is what I have tried.
from google.cloud import bigquery
client = bigquery.Client(project='project_id')
query = "select 1 as cnt from gudb.TEST001 limit 1"
try:
client.query(query)
except Exception as e:
print(e)
its not throwing any exception, even the table is not available in bigquery.
But when I tried to store the variable
var=client.query(query) # in try
for x in var:
print(x)
I can see the exception stored in that var. But I am expecting to get the exception to be prompted in the stdout when I try to run the try-except block.
Can someone please let me know, how to get the exception
英文:
I am trying to catch any error, when I ran query in Bigquery through Python Operator in airflow. But I am not able to get the expected behaviour. Its not throwing any exception.
This is what I have tried.
from google.cloud import bigquery
client = bigquery.Client(project='project_id')
query = "select 1 as cnt from gudb.TEST001 limit 1"
try:
client.query(query)
except Exception as e:
print(e)
its not throwing any exception, even the table is not available in bigquery.
But when I tried to store the variable
var=client.query(query) # in try
for x in var:
print(x)
I can see the exception stored in that var. But I am expecting to get the exception to be prompted in the stdout when I try to run the try except block.
Can someone please let me know, how to get the exception
答案1
得分: 1
You can find the error status of the query by calling the result
function. For your requirement you can consider the following code:
try:
client.query(query).result()
except Exception as e:
print(e)
Example:
def bigquerytest():
client = bigquery.Client()
query = "SELECT * FROM `my-project-36069-km.demo12.vvvvvvv`"
try:
query_job = client.query(query)
query_job.result()
except Exception as s:
print(f'The error is {s}')
with DAG(dag_id='ne', start_date=datetime(2021, 4, 5, 15, 0), schedule_interval='@daily', catchup=False) as dag:
t1 = BashOperator(task_id='print', bash_command='echo finished')
t2 = PythonOperator(task_id='bigquery', python_callable=bigquerytest)
t2 >> t1
Error:
英文:
You can find the error status of the query by calling the result
function.For your requirement you can consider the following code:
try:
client.query(query).result()
except Exception as e:
print(e)
Example:
def bigquerytest():
client = bigquery.Client()
query = "SELECT * FROM `my-project-36069-km.demo12.vvvvvvv`"
try:
query_job = client.query(query)
query_job.result()
except Exception as s:
print(f'The error is {s}')
with DAG(dag_id='ne',start_date=datetime(2021, 4, 5, 15, 0),schedule_interval='@daily',catchup=False) as dag:
t1=BashOperator(task_id='print',bash_command='echo finished')
t2=PythonOperator(task_id='bigquery',python_callable=bigquerytest)
t2>>t1
Error:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论