Buy Me a Coffee? Your support is much appreciated!




Source Code:  

import os
from google.cloud import bigquery
from google.cloud.exceptions import NotFound

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'bigquery_service_acct.json'

def check_table(project_id, dataset_id, table_id):
    dataset_ref = bigquery.DatasetReference(project_id, dataset_id)
    table_ref = bigquery.TableReference(dataset_ref, table_id)
    try:
        client.get_table(table_ref)
        return True
    except NotFound:
        return False

def check_dataset(project_id, dataset_id):
    dataset_ref = bigquery.DatasetReference(project_id, dataset_id)
    try:
        client.get_dataset(dataset_ref)
        return True
    except NotFound:
        return False

if __name__ == '__main__':
    client = bigquery.Client()
    print('check_table BigQuery', check_table(client.project, 'Reddit', 'bigquery'))
    print('check_table python', check_table(client.project, 'Reddit', 'python'))
    print('check_dataset Reddit', check_dataset(client.project, 'Reddit'))
    print('check_dataset DataAnalysis', check_dataset(client.project, 'DataAnalysis'))