简体   繁体   中英

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?

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM