Verify BigQuery table existence
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))