Skip to content

Commit a253e0d

Browse files
authored
Create create-bq.py
1 parent a4c4026 commit a253e0d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

BigQuery-with-python/create-bq.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

Comments
 (0)