Here is a snippet of Python code that will tell if a table exists (deleting it in the process - carefully!):
def doesTableExist(project_id, dataset_id, table_id): bq.tables().delete( projectId=project_id, datasetId=dataset_id, tableId=table_id).execute() return False
On the other hand, if you do not want to delete the table in the process, you can try:
def doesTableExist(project_id, dataset_id, table_id): try: bq.tables().get( projectId=project_id, datasetId=dataset_id, tableId=table_id).execute() return True except HttpError, err if err.resp.status <> 404: raise return False
If you want to know where bq came from, you can call build_bq_client here: http://code.google.com/p/bigquery-e2e/source/browse/samples/ch12/auth.py
In general, if you use this to check whether you should run a task that changes the table, it might be a good idea to just complete the task anyway and use WRITE_TRUNCATE as the record location.
Another approach could be to create a predictable job identifier and re-run the job with that identifier. If the task already exists, the task is already completed (however, you can check it again to make sure that the task did not fail).
Jordan tigani
source share