This repository has been archived by the owner on Jul 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinflux_db_importer.py
73 lines (43 loc) · 1.49 KB
/
influx_db_importer.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Script to import data from a TXT file to an InfluxDB database
import os
from influxdb import InfluxDBClient
import pandas as pd
import datetime
from datetime import time
from datetime import datetime
import sys
dir_path = os.path.dirname(os.path.realpath(__file__))
city = sys.argv[1]
db_name = 'Bicis_' + city + '_Availability'
db_name_pred = 'Bicis_' + city + '_Prediction'
client = InfluxDBClient('localhost', '8086', 'root', 'root', db_name)
print("> Database created")
client.drop_database(db_name)
client.create_database(db_name)
print("> Pre read file")
dataframe = pd.read_csv(__dir_path + '/data/' + city + '.txt')
print("> File read!")
f = lambda x: datetime.strptime(x, '%Y/%m/%d %H:%M').strftime('%Y-%m-%dT%H:%M:%SZ')
dataframe["datetime"] = dataframe["datetime"].apply(f)
print("> Converted first column to correct datetime format!")
json_body = []
i = 0
for row in dataframe.itertuples():
meas = {}
meas["measurement"] = "bikes"
meas["tags"] = { "station_name" : row[4], "station_id": row[3]}
meas["time"] = row[1]
meas["fields"] = { "value" : str(row[5]) }
json_body.append(meas)
if i % 10000 == 0:
print("Written row " + str(i) + "/" + str(len(dataframe.values)))
client.write_points(json_body)
json_body = []
i += 1
client.write_points(json_body)
client.close()
client = InfluxDBClient('localhost', '8086', 'root', 'root', db_name_pred)
print("> Database created")
client.drop_database(db_name_pred)
client.create_database(db_name_pred)
client.close()