Skip to content

Commit f89b122

Browse files
authored
Merge pull request #2 from SukiGit2021/SukiGit2021/Database-Structure/Graph-Database
Add files via upload
2 parents cb416b8 + 6bcd33d commit f89b122

6 files changed

+610
-0
lines changed

FIT5137_A2Codes.zip

8.22 KB
Binary file not shown.

FIT5137_A2Report.pdf

3.36 MB
Binary file not shown.

TaskC1.cypher

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
//Student Name: PEIYU LIU
2+
//Student ID: 31153291
3+
4+
// Task C1 Database Design.
5+
6+
// ● Create a new project called MASL.
7+
//DONE
8+
// ● Create a new graph called MASLgraph.
9+
//DONE
10+
11+
// import csv to neo4j graph
12+
//Analysis
13+
//ufo: individual_nodes:[year,day,month,hour, shape,conds,wdire,location]
14+
//state: state-countyname-city[latitude,longitude]
15+
//Potential nodes: UFO,YEAR,DAY,MONTH,HOUR,SHAPE,WEATHER_CONDITION,WIND_DIRECTION,LOCATION(CITY,COUNTYNAME,STATE)
16+
//Potential relationships:
17+
// (UFO)-[:IS_HAPPENED_IN]->(LOCATION)
18+
// (UFO) -[:IS_SHAPE_OF]-> (SHAPE)
19+
// (UFO) -[:IS_WEAHTER_CONDITION_OF]-> (WEAHTER_CONDITION)
20+
// (UFO) -[:IS_WEAHTER_DIRECTION_OF]-> (WEAHTER_DIRECTION)
21+
// (UFO) -[:IN_MONTH_OF]-> (MONTH)
22+
// (UFO) -[:IN_YEAR_OF]-> (YEAR)
23+
// (UFO) -[:IN_DAY_OF]-> (DAY)
24+
// (UFO) -[:IN_HOUR_OF]-> (HOUR)
25+
26+
//Import state
27+
LOAD CSV WITH HEADERS FROM 'file:///states_a2.csv' AS data
28+
WITH data
29+
// check null value
30+
WHERE data.city IS NOT NULL
31+
// set node with properties[city,county,state,lat,lng]
32+
// ensure data format is right using toUpper and toFloat
33+
MERGE(location:Location{
34+
city:toUpper(data.city),
35+
lat:toFloat(data.lat),
36+
lng:toFloat(data.lng),
37+
countyName:toUpper(data.countyName),
38+
state:toUpper(data.state)});
39+
40+
//Import ufo
41+
LOAD CSV WITH HEADERS FROM 'file:///ufo_a2.csv' AS data
42+
WITH data
43+
//Create nodes for each row in ufo.csv
44+
CREATE(u:UFO{
45+
})
46+
// give conditions to check null value: CASE X WHEN NULL THEN NULL ELSE X
47+
// match data value with csv file, `` to avoid space between variables' name
48+
SET u.duration = (CASE data.`duration` WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE trim(data.`duration`)END)
49+
// trim to remove empty space in front of string value or in the end of string value
50+
SET u.text = (CASE data.`text` WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE trim(data.`text`)END)
51+
SET u.summary = (CASE data.`summary` WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE trim(data.`summary`)END)
52+
// ensure float and integer format when I set value to my properties
53+
SET u.pressure = (CASE toFloat(data.`pressure`) WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toFloat(data.`pressure`)END)
54+
SET u.temp = (CASE toFloat(data.`temp`) WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toFloat(data.`temp`)END)
55+
SET u.hail = (CASE toInteger(data.`hail`) WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toInteger(data.`hail`)END)
56+
SET u.heatindex = (CASE toFloat(data.`heatindex`) WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toFloat(data.`heatindex`)END)
57+
SET u.windchill = (CASE data.`windchill` WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toFloat(data.`windchill`)END)
58+
SET u.rain = (CASE data.`rain` WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toInteger(data.`rain`)END)
59+
SET u.vis = (CASE data.`vis` WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toFloat(data.`vis`)END)
60+
SET u.dewpt = (CASE data.`dewpt` WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toFloat(data.`dewpt`)END)
61+
SET u.thunder = (CASE data.`thunder` WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toInteger(data.`thunder`)END)
62+
SET u.fog = (CASE data.fog WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toInteger(data.`fog`)END)
63+
SET u.precip= (CASE data.precip WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toFloat(data.`precip`)END)
64+
SET u.wspd= (CASE data.wspd WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toFloat(data.`wspd`)END)
65+
SET u.tornado= (CASE data.tornado WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toInteger(data.`tornado`)END)
66+
SET u.hum= (CASE data.hum WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toFloat(data.`hum`)END)
67+
SET u.snow= (CASE data.snow WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toInteger(data.`snow`)END)
68+
SET u.wgust= (CASE data.wgust WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toFloat(data.`wgust`) END)
69+
SET u.city= (CASE data.city WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toUpper(data.`city`)END)
70+
SET u.state= (CASE data.state WHEN "" THEN NULL WHEN "NA" THEN NULL ELSE toUpper(data.`state`) END)
71+
// traverse each row and unwind shape as individual node for query's convenience.
72+
// CASE WHEN ELSE END to deal with null value.
73+
// Create relationships with UFO
74+
FOREACH(IGNOREME IN CASE data.shape WHEN NULL THEN NULL WHEN "NA" THEN NULL ELSE toUpper(trim(data.shape)) END|
75+
MERGE (s:Shape {shape:toUpper(trim(data.shape))})
76+
MERGE (u) -[:IS_SHAPE_OF]-> (s)
77+
)
78+
// traverse each row and unwind weahter condition as individual node for query's convenience.
79+
// CASE WHEN ELSE END to deal with null value.
80+
// Create relationships with UFO
81+
FOREACH(IGNOREME IN CASE data.conds WHEN NULL THEN NULL WHEN "NA" THEN NULL ELSE toUpper(trim(data.conds)) END|
82+
MERGE (c:Conds {condition:toUpper(trim(data.conds))})
83+
MERGE (u) -[:IS_WEAHTER_CONDITION_OF]-> (c)
84+
)
85+
// traverse each row and unwind weather direction as individual node for query's convenience.
86+
// CASE WHEN ELSE END to deal with null value.
87+
// Create relationships with UFO
88+
FOREACH(IGNOREME IN CASE data.wdire WHEN NULL THEN NULL WHEN "NA" THEN NULL ELSE toUpper(trim(data.wdire)) END|
89+
MERGE (wd:Wdire {direction:toUpper(trim(data.wdire))})
90+
MERGE (u) -[:IS_WEAHTER_DIRECTION_OF]-> (wd)
91+
)
92+
// traverse each row and unwind month as individual node for query's convenience.
93+
// CASE WHEN ELSE END to deal with null value.
94+
// Create relationships with UFO
95+
FOREACH(IGNOREME IN CASE data.month WHEN NULL THEN NULL WHEN "NA" THEN NULL ELSE data.month END|
96+
MERGE (m:Month {month:data.month})
97+
MERGE (u) -[:IN_MONTH_OF]-> (m)
98+
)
99+
// traverse each row and unwind year as individual node for query's convenience.
100+
// CASE WHEN ELSE END to deal with null value.
101+
// Create relationships with UFO
102+
FOREACH(IGNOREME IN CASE data.year WHEN NULL THEN NULL WHEN "NA" THEN NULL ELSE data.year END|
103+
MERGE (y:Year {year:data.year})
104+
MERGE (u) -[:IN_YEAR_OF]-> (y)
105+
)
106+
// traverse each row and unwind day as individual node for query's convenience.
107+
// CASE WHEN ELSE END to deal with null value.
108+
// Create relationships with UFO
109+
FOREACH(IGNOREME IN CASE data.day WHEN NULL THEN NULL WHEN "NA" THEN NULL ELSE data.day END|
110+
MERGE (d:Day {day:data.day})
111+
MERGE (u) -[:IN_DAY_OF]-> (d)
112+
)
113+
// traverse each row and unwind hour as individual node for query's convenience.
114+
// CASE WHEN ELSE END to deal with null value.
115+
// Create relationships with UFO
116+
FOREACH(IGNOREME IN CASE data.hour WHEN NULL THEN NULL WHEN "NA" THEN NULL ELSE data.hour END|
117+
MERGE (h:Hour {hour:data.hour})
118+
MERGE (u) -[:IN_HOUR_OF]-> (h)
119+
);
120+
121+
//Create relationships between UFO and where was it happened.
122+
LOAD CSV WITH HEADERS FROM 'file:///ufo_a2.csv' AS data
123+
WITH data
124+
// MATCH right UFO and Location nodes.
125+
MATCH(u:UFO{
126+
city:toUpper(data.city),
127+
state:toUpper(data.state),
128+
text:data.text,
129+
summary:data.summary,
130+
pressure:toFloat(data.pressure)})
131+
MATCH(location:Location{city:toUpper(data.city),state:toUpper(data.state)})
132+
// create relationship
133+
CREATE(u)-[:IS_HAPPENED_IN]->(location);
134+
// change year,month,hour,day to int format.
135+
MATCH (y:Year) SET y.year = toInteger(y.year);
136+
MATCH (y:Month) SET y.month = toInteger(y.month);
137+
MATCH (y:Day) SET y.day = toInteger(y.day);
138+
MATCH (y:Hour) SET y.hour = toInteger(y.hour);

