database_pre3.py 4.38 KB
Newer Older
Tianyang's avatar
Tianyang committed
1
2
3
4
5
6
7
8
9
10
11
import cassandra.cluster
import csv
import re


def connection():
    import cassandra.cluster
    cluster = cassandra.cluster.Cluster(['localhost'])
    session = cluster.connect('caitiany')
    return session

Tianyang's avatar
Tianyang committed
12
def databaseCreate_Q3(session):
Tianyang's avatar
Tianyang committed
13
    query = """ 
Tianyang's avatar
Tianyang committed
14
15
	CREATE TABLE database_kmeans ( 
        date timestamp,
Tianyang's avatar
Tianyang committed
16
17
        lon float,
        lat float,
Tianyang's avatar
Tianyang committed
18
        station varchar,
Tianyang's avatar
Tianyang committed
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
		tmpf float,
		dwpf float,
		relh float,
		drct float,
		sknt float,
		p01i float,
		alti float,
		mslp float,
		vsby float,
		gust float,
		skyc1 varchar,
		skyc2 varchar,
		skyc3 varchar,
		skyc4 varchar,
		skyl1 float,
		skyl2 float,
		skyl3 float,
		skyl4 float,
		wxcodes varchar,
		ice_accretion_1hr float,
		ice_accretion_3hr float,
		ice_accretion_6hr float,
		peak_wind_gust float,
		peak_wind_drct float,
		peak_wind_time varchar,
		feel float,
		metar varchar,
Tianyang's avatar
Tianyang committed
46
		PRIMARY KEY ((date),lon,lat,station)
Tianyang's avatar
Tianyang committed
47
48
	)"""
    session.execute(query)
Tianyang's avatar
Tianyang committed
49
    print("DATA BASE database_kmeans created!")
Tianyang's avatar
Tianyang committed
50
51
52
53
54


def load_data(filename):
    with open(filename) as f:
        for r in csv.DictReader(f):
Tianyang's avatar
Tianyang committed
55
            if not r["valid"]:
Tianyang's avatar
Tianyang committed
56
57
58
59
60
61
62
                continue

            for collonne in r:
                if r[collonne] == "M":
                    r[collonne]= "nan"
            
            data = {}
Tianyang's avatar
Tianyang committed
63
            data["date"] = r["valid"]
Tianyang's avatar
Tianyang committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
            data["station"] = r["station"]
            data["lon"] = float(r["lon"])
            data["lat"] = float(r["lat"])
            data["tmpf"] = float(r["tmpf"])
            data["dwpf"] = float(r["dwpf"])
            data["relh"] = float(r["relh"])
            data["drct"] = float(r["drct"])
            data["sknt"] = float(r["sknt"])
            data["p01i"] = float(r["p01i"])
            data["alti"] = float(r["alti"])
            data["mslp"] = float(r["mslp"])
            data["vsby"] = float(r["vsby"])
            data["gust"] = float(r["gust"])

            data["skyc1"] = r["skyc1"]
            data["skyc2"] = r["skyc2"]
            data["skyc3"] = r["skyc3"]
            data["skyc4"] = r["skyc4"]

            data["skyl1"] = float(r["skyl1"])
            data["skyl2"] = float(r["skyl2"])
            data["skyl3"] = float(r["skyl3"])
            data["skyl4"] = float(r["skyl4"])
            data["wxcodes"] = r["wxcodes"]

            data["ice_accretion_1hr"] = float(r["ice_accretion_1hr"])
            data["ice_accretion_3hr"] = float(r["ice_accretion_3hr"])
            data["ice_accretion_6hr"] = float(r["ice_accretion_6hr"])
            data["peak_wind_gust"] = float(r["peak_wind_gust"])
            data["peak_wind_drct"] = float(r["peak_wind_drct"])
            data["peak_wind_time"] = r["peak_wind_time"]

            data["feel"] = float(r["feel"])
            data["metar"] = r["metar"]

            yield data



Tianyang's avatar
Tianyang committed
103
def insection_sql_Q3(filename,session):
Tianyang's avatar
Tianyang committed
104
105
106
107
    target = load_data(filename)
    i = 1
    for data in target:
        i += 1
Tianyang's avatar
Tianyang committed
108
        k = 0
Tianyang's avatar
Tianyang committed
109
        if (i % 500 == 0):
Tianyang's avatar
Tianyang committed
110
111
112
113
            k += 1
            print(k,". 500 finished.....")
        ligne = (
        data["date"],
Tianyang's avatar
Tianyang committed
114
115
        data["lon"],
        data["lat"],
Tianyang's avatar
Tianyang committed
116
        data["station"],
Tianyang's avatar
Tianyang committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
        data["tmpf"],
        data["dwpf"],
        data["relh"],
        data["drct"],
        data["sknt"],
        data["p01i"],
        data["alti"],
        data["mslp"],
        data["vsby"],
        data["gust"],
        data["skyc1"],
        data["skyc2"],
        data["skyc3"],
        data["skyc4"],
        data["skyl1"],
        data["skyl2"],
        data["skyl3"],
        data["skyl4"],
        data["wxcodes"],
        data["ice_accretion_1hr"],
        data["ice_accretion_3hr"],
        data["ice_accretion_6hr"],
        data["peak_wind_gust"],
        data["peak_wind_drct"],
        data["peak_wind_time"],
        data["feel"],
        data["metar"])

        query = """
Tianyang's avatar
Tianyang committed
146
147
        INSERT INTO database_kmeans(
        date,
Tianyang's avatar
Tianyang committed
148
149
        lon,
        lat,
Tianyang's avatar
Tianyang committed
150
        station,
Tianyang's avatar
Tianyang committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
		tmpf,
		dwpf,
		relh,
		drct,
		sknt,
		p01i,
		alti,
		mslp,
		vsby,
		gust,
		skyc1,
		skyc2,
		skyc3,
		skyc4,
		skyl1,
		skyl2,
		skyl3,
		skyl4,
		wxcodes,
		ice_accretion_1hr,
		ice_accretion_3hr,
		ice_accretion_6hr,
		peak_wind_gust,
		peak_wind_drct,
		peak_wind_time,
		feel,
		metar)
Tianyang's avatar
Tianyang committed
178
        VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
Tianyang's avatar
Tianyang committed
179
180
181
182
183
184
185
186
187
188
        """

        session.execute(query, ligne)





if __name__ == "__main__":
    session = connection()
Tianyang's avatar
Tianyang committed
189
190
    databaseCreate_Q3(session)
    insection_sql_Q3("Projet-NF26/data.csv",session)