Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify BigQuery table existence

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?

like image 868
CodeTrooper Avatar asked Nov 22 '17 17:11

CodeTrooper


3 Answers

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
like image 130
Willian Fuks Avatar answered Oct 10 '22 05:10

Willian Fuks


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))
like image 43
Wolfram Arnold Avatar answered Oct 10 '22 05:10

Wolfram Arnold


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

like image 1
Daria Avatar answered Oct 10 '22 04:10

Daria