-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy_status_writer.py
50 lines (40 loc) · 1.44 KB
/
legacy_status_writer.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
import json
import os
import pandas as pd
import random
print(pd.__version__)
from pandas.io.json import json_normalize #package for flattening json in pandas df
import db.timescaledb as db
'''
param_dic = {
"host" : "tsdb-164cde20-generac-38fe.a.timescaledb.io",
"database" : "defaultdb",
"user" : "tsdbadmin",
"password" : "",
"port" : 12949
}
'''
param_dic = {
"host" : "127.0.0.1",
"database" : "keycloak",
"user" : "keycloak",
"password" : "password",
"port" : 5432
}
#yoyo apply --database postgresql://tsdbadmin:[email protected]:12949/defaultdb ./migrations/legacy-status-updates
#yoyo rollback --database postgresql://tsdbadmin:[email protected]:12949/defaultdb ./migrations/legacy-status-updates
conn = db.connect(param_dic)
def bulk_load_to_timescaledb(conn, dataframe):
# bulk write to timescaledb
db.execute_values(conn, df, "status.legacy_status")
# write dataframe to csv
#df.to_csv('csv/5min_24hr_output.csv', index=False)
if __name__ == "__main__":
data = pd.read_csv('csv/legacy-status-one-day.csv')
for x in range(1000):
rand = int(random.random()*10000)
df = pd.DataFrame(data)
df['device_id'] = df['device_id'] + rand
#bulk_load_to_timescaledb(conn, df)
db.copy_from_stringio(conn, df, "status.legacy_status")
print(x)