-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupload_to_bigquery.py
54 lines (42 loc) · 1.4 KB
/
upload_to_bigquery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python
import os
import sys
import json
import yaml
from pathlib import Path
from glob import glob
from google.cloud import bigquery
FILE_PATH = Path(__file__).resolve().parent
ENVS_PATH = os.path.join(FILE_PATH, "../envs")
DATA_PATH = os.path.join(FILE_PATH, "../data")
def main():
with open(os.path.join(ENVS_PATH, 'bigquery.config.yml')) as f:
bigquery_config = yaml.load(f, Loader=yaml.Loader)
data_list = []
for file in glob(os.path.join(DATA_PATH, '*.content.info.jsonl')):
with open(file) as f:
for data in f:
data_json = json.loads(data)
for key, value in data_json.items():
if value == '':
data_json[key] = None
data_list.append(data_json)
client = bigquery.Client.from_service_account_json(
os.path.join(ENVS_PATH, bigquery_config['service_account_json'])
)
dataset_reference = bigquery.DatasetReference(
client.project, bigquery_config['dataset_id']
)
table_reference = bigquery.TableReference(
dataset_reference, bigquery_config['table_id']
)
response = client.insert_rows_json(
table_reference, data_list
)
if response:
sys.stderr.write("[ERROR]")
# sys.stderr.write(response)
for i in response :
sys.stderr.write(str(i))
if __name__ == "__main__":
main()