I have a simple function to determine whether a table exists or not:
def check_users_usersmetadata_existence():
"""
Checks if the table Prod_UserUserMetadata exists
"""
app_id = get_app_id()
bigquery_client = bigquery.Client(project=app_id)
dataset_ref = bigquery_client.dataset('Backup')
table_ref = dataset_ref.table('Prod_UserUserMetadata')
try:
table = bigquery_client.get_table(table_ref)
if table:
print('Table {}\'s existence sucessfully proved!'.format(table_ref))
return True
except HttpError as error:
raise
print('Whoops! Table {} doesn\'t exist here! Ref: {}'.format(table_ref, error.resp.status))
return False
Problem is, it throws a 404 on this line table = bigquery_client.get_table(table_ref)
which is ok because the table shouldn't exist. But it won't continue to process the rest of the script. I'm trying to parse it inside a try except
wrapper however it's not working. How would I parse this?
Your script is not entering the exception clause as it raises a NotFound
error and not a HttpError
.
This should work:
from google.cloud.exceptions import NotFound
def check_users_usersmetadata_existence():
# (...)
try:
table = bigquery_client.get_table(table_ref)
if table:
print('Table {}\'s existence sucessfully proved!'.format(table_ref))
return True
except NotFound as error:
# ...do some processing ...
print('Whoops! Table {} doesn\'t exist here! Ref: {}'.format(table_ref, error.resp.status))
return False
See the example in the official documentation of the BigQuery Python client: https://googleapis.dev/python/bigquery/latest/usage/tables.html#getting-a-table
Excerpt:
from google.cloud import bigquery
from google.cloud.exceptions import NotFound
client = bigquery.Client()
# table_id = "your-project.your_dataset.your_table"
try:
client.get_table(table_id) # Make an API request.
print("Table {} already exists.".format(table_id))
except NotFound:
print("Table {} is not found.".format(table_id))
Try using except NotFound NotFound is defined in https://github.com/googleapis/google-cloud-python/blob/release-core-1.3.0/api_core/google/api_core/exceptions.py#L219
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With