-
Notifications
You must be signed in to change notification settings - Fork 16
/
import.py
131 lines (107 loc) · 4.46 KB
/
import.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
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
from csv_things import import_csv_to_dict
from py2neo import authenticate, Graph
from batch_upload import batch_upload
authenticate("localhost:7474", "neo4j", "neo4j")
graph = Graph()
data = import_csv_to_dict('On_Time_On_Time_Performance_2014_1.csv', headers = True)
query = """
MERGE (oAirport:Airport {id: TOINT({OriginAirportID}) })
SET oAirport.abbr = UPPER({Origin})
MERGE (oCity:City {name: UPPER({OriginCityName}) })
MERGE (oState:State {abbr: UPPER({OriginState}), name: UPPER({OriginStateName}) })
MERGE (oCity)-[:IN_STATE]->(oState)
MERGE (oAirport)-[:IN_CITY]->(oCity)
MERGE (oAirport)-[:IN_STATE]->(oState)
MERGE (dAirport:Airport {id: TOINT({DestAirportID}) })
SET dAirport.abbr = UPPER({Dest})
MERGE (dCity:City {name: UPPER({DestCityName}) })
MERGE (dState:State {abbr: UPPER({DestState}), name: UPPER({DestStateName}) })
MERGE (dCity)-[:IN_STATE]->(dState)
MERGE (dAirport)-[:IN_CITY]->(dCity)
MERGE (dAirport)-[:IN_STATE]->(dState)
CREATE (f:Flight {flight_num: TOINT({FlightNum}) }),
(f)-[:ORIGIN {taxi_time: TOINT({TaxiOut}), dep_delay: TOINT({DepDelay}) }]->(oAirport),
(f)-[:DESTINATION {taxi_time: TOINT({TaxiIn}), arr_delay: TOINT({ArrDelay}) }]->(dAirport)
SET f.year = TOINT({Year}),
f.month = TOINT({Month}),
f.day = TOINT({DayofMonth}),
f.weekday = TOINT({DayOfWeek})
FOREACH (a IN (CASE {TailNum} WHEN "" THEN [] ELSE [{TailNum}] END) |
MERGE (craft:Aircraft {tail_num:a})
MERGE (f)-[:AIRCRAFT]->(craft)
)
FOREACH (c IN (CASE {AirlineID} WHEN "" THEN [] ELSE [TOINT({AirlineID})] END) |
MERGE (car:Carrier {id:c, abbr: UPPER({UniqueCarrier}) })
MERGE (f)-[:CARRIER]->(car)
)
FOREACH (delay IN (CASE {CarrierDelay} WHEN "" THEN []
WHEN "0.00" THEN []
ELSE [TOINT({CarrierDelay})]
END) |
MERGE (r:Reason {name:'CARRIER'})
MERGE (f)-[:DELAYED_BY {time: delay }]->(r)
)
FOREACH (delay IN (CASE {WeatherDelay} WHEN "" THEN []
WHEN "0.00" THEN []
ELSE [TOINT({WeatherDelay})]
END) |
MERGE (r:Reason {name:'WEATHER'})
MERGE (f)-[:DELAYED_BY {time: delay }]->(r)
)
FOREACH (delay IN (CASE {NASDelay} WHEN "" THEN []
WHEN "0.00" THEN []
ELSE [TOINT({NASDelay})]
END) |
MERGE (r:Reason {name:'NATIONAL AIR SYSTEM'})
MERGE (f)-[:DELAYED_BY {time: delay }]->(r)
)
FOREACH (delay IN (CASE {SecurityDelay} WHEN "" THEN []
WHEN "0.00" THEN []
ELSE [TOINT({SecurityDelay})]
END) |
MERGE (r:Reason {name:'SECURITY'})
MERGE (f)-[:DELAYED_BY {time: delay }]->(r)
)
FOREACH (delay IN (CASE {LateAircraftDelay} WHEN "" THEN []
WHEN "0.00" THEN []
ELSE [TOINT({LateAircraftDelay})]
END) |
MERGE (r:Reason {name:'LATE AIRCRAFT'})
MERGE (f)-[:DELAYED_BY {time: delay }]->(r)
)
FOREACH (cancellation IN (CASE {CancellationCode} WHEN "A" THEN ["CARRIER"]
WHEN "B" THEN ["WEATHER"]
WHEN "C" THEN ["NATIONAL AIR SYSTEM"]
WHEN "D" THEN ["SECURITY"]
ELSE []
END) |
MERGE (r:Reason {name:cancellation})
MERGE (f)-[:CANCELLED_BY]->(r)
)
FOREACH (div IN (CASE {Div1AirportID} WHEN "" THEN [] ELSE [TOINT({Div1AirportID})] END) |
MERGE (a:Airport {id:div})
MERGE (f)-[d:DIVERTED_TO]->(a)
SET d.step = 1
)
FOREACH (div IN (CASE {Div2AirportID} WHEN "" THEN [] ELSE [TOINT({Div2AirportID})] END) |
MERGE (a:Airport {id:div})
MERGE (f)-[d:DIVERTED_TO]->(a)
SET d.step = 2
)
FOREACH (div IN (CASE {Div3AirportID} WHEN "" THEN [] ELSE [TOINT({Div3AirportID})] END) |
MERGE (a:Airport {id:div})
MERGE (f)-[d:DIVERTED_TO]->(a)
SET d.step = 3
)
FOREACH (div IN (CASE {Div4AirportID} WHEN "" THEN [] ELSE [TOINT({Div4AirportID})] END) |
MERGE (a:Airport {id:div})
MERGE (f)-[d:DIVERTED_TO]->(a)
SET d.step = 4
)
FOREACH (div IN (CASE {Div5AirportID} WHEN "" THEN [] ELSE [TOINT({Div5AirportID})] END) |
MERGE (a:Airport {id:div})
MERGE (f)-[d:DIVERTED_TO]->(a)
SET d.step = 5
);
"""
batch_upload(graph, data, query)