-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataInsertion.py
More file actions
410 lines (325 loc) · 12.9 KB
/
DataInsertion.py
File metadata and controls
410 lines (325 loc) · 12.9 KB
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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
146
147
148
149
150
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# -*- coding: utf-8 -*-
"""
This is a file to insert data.
@author: shellyli
"""
import json
import time
import psycopg2
import re
# 读取 JSON 文件
with open('Data_v1/cards.json') as cards_f:
card_d = json.load(cards_f)
with open('Data_v1/passenger.json') as passenger_f:
passenger_d = json.load(passenger_f)
with open('Data_v1/ride.json') as ride_f:
ride_d = json.load(ride_f)
with open('Data_v1/stations.json') as stations_f:
stations_d = json.load(stations_f)
with open('Data_v1/lines.json') as lines_f:
lines_d = json.load(lines_f)
# 连接到 PostgreSQL 数据库
conn = psycopg2.connect(
dbname="project1",
user="checker",
password="123456",
host="localhost",
port="5432"
)
cursor = conn.cursor()
start_time_total = time.time()
'''将数据插入数据库'''
# card
start_time_1 = time.time()
for item in card_d:
keys = ','.join(item.keys())
values = ','.join(['%s'] * len(item))
insert_query = "INSERT INTO card ({}) VALUES ({})".format(keys, values)
cursor.execute(insert_query, list(item.values()))
end_time_1 = time.time()
total_time_1 = end_time_1 - start_time_1
print(f"Table card data insertion time: {total_time_1} seconds")
# passenger
start_time_2 = time.time()
key_mapping = {
"id_number": "id",
"name": "name",
"phone_number": "phone_number",
"gender": "gender",
"district": "district"
}
for item in passenger_d:
insert_query = "INSERT INTO passenger({}) VALUES ({})".format(
','.join(key_mapping.values()),
','.join(['%s'] * len(key_mapping))
)
column_values = [item.get(key, None) for key in key_mapping.keys()]
cursor.execute(insert_query, column_values)
end_time_2 = time.time()
total_time_2 = end_time_2 - start_time_2
print(f"Table passenger data insertion time: {total_time_2} seconds")
# line
start_time_3 = time.time()
def insert_data(data, table_name, tmp_dic):
key_mapping = {
"name": "name",
"start_time": "start_time",
"end_time": "end_time",
"intro": "intro",
"mileage": "mileage",
"color" : "color",
"first_opening": "first_opening",
"url": "url"
}
for key, value in data.items():
tmp_dic['name'] = (key,)
for key, value in value.items():
tmp_dic[key] = value
insert_query = "INSERT INTO {}({}) VALUES ({})".format(
table_name,
','.join(key_mapping.values()),
','.join(['%s'] * len(key_mapping))
)
column_values = [tmp_dic.get(key, None) for key in key_mapping.keys()]
cursor.execute(insert_query, column_values)
insert_data(lines_d, 'line', {})
end_time_3 = time.time()
total_time_3 = end_time_3 - start_time_3
print(f"Table line data insertion time: {total_time_3} seconds")
# station
start_time_4 = time.time()
def insert_data(data, table_name, tmp_dic):
key_mapping = {
"english_name":"English_name",
"district": "district",
"intro": "intro",
"chinese_name":"Chinese_name"
}
for key, value in data.items():
tmp_dic['english_name'] = (key,)
for key, value in value.items():
tmp_dic[key] = value
insert_query = "INSERT INTO {}({}) VALUES ({})".format(
table_name,
','.join(key_mapping.values()),
','.join(['%s'] * len(key_mapping))
)
column_values = [tmp_dic.get(key, None) for key in key_mapping.keys()]
cursor.execute(insert_query, column_values)
insert_data(stations_d, 'station', {})
end_time_4 = time.time()
total_time_4 = end_time_4 - start_time_4
print(f"Table station data insertion time: {total_time_4} seconds")
# line_detail
start_time_5 = time.time()
tmp_dic = {}
for key, value in lines_d.items():
tmp_dic[key] = value['stations']
for key, values in tmp_dic.items():
for value in values:
if "'" in value:
value_correct = value.replace("'", "''")
else:
value_correct = value
insert_query = "INSERT INTO line_detail(line_name, station_name) VALUES ('{}', '{}')".format(key, value_correct)
cursor.execute(insert_query)
end_time_5 = time.time()
total_time_5 = end_time_5 - start_time_5
print(f"Table line_detail data insertion time: {total_time_5} seconds")
# ride
start_time_6 = time.time()
key_mapping_passenger = {
"user": "passenger_id",
"start_station": "start_station",
"end_station": "end_station",
"start_time": "start_time",
"end_time": "end_time",
"price" : "price"
}
key_mapping_code = {
"user": "card_code",
"start_station": "start_station",
"end_station": "end_station",
"start_time": "start_time",
"end_time": "end_time",
"price" : "price"
}
for item in ride_d: # item: dic
if len(item['user']) == 18:
insert_query = "INSERT INTO passenger_ride({}) VALUES ({})".format(
','.join(key_mapping_passenger.values()),
','.join(['%s'] * len(key_mapping_passenger))
)
column_values = [item.get(key, None) for key in key_mapping_passenger.keys()]
cursor.execute(insert_query, column_values)
elif len(item['user']) == 9:
insert_query = "INSERT INTO card_ride({}) VALUES ({})".format(
','.join(key_mapping_code.values()),
','.join(['%s'] * len(key_mapping_code))
)
column_values = [item.get(key, None) for key in key_mapping_code.keys()]
cursor.execute(insert_query, column_values)
end_time_6 = time.time()
total_time_6 = end_time_6 - start_time_6
print(f"Table ride data insertion time: {total_time_6} seconds")
# exit
start_time_7 = time.time()
for key, value in stations_d.items():
tmp_dic = {}
if "'" in key:
key_correct = key.replace("'", "''")
else:
key_correct = key
query = "SELECT * FROM station WHERE English_name = '{}'".format(key_correct)
cursor.execute(query)
result = cursor.fetchall()
for row in result:
station_id = row[0]
tmp_dic[station_id] = []
# {district:, bus_info:, out_info:, intro:, chinese_name:}
for key, value in value.items():
if key == 'out_info':
for item in value: # item: dic{outt:xx, textt:xx}
tmp_dic[station_id].append(item['outt'].strip())
for key, values in tmp_dic.items():
for value in values:
insert_query = "INSERT INTO exit(station_id, number) VALUES ('{}', '{}')".format(key, value)
cursor.execute(insert_query)
end_time_7 = time.time()
total_time_7 = end_time_7 - start_time_7
print(f"Table exit data insertion time: {total_time_7} seconds")
# buildings
start_time_8 = time.time()
for key, value in stations_d.items():
tmp_dic = {}
if "'" in key:
key_correct = key.replace("'", "''")
else:
key_correct = key
query = "SELECT * FROM station WHERE english_name = '{}'".format(key_correct)
cursor.execute(query)
result = cursor.fetchall()
for row in result:
station_id = row[0]
for inner_key, inner_value in value.items():
if inner_key == 'out_info':
for item in inner_value: # item: dic{outt:xx, textt:xx}
query = "SELECT * FROM exit WHERE number = '{}' and station_id = {}".format(item['outt'].strip(), station_id)
cursor.execute(query)
result = cursor.fetchall()
for row in result:
exit_id = row[0]
tmp_dic[exit_id] = item['textt'].split('、')
else:
continue
for key, values in tmp_dic.items():
for value in values:
insert_query = "INSERT INTO buildings(exit_id, name) VALUES ('{}', '{}')".format(key, value)
cursor.execute(insert_query)
end_time_8 = time.time()
total_time_8 = end_time_8 - start_time_8
print(f"Table buildings data insertion time: {total_time_8} seconds")
# bus_stop(Corrected in 5.9)
start_time_9 = time.time()
def update_dict(dic, key, value):
if key not in dic or (isinstance(dic[key], list) and value not in dic[key]):
dic[key] = value
elif isinstance(dic[key], list):
dic[key].append(value)
else:
dic[key] = [dic[key], value]
return dic
for key, value in stations_d.items():
tmp_dic = {}
if "'" in key:
key_correct = key.replace("'", "''")
else:
key_correct = key
query = "SELECT * FROM station WHERE english_name = '{}'".format(key_correct)
cursor.execute(query)
result = cursor.fetchall()
for row in result:
station_id = row[0]
for inner_key, inner_value in value.items():
if inner_key == 'bus_info':
for item in inner_value: # item: dic{busOutInfo:[{x:xx, y:yy}], chukou:xx}
query = "SELECT * FROM exit WHERE number = '{}' and station_id = {}".format(item['chukou'].strip(), station_id)
cursor.execute(query)
result = cursor.fetchall()
for row in result:
exit_id = row[0]
for key_i, value_i in item.items():
if isinstance(value_i, list):
for item_i in value_i:
bus_stop_name = item_i['busName']
update_dict(tmp_dic, exit_id, bus_stop_name)
else:
continue
"print(tmp_dic)"
for key, value in tmp_dic.items():
if isinstance(value, list):
for item in value:
insert_query = "INSERT INTO bus_stop(exit_id, name) VALUES ('{}', '{}')".format(key, item)
cursor.execute(insert_query)
else:
insert_query = "INSERT INTO bus_stop(exit_id, name) VALUES ('{}', '{}')".format(key, value)
cursor.execute(insert_query)
end_time_9 = time.time()
total_time_9 = end_time_9 - start_time_9
print(f"Table bus_stop data insertion time: {total_time_9} seconds")
# bus_line(Corrected in 5.9)
start_time_10 = time.time()
for key, value in stations_d.items():
tmp_dic = {}
if "'" in key:
key_correct = key.replace("'", "''")
else:
key_correct = key
query = "SELECT * FROM station WHERE english_name = '{}'".format(key_correct)
cursor.execute(query)
result = cursor.fetchall()
for row in result:
station_id = row[0]
for inner_key, inner_value in value.items():
if inner_key == 'bus_info':
for item in inner_value: # item: dic{busOutInfo:[{x:xx, y:yy}], chukou:xx}
query = "SELECT * FROM exit WHERE number = '{}' and station_id = {}".format(item['chukou'].strip(), station_id)
cursor.execute(query)
result = cursor.fetchall()
for row in result:
exit_id = row[0]
for key_i, value_i in item.items():
if isinstance(value_i, list):
for item_s in value_i:
bus_stop_name = item_s['busName']
query = "SELECT * FROM bus_stop WHERE exit_id = '{}' and name = '{}'".format(exit_id,bus_stop_name)
cursor.execute(query)
result = cursor.fetchall()
for row in result:
bus_stop_id = row[0]
bus_line = item_s['busInfo']
target = re.split(r'[,,;.、\s]+', bus_line) # 引入正则表达式
tmp_dic[bus_stop_id] = target
else:
continue
"print(tmp_dic)"
for key, values in tmp_dic.items():
for value in values:
insert_query = "INSERT INTO bus_line(bus_stop_id, name) VALUES ('{}', '{}')".format(key, value)
# 在执行插入操作之前,先检查数据库中是否已经存在相同的记录
check_query = "SELECT * FROM bus_line WHERE bus_stop_id = '{}' AND name = '{}'".format(key, value)
cursor.execute(check_query)
existing_record = cursor.fetchone()
# 如果数据库中不存在相同的记录,则执行插入操作
if not existing_record:
cursor.execute(insert_query)
end_time_10 = time.time()
total_time_10 = end_time_10 - start_time_10
print(f"Table bus_line data insertion time: {total_time_10} seconds")
# 提交更改并关闭连接
conn.commit()
cursor.close()
conn.close()
end_time_total = time.time() # 记录结束时间
total_time = end_time_total - start_time_total
print(f"Total time for data insertion: {total_time} seconds")