GCP Bigquery Python操作中的异常处理

huangapple go评论75阅读模式
英文:

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:

GCP Bigquery Python操作中的异常处理

英文:

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:

GCP Bigquery Python操作中的异常处理

huangapple
  • 本文由 发表于 2023年6月15日 14:48:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定