|
| 1 | +from google.cloud import bigquery |
| 2 | +import os |
| 3 | +from var import * |
| 4 | + |
| 5 | +#Service account key file path |
| 6 | +os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "../key.json" |
| 7 | + |
| 8 | +def create_bq(): |
| 9 | + client = bigquery.Client() |
| 10 | + |
| 11 | + dataset_id = my_dataset |
| 12 | + table_id = my_table |
| 13 | + |
| 14 | + schema = [ |
| 15 | + bigquery.SchemaField("id", "INTEGER"), |
| 16 | + bigquery.SchemaField("name", "STRING"), |
| 17 | + bigquery.SchemaField("email", "STRING"), |
| 18 | + bigquery.SchemaField("description","STRING"), |
| 19 | + bigquery.SchemaField("address","STRING"), |
| 20 | + bigquery.SchemaField("city","STRING"), |
| 21 | + bigquery.SchemaField("state","STRING"), |
| 22 | + bigquery.SchemaField("country","STRING"), |
| 23 | + bigquery.SchemaField("birthdate","DATE"), |
| 24 | + bigquery.SchemaField("password","STRING"), |
| 25 | + bigquery.SchemaField("last_login","TIMESTAMP") |
| 26 | + ] |
| 27 | + |
| 28 | + table_ref = client.dataset(my_dataset).table(my_table) |
| 29 | + table = bigquery.Table(table_ref, schema=schema) |
| 30 | + |
| 31 | + try: |
| 32 | + dataset = client.create_dataset(my_dataset) |
| 33 | + print(f"Created dataset {my_dataset}") |
| 34 | + except: |
| 35 | + print(f"Dataset {my_dataset} already exists") |
| 36 | + |
| 37 | + try: |
| 38 | + table = client.create_table(table) |
| 39 | + print(f"Created table {table_id}") |
| 40 | + except: |
| 41 | + print(f"Table {table_id} already exists") |
0 commit comments