Possible to create BigQuery Table/Schema without populating with Data?

3.4k views Asked by At

Is it possible to create a Table schema without first populating it with data? Preferably using Google's python client. Google's documentation does not seem to provide a clear yes or no answer. They suggest creating a table with a query, but this is both non-intuitive and again, not highly documented:

1

There are 1 answers

5
Pentium10 On BEST ANSWER

In python you can run an insert job on the tables API endpoint and that will create an empty table as it's documented here you need to supply a TableResource

project_id = <my project>
dataset_id = <my dataset>
table_id = 'table_001'
dataset_ref = {'datasetId': dataset_id,
               'projectId': project_id}
table_ref = {'tableId': table_id,
             'datasetId': dataset_id,
             'projectId': project_id}
schema_ref = {<schema comes here>}
table = {'tableReference': table_ref,
         'schema': schema_ref}
table = bigquery.tables().insert(
    body=table, **dataset_ref).execute(http)

**dataset_ref is a python trick to copy the contents to named arguments

Browse other python + bigquery questions.