-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlightData.scala
230 lines (168 loc) · 8.81 KB
/
FlightData.scala
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
package com.prathima
import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}
import org.apache.spark.sql.functions._
import java.sql.Date
case class Flight(passengerId: Int, flightId: Int, from: String, to: String, date: String)
case class Passengers(passengerId: Int, firstName: String, lastName: String)
case class FlightsCount(month: String, numberofFlights: Long)
case class TopMostFrequentFlyers(passengerId: Int, numberofFlights: Long, firstName: String, lastName: String)
case class FinalResults(passengerId: Int, routes: String, longestRouteWithoutUK: Int)
case class PassengersFlewTogether(passenger1Id: Int, passenger2Id: Int, numberofFlightsTogether: Long)
case class PassengersFlewTogetherDateRange(passenger1Id: Int, passenger2Id: Int, numberofFlightsTogether: Long, from: String, to: String)
object FlightData {
def main(args: Array[String]): Unit = {
if (args.length == 0) {
println("Inside Args if ")
System.exit(1)
}
Logger.getLogger("org").setLevel(Level.WARN)
Logger.getLogger("akka").setLevel(Level.WARN)
val dataPath = args(0)
val dataPath2 = args(1)
// println(s"Data path: $dataPath")
//println(s"Data path: $dataPath2")
//println(s"Working directory: ${new java.io.File(".").getCanonicalPath}")
//Creation of Spark Session with 1 core
val spark: SparkSession = SparkSession.builder().master("local[3]")
.appName("Flight DataSet")
.getOrCreate()
//modularised the readcsv
val flightDataFrame = readCSV(spark, dataPath)
val passengerDataFrame = readCSV(spark, dataPath2)
import spark.implicits._
val flightDS: Dataset[Flight] = flightDataFrame.select("passengerId", "flightId", "from", "to", "date").as[Flight]
//count Number of Flights per month
val flightsCountPerMonth: Dataset[FlightsCount] = countNumberofFlights(flightDS)(spark)
//flightsCountPerMonth.show()
val passengerDS: Dataset[Passengers] = passengerDataFrame.select("passengerId", "firstName", "lastName").as[Passengers]
//compute top 100 Frequent Flyers
val topFrequentFlyers: Dataset[TopMostFrequentFlyers] = topHundredMostFrequentFlyers(flightDS, passengerDS)(spark)
topFrequentFlyers.show(100)
//compute the flight routes and calculate the longest run without UK
val longestRouteWithoutUK: Dataset[FinalResults] = computeFlightRoutes(flightDS)(spark)
longestRouteWithoutUK.show()
//passenger who have been more than 3 flights together
//
val passengersFlewTogether: Dataset[PassengersFlewTogether] = passengerFlewTogether(flightDS)(spark)
passengersFlewTogether.show()
// flown together testing for atleast 5 times and date range as below should work for anything
val passengerFlownTogetherwithinDate: Dataset[PassengersFlewTogetherDateRange] = flownTogether(flightDS, 5, Date.valueOf("2017-01-01"), Date.valueOf("2017-3-01"))(spark)
passengerFlownTogetherwithinDate.show()
//result.show()
spark.stop()
}
def flownTogether(flightDS: Dataset[Flight], atLeastNTimes: Int, from: Date, to: Date)(implicit spark: SparkSession): Dataset[PassengersFlewTogetherDateRange] = {
import spark.implicits._
// joining the flight Dataset and Passenger Dataset applying where clause to get only travelled between that date range
val joinDS = flightDS.alias("f1").joinWith(flightDS.alias("f2"), $"f1.flightId" === $"f2.flightId" && $"f1.passengerId" =!= $"f2.passengerId").select(
$"_1.passengerId".alias("passenger1Id").as[Int],
$"_2.passengerId".alias("passenger2Id").as[Int],
$"_1.flightId".as[Int],
$"_1.date".as[String]
).filter($"_1.date".between(from, to) && $"_2.date".between(from, to))
//ordering the flights to avoid repeating the same passenger1Id and passenger2Id interchangeably
val orderedFlights = joinDS.map { case (passenger1Id, passenger2Id, flightId, date) =>
val (minId, maxId) = if (passenger1Id < passenger2Id) (passenger1Id, passenger2Id) else (passenger2Id, passenger1Id)
(minId, maxId, flightId, date)
}.toDF("passenger1Id", "passenger2Id", "flightId", "date").distinct()
//getting the flight count of passengerId 1 and passengerId 2 between the date Ranges
val passengerPairsCount = orderedFlights
.groupBy("passenger1Id", "passenger2Id")
.agg(count("flightId").alias("numberofFlightsTogether"),
min("date").alias("from"),
max("date").alias("to")
)
//filtering to exclude the join result of less than 3
val frequentFlyerPairs = passengerPairsCount
.filter($"numberofFlightsTogether" > atLeastNTimes)
return frequentFlyerPairs.as[PassengersFlewTogetherDateRange]
}
def passengerFlewTogether(flightDS: Dataset[Flight])(implicit spark: SparkSession): Dataset[PassengersFlewTogether] = {
import spark.implicits._
val joinDS = flightDS.alias("f1").joinWith(flightDS.alias("f2"), $"f1.flightId" === $"f2.flightId" && $"f1.passengerId" =!= $"f2.passengerId").select(
$"_1.passengerId".alias("passenger1Id").as[Int],
$"_2.passengerId".alias("passenger2Id").as[Int],
$"_1.flightId".as[Int]
)
//the below logic is implemented to avoid the same set of passengerId's interchangeably in passenger1Id and passenger2Id columns
val orderedFlights = joinDS.map { case (passenger1Id, passenger2Id, flightId) =>
val (minId, maxId) = if (passenger1Id < passenger2Id) (passenger1Id, passenger2Id) else (passenger2Id, passenger1Id)
(minId, maxId, flightId)
}.toDF("passenger1Id", "passenger2Id", "flightId").distinct()
//getting the flight count of passengerId 1 and passengerId 2
val passengerPairsCount = orderedFlights
.groupBy("passenger1Id", "passenger2Id")
.agg(count("flightId").alias("numberofFlightsTogether"))
//filtering to exclude the join result of less than 3
val frequentFlyerPairs = passengerPairsCount
.filter($"numberofFlightsTogether" > 3)
return frequentFlyerPairs.as[PassengersFlewTogether]
}
def computeFlightRoutes(flightDS: Dataset[Flight])(implicit spark: SparkSession): Dataset[FinalResults] = {
import spark.implicits._
// Flight Route is computed based on from chained as example uk->no and grouped per passenger ID sorted by date of travel
val groupedDS = flightDS
.groupByKey(_.passengerId)
.mapGroups { case (passengerId, iter) =>
val sortedRoutes = iter.toSeq.sortBy(_.date).map(_.from).toList
val routesList = sortedRoutes.mkString("->")
val longestRouteWithoutUK = longestNonUkSequence(sortedRoutes)
FinalResults(passengerId, routesList, longestRouteWithoutUK)
}.orderBy(desc("longestRouteWithoutUK"))
return groupedDS
}
//The function is being invoked from map transformation to compute the longest route without UK
def longestNonUkSequence(countries: List[String]): Int = {
var maxCount = 0
var currentCount = 0
for (country <- countries) {
if (country == "uk") {
maxCount = math.max(maxCount, currentCount)
currentCount = 0
} else {
currentCount += 1
}
}
// Check the last sequence as well
maxCount = math.max(maxCount, currentCount)
return maxCount
}
def topHundredMostFrequentFlyers(flightDS: Dataset[Flight], passengerDS: Dataset[Passengers])(spark: SparkSession): Dataset[TopMostFrequentFlyers] = {
import spark.implicits._
// joining the flight Dataset and passenger Dataset and retrieving the required fields
val joinDS = flightDS.joinWith(passengerDS, flightDS.col("passengerId") === passengerDS.col("passengerId")).map {
case (flight, passenger) => (flight.passengerId, flight.flightId, passenger.firstName, passenger.lastName)
}
val countflights = joinDS.
groupBy("_1", "_3", "_4")
.agg(count("_2").alias("Number of Flights")).orderBy(desc("Number of Flights"))
.limit(100)
// countflights.show()
val result = countflights.select(
$"_1".alias("passengerId"),
$"Number of Flights".alias("numberofFlights"),
$"_3".alias("firstName"),
$"_4".alias("lastName")
).as[TopMostFrequentFlyers]
return result
}
def countNumberofFlights(flightDS: Dataset[Flight])(spark: SparkSession): Dataset[FlightsCount] = {
import spark.implicits._
val countNumberOfFlightsByMonth = flightDS.map(flight => {
val month = flight.date.substring(5, 7) // Extracting '' as the month
(month, flight.flightId)
})
val flightsCountByMonth = countNumberOfFlightsByMonth
.groupBy("_1")
.count()
.withColumnRenamed("_1", "month")
.withColumnRenamed("count", "numberofFlights").as[FlightsCount]
return flightsCountByMonth
}
def readCSV(spark: SparkSession, dataPath: String): DataFrame = {
val dataFrame = spark.read.option("header", true).option("inferSchema", true).csv(dataPath)
// dataFrame.show()
return dataFrame
}
}