-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatisticsSqlApp.scala
More file actions
71 lines (53 loc) · 2.47 KB
/
StatisticsSqlApp.scala
File metadata and controls
71 lines (53 loc) · 2.47 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
/** Spark application to calculate some statistics on event sessions.
* Input should be generated by `SessionSqlApp` or `SessionAggregateApp`.
*
* Input format: `category,product,userId,eventTime,eventType,sessionStartTime,sessionEndTime,sessionId`
*
* Output format: `category,medianSessionDurationInMinutes,usersSpendingLessThan1Min,usersSpending1To5Min,usersSpendingMoreThan5Min`
*
*/
object StatisticsSqlApp extends GenericApp {
def appName = "statistics-sql-app"
def execute(inputPath: String, outputPath: String) = withSpark { spark =>
val eventsWithSessions = spark.read
.option("header", "true")
.csv(inputPath)
eventsWithSessions.createTempView("eventsWithSessions")
val eventsWithSessionDurations = spark.sql("""SELECT
userId, category, sessionId,
unix_timestamp(cast(sessionEndTime AS TIMESTAMP)) -
unix_timestamp(cast(sessionStartTime AS TIMESTAMP)) AS sessionDuration
FROM eventsWithSessions""")
eventsWithSessionDurations.createTempView("eventsWithSessionDurations")
val sessions = spark.sql("""SELECT userId, category, sessionDuration
FROM eventsWithSessionDurations GROUP BY userId, category, sessionId, sessionDuration""")
sessions.createTempView("sessions")
val percentile = spark.sql("""SELECT category,
percentile(sessionDuration, 0.5) / 60 AS medianSessionDurationInMinutes
FROM sessions GROUP BY category""")
percentile.createTempView("percentile")
val userDurations = spark.sql("""SELECT category, userId,
sum(sessionDuration) AS sumDuration
FROM sessions GROUP BY category, userId""")
userDurations.createTempView("userDurations")
val durations = spark.sql("""SELECT category,
count(CASE WHEN sumDuration < 60 THEN userId ELSE NULL END)
AS usersSpendingLessThan1Min,
count(CASE WHEN sumDuration BETWEEN 60 and 300 THEN userId ELSE NULL END)
AS usersSpending1To5Min,
count(CASE WHEN sumDuration > 300 THEN userId ELSE NULL END)
AS usersSpendingMoreThan5Min
FROM userDurations GROUP BY category""")
durations.createTempView("durations")
val statistics = spark.sql("""SELECT percentile.category,
medianSessionDurationInMinutes, usersSpendingLessThan1Min,
usersSpending1To5Min, usersSpendingMoreThan5Min
FROM percentile JOIN durations USING (category)""")
.cache()
statistics.write
.option("header", "true")
.csv(outputPath)
statistics.show()
}
run()
}