-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtosql.py
More file actions
31 lines (29 loc) · 938 Bytes
/
tosql.py
File metadata and controls
31 lines (29 loc) · 938 Bytes
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
import pandas as pd
import json
import os
import glob
import gc
from sqlalchemy import engine
# Create SQL connection
dbengine = engine.create_engine('postgresql://localhost:5432/postgres')
myconnection = dbengine.connect()
# Get a list of JSON files in the current directory
os.chdir('./')
filelist = glob.glob('*.{}'.format('json'))
print("Found for processing\n",filelist, "\n")
# Import files to a dict
# Flatten that dict to a dataframe
# Push the dataframe to SQL
# Delete the DF and do some garbage collection just in case it's giant
for file in filelist:
with open(file) as rawjson:
print("Opening ", file)
rawjson = json.load(rawjson)
print("Flattening ", file)
flattened = pd.json_normalize(rawjson)
print("Appending ", file)
flattened.to_sql('nomad_logs',myconnection,if_exists='append')
del rawjson
del flattened
gc.collect()
print("Finished processing ", file, "\n")