TaskC2.cypher

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
//Student Name: PEIYU LIU
2+
//Student ID: 31153291
3+
4+
//Queries
5+
// CREATE INDEXES FOR efficient queries
6+
// state, city and countyName are used frequently.
7+
CREATE INDEX ON:Location(state);
8+
CREATE INDEX ON:Location(city);
9+
CREATE INDEX ON:Location(countyName);
10+
// :schema check indexes
11+
12+
// 1. month = 4 UFO records
13+
//Aprial = 4
14+
MATCH(u:UFO)-[:IN_MONTH_OF]->(m:Month)
15+
WHERE m.month =4
16+
RETURN COUNT(u) AS UFO_records;
17+
// ╒═════════════╕
18+
// │"UFO_records"│
19+
// ╞═════════════╡
20+
// │102 │
21+
// └─────────────┘
22+
23+
//2. unique weather conditions in lowercase letters.
24+
// condition year<2014, state = 'AZ', shape = 'CIRCLE'
25+
MATCH(u:UFO)-[:IN_YEAR_OF]->(y:Year),
26+
(u:UFO)-[:IS_WEAHTER_CONDITION_OF]->(c:Conds),
27+
(u:UFO)-[:IS_HAPPENED_IN]->(l:Location{state:'AZ'}),
28+
(u:UFO)-[:IS_SHAPE_OF]->(s:Shape{shape:'CIRCLE'})
29+
WHERE y.year<2014
30+
RETURN DISTINCT toLower(c.condition) AS `unique weather conditions`;
31+
// ╞══════════════════╡
32+
// │"SCATTERED CLOUDS"│
33+
// ├──────────────────┤
34+
// │"CLEAR" │
35+
// ├──────────────────┤
36+
// │"MOSTLY CLOUDY" │
37+
// ├──────────────────┤
38+
// │"PARTLY CLOUDY" │
39+
// ├──────────────────┤
40+
// │"OVERCAST" │
41+
// └──────────────────┘
42+
43+
//3. unique UFO shapes that appeared in 2015 but not in 2000
44+
MATCH(u:UFO)-[:IN_YEAR_OF]->(y:Year{year:2015}),(u:UFO)-[:IS_SHAPE_OF]->(s:Shape)
45+
WHERE NOT (u:UFO)-[:IN_YEAR_OF]->(y:Year{year:2000})
46+
RETURN DISTINCT s.shape AS `unique shapes`;
47+
// ╞═══════════╡
48+
// │"SPHERE" │
49+
// ├───────────┤
50+
// │"CIRCLE" │
51+
// ├───────────┤
52+
// │"DISK" │
53+
// ├───────────┤
54+
// │"LIGHT" │
55+
// ├───────────┤
56+
// │"FORMATION"│
57+
// ├───────────┤
58+
// │"OVAL" │
59+
// ├───────────┤
60+
// │"CONE" │
61+
// ├───────────┤
62+
// │"FIREBALL" │
63+
// ├───────────┤
64+
// │"OTHER" │
65+
// ├───────────┤
66+
// │"CHANGING" │
67+
// ├───────────┤
68+
// │"TRIANGLE" │
69+
// └───────────┘
70+
71+
//4. unique years(ascending order)
72+
// condition:‘at high speeds’ in the text
73+
MATCH (u:UFO)-[:IN_YEAR_OF]->(y:Year)
74+
WHERE u.text CONTAINS 'at high speeds'
75+
RETURN DISTINCT y.year AS `year`
76+
ORDER BY y.year ASC;
77+
// ╞════════╡
78+
// │2008 │
79+
// ├────────┤
80+
// │2011 │
81+
// ├────────┤
82+
// │2013 │
83+
// └────────┘
84+
85+
//5. count each wind direction times.
86+
// times of each direction in descending order.
87+
MATCH(u:UFO)-[:IS_WEAHTER_DIRECTION_OF]->(wd:Wdire),
88+
(u:UFO)-[:IN_YEAR_OF]->(y:Year)
89+
RETURN wd.direction AS `wdire`,COUNT(wd.direction) AS `times of each direction`
90+
ORDER BY COUNT(wd.direction) DESC;
91+
// ╒══════════╤═════════════════════════╕
92+
// │"wdire" │"times of each direction"│
93+
// ╞══════════╪═════════════════════════╡
94+
// │"NORTH" │745 │
95+
// ├──────────┼─────────────────────────┤
96+
// │"SOUTH" │299 │
97+
// ├──────────┼─────────────────────────┤
98+
// │"WEST" │217 │
99+
// ├──────────┼─────────────────────────┤
100+
// │"SSE" │184 │
101+
// ├──────────┼─────────────────────────┤
102+
// │"SSW" │183 │
103+
// ├──────────┼─────────────────────────┤
104+
// │"SW" │179 │
105+
// ├──────────┼─────────────────────────┤
106+
// │"WSW" │177 │
107+
// ├──────────┼─────────────────────────┤
108+
// │"SE" │172 │
109+
// ├──────────┼─────────────────────────┤
110+
// │"WNW" │138 │
111+
// ├──────────┼─────────────────────────┤
112+
// │"ESE" │132 │
113+
// ├──────────┼─────────────────────────┤
114+
// │"NW" │129 │
115+
// ├──────────┼─────────────────────────┤
116+
// │"VARIABLE"│129 │
117+
// ├──────────┼─────────────────────────┤
118+
// │"EAST" │121 │
119+
// ├──────────┼─────────────────────────┤
120+
// │"NNW" │98 │
121+
// ├──────────┼─────────────────────────┤
122+
// │"ENE" │75 │
123+
// ├──────────┼─────────────────────────┤
124+
// │"NE" │71 │
125+
// ├──────────┼─────────────────────────┤
126+
// │"NNE" │70 │
127+
// └──────────┴─────────────────────────┘
128+
129+
//6. nearest city
130+
// condition: city: ‘CORAL SPRINGS’; countyName:‘BROWARD’; state: ‘FL’.
131+
MATCH (l:Location{city:'CORAL SPRINGS',countyName:'BROWARD',state:'FL'})
132+
WITH l, point({longitude:l.lng,latitude:l.lat}) AS location1
133+
MATCH(l2:Location)
134+
WITH location1,l,l2,point({longitude:l2.lng,latitude:l2.lat}) AS location2
135+
WHERE location1 <> location2
136+
WITH l.city AS `source city`,l2.city AS `target city`,distance(location1,location2) AS distance
137+
RETURN `source city`,`target city`,distance
138+
ORDER BY distance ASC
139+
LIMIT 1;
140+
// ╒═══════════════╤═════════════╤════════════════╕
141+
// │"source city" │"target city"│"distance" │
142+
// ╞═══════════════╪═════════════╪════════════════╡
143+
// │"CORAL SPRINGS"│"MARGATE" │5394.95935153865│
144+
// └───────────────┴─────────────┴────────────────┘
145+
146+
//7.year with the least number of different kinds of UFO shapes.
147+
MATCH (u:UFO)-[:IN_YEAR_OF]->(y:Year),(u:UFO)-[:IS_SHAPE_OF]->(s:Shape)
148+
RETURN y.year AS Year, COUNT(DISTINCT(s.shape)) AS `number of counting`
149+
ORDER BY `number of counting` ASC
150+
LIMIT 1;
151+
// ╒══════╤════════════════════╕
152+
// │"Year"│"number of counting"│
153+
// ╞══════╪════════════════════╡
154+
// │1997 │3 │
155+
// └──────┴────────────────────┘
156+
157+
//8.average temperature, pressure, and humidity
158+
//condition:each UFO shape,average values rounded to 3 decimal places
159+
MATCH (u:UFO)-[:IS_SHAPE_OF]->(s:Shape),(u:UFO)
160+
WITH DISTINCT(s.shape) AS `shape category`,round(AVG(u.temp),3) AS `average temperature`,round(AVG(u.hum),3) AS `average humidity`,round(AVG(u.pressure),3) AS `average pressure`
161+
RETURN `shape category`,`average temperature`,`average humidity`,`average pressure`
162+
ORDER BY `average humidity` DESC;
163+
164+
//9.top 3 counties with the most number of different cities.
165+
MATCH (l:Location)
166+
RETURN l.countyName AS countyName, COUNT(l.city) AS `city numbers`
167+
ORDER BY `city numbers` DESC
168+
LIMIT 3;
169+
// ╒═════════════╤══════════════╕
170+
// │"countyName" │"city numbers"│
171+
// ╞═════════════╪══════════════╡
172+
// │"WASHINGTON" │87 │
173+
// ├─────────────┼──────────────┤
174+
// │"JEFFERSON" │79 │
175+
// ├─────────────┼──────────────┤
176+
// │"LOS ANGELES"│79 │
177+
// └─────────────┴──────────────┘
178+
//10.Rank the total number of UFO sighting
179+
// condition:each state,descending order
180+
MATCH(u:UFO)-[:IS_HAPPENED_IN]->(l:Location)
181+
WITH l.state AS State
182+
RETURN State, COUNT(*) AS `number of UFO sighting recordings`
183+
ORDER BY State DESC;

TaskC3.cypher

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//Student Name: PEIYU LIU
2+
//Student ID: 31153291
3+
4+
// 1. A new UFO sighting records
5+
//match conditions to get records, add these records info to new node
6+
// Check database before change it.
7+
// MATCH (u)-[:IN_YEAR_OF]->(y2:Year{year:2021}),
8+
// (u)-[:IN_MONTH_OF]->(m2:Month{month:1}),
9+
// (u)-[:IN_DAY_OF]->(d2:Day{day:14}),
10+
// (u)-[:IN_HOUR_OF]->(h2:Hour{hour:23}),
11+
// (u)-[:IS_HAPPENED_IN]->(l2:Location{state:'IN',city:'HIGHLAND',countyName:'LAKE'})
12+
// RETURN u;
13+
14+
15+
MATCH (u1:UFO)-[:IN_MONTH_OF]->(m:Month{month:8}),
16+
(u1:UFO)-[:IN_DAY_OF]->(d:Day{day:14}),
17+
(u1:UFO)-[:IN_HOUR_OF]->(h:Hour{hour:16}),
18+
(u1:UFO)-[:IN_YEAR_OF]->(y:Year{year:1998}),
19+
(u1:UFO)-[:IS_WEAHTER_CONDITION_OF]->(c1:Conds),
20+
(u1:UFO)-[:IS_WEAHTER_DIRECTION_OF]->(w1:Wdire)
21+
//RETURN INFORMATION
22+
// create a new ufo node
23+
CREATE (u:UFO{})
24+
// set results into new node
25+
SET u.duration='25 minutes'
26+
SET u.text='Awesome lights were seen in the sky'
27+
SET u.summary='Awesome lights'
28+
SET u.pressure=u1.pressure
29+
SET u.temp=u1.temp
30+
SET u.windchill=u1.windchill
31+
SET u.rain=u1.rain
32+
SET u.vis=u1.vis
33+
SET u.dewpt=u1.dewpt
34+
SET u.thunder=u1.thunder
35+
SET u.fog=u1.fog
36+
SET u.precip=u1.precip
37+
SET u.wspd=u1.wspd
38+
SET u.tornado=u1.tornado
39+
SET u.hum=u1.hum
40+
SET u.snow=u1.snow
41+
SET u.wgust=u1.wgust
42+
SET u.heatindex=u1.heatindex
43+
SET u.hail=u1.hail
44+
// add existing relationships to new node
45+
MERGE(u)-[:IN_YEAR_OF]->(y2:Year{year:2021})
46+
MERGE (u)-[:IS_WEAHTER_CONDITION_OF]->(c1)
47+
MERGE(u)-[:IS_WEAHTER_DIRECTION_OF]->(w1)
48+
MERGE(u)-[:IN_MONTH_OF]->(m2:Month{month:1})
49+
MERGE(u)-[:IN_DAY_OF]->(d2:Day{day:14})
50+
MERGE(u)-[:IN_HOUR_OF]->(h2:Hour{hour:23})
51+
MERGE(u)-[:IS_HAPPENED_IN]->(l2:Location{state:'IN',city:'HIGHLAND',countyName:'LAKE'})
52+
RETURN u,l2,c1,w1,d2,m2,h2,y2;
53+
54+
//2. change unknow shape to ‘flying saucer’
55+
//condition year [2011 and 2008]
56+
// Conds:‘Clear’ to ‘Sunny/Clear’
57+
MATCH (u:UFO)-[:IS_WEAHTER_CONDITION_OF]->(c:Conds{condition:toUpper('Clear')}),
58+
(u:UFO)-[:IN_YEAR_OF]->(y:Year),
59+
(u:UFO)-[:IS_SHAPE_OF]->(s:Shape{shape:toUpper('Unknown')})
60+
WHERE y.year = 2011 or y.year = 2008
61+
SET s.shape = 'flying saucer'
62+
SET c.condition = 'Sunny/Clear'
63+
RETURN u,y,s,c;
64+
65+
//3. delete city:'ARCADIA'
66+
// condition: city:'ARCADIA', state:'FL'
67+
// check MATCH (l:Location{city:'ARCADIA',state:'FL'}) RETURN l;
68+
MATCH(l:Location{city:'ARCADIA',state:'FL'})
69+
DETACH DELETE l;

0 commit comments

Comments
 (0)