-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathspark-sql-exercises.html
240 lines (203 loc) · 10.1 KB
/
spark-sql-exercises.html
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
<!doctype html>
<!-- Inspired by view-source:https://brookewenig.github.io/SparkOverview.html -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="description" content="Apache Spark 2 Workshop | Spark SQL Exercises">
<meta name="author" content="Jacek Laskowski">
<title>Apache Spark 2 Workshop | Spark SQL Exercises</title>
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="revealjs-css/jacek.css">
<link rel="stylesheet" href="reveal.js/css/theme/beige.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section class="intro" data-transition="zoom" id="home">
<p>
<img width="25%" style="background:none; border:none; box-shadow:none;" data-src="images/spark-logo.png">
<img width="15%" src="images/jacek_laskowski_20141201_512px.png" style="border: 0">
</p>
<h1>Spark SQL Exercises</h1>
<h3>Apache Spark 2.3 / Spark SQL</h3>
<h4 style="font-size: smaller;"><a href="https://twitter.com/jaceklaskowski">@jaceklaskowski</a> / <a href="http://stackoverflow.com/users/1305344/jacek-laskowski">StackOverflow</a> / <a href="https://github.com/jaceklaskowski">GitHub</a><br />
Books: <a href="http://bit.ly/mastering-apache-spark">Mastering Apache Spark</a> / <a href="http://bit.ly/mastering-spark-sql">Mastering Spark SQL</a> / <a href="http://bit.ly/spark-structured-streaming">Spark Structured Streaming</a></h4>
<footer style="font-size: small;">©<a href="https://medium.com/@jaceklaskowski">Jacek Laskowski</a> 2018 / <a href="https://twitter.com/jaceklaskowski">@JacekLaskowski</a> / [email protected]</footer>
</section>
<section id="hints">
<h2>Spark SQL Exercises — Hints</h2>
<ul>
<li>Use the <a href="http://spark.apache.org/docs/latest/api/scala">scaladoc of Spark API for Scala</a>
<ul>
<li><a href="http://spark.apache.org/docs/latest/api/scala">http://spark.apache.org/docs/latest/api/scala</a></li>
</ul>
</li>
<li>Use <b>spark-shell</b> often</li>
<li>Use <a href="http://bit.ly/mastering-spark-sql">Mastering Spark SQL</a> book</li>
</ul>
<footer style="font-size: small;">©<a href="https://medium.com/@jaceklaskowski">Jacek Laskowski</a> 2018 / <a href="https://twitter.com/jaceklaskowski">@JacekLaskowski</a> / [email protected]</footer>
</section>
<section id="exercises" data-markdown>
<script type="text/template">
<!-- .slide: style="font-size: 80%; text-align: left" -->
## Exercises
<hr>
1. [Collecting aggregates](#/groupby-collect-list)
1. [Window with count](#/window-count)
1. [max with All Columns](#/max-with-all-columns)
1. [Window with collect_set and UDF](#/window-collect-set-udf)
1. [Window with lag](#/window-lag)
1. [Window with rowsBetween](#/window-rows-between)
1. [Aggregation with nulls](#/aggregation-with-nulls)
</script>
</section>
<section>
<section id="groupby-collect-list" data-markdown>
<script type="text/template">
## Collecting aggregates
1. [How to aggregate values into collection after groupBy?](https://stackoverflow.com/q/34202997/1305344)
1. Dataset has visitorId, trackingIds and emailIds
1. visitorId can appear multiple times
1. Collect all trackingIds and emailIds per visitorId
```
// Final result
+----------+----------------+----------+
|visitorId | trackingIds | emailIds |
+----------+----------------+----------+
| a158|[666b,666b,777c]| [12,'']|
| 7g21|[c0b5,c0b4] | [45, 87]|
+----------+----------------+----------+
```
</script>
</section>
<section data-markdown>
<script type="text/template">
<span class="menu-title" style="display: none">Hints</span>
## Collecting aggregates <small>Hints</small>
1. Use <b>groupBy</b> aggregate operator
1. Use [collect_list](http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.functions$) function
* Aggregate function that returns a list of objects with duplicates.
</script>
</section>
</section>
<section>
<section id="window-count" data-markdown>
<script type="text/template">
<span class="menu-title" style="display: none">Window with count</span>
## Window with count
1. [How to add groupBy().count() to the source DataFrame?](https://stackoverflow.com/q/44434590/1305344)
```scala
// count column added
scala> solution.show
+---+---+-----+
| x| y|count|
+---+---+-----+
| 1| a| 2|
| 1| b| 1|
| 1| a| 2|
+---+---+-----+
```
</script>
</section>
<section data-markdown>
<script type="text/template">
<span class="menu-title" style="display: none">Hints</span>
## Window with count <small>Hints</small>
1. Use window aggregation
1. Use [count](http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.functions$) function
* Aggregate function that returns the number of items in a group.
</script>
</section>
</section>
<section id="max-with-all-columns" data-markdown>
<script type="text/template">
## max with All Columns
1. [How to max value and keep all columns (for max records per group)?](https://stackoverflow.com/q/42636179/1305344)
</script>
</section>
<section id="window-collect-set-udf" data-markdown>
<script type="text/template">
## Window with collect_set and UDF
1. [How to pass in limit parameter to collect_set function?](https://stackoverflow.com/q/38730912/1305344)
</script>
</section>
<section id="window-lag" data-markdown>
<script type="text/template">
## Window with lag
1. [How to divide the value of current row with the following one?](https://stackoverflow.com/q/44392754/1305344)
</script>
</section>
<section id="window-rows-between" data-markdown>
<script type="text/template">
## Window with rowsBetween
1. [How to calculate the current row with the next one?](https://stackoverflow.com/q/44390004/1305344)
</script>
</section>
<section id="aggregation-with-nulls" data-markdown>
<script type="text/template">
## Aggregation with nulls
1. [How to max per column with nulls that should be replaced (with 0)?](https://stackoverflow.com/q/42688684/1305344)
</script>
</section>
<section style="text-align: left" data-markdown id="questions">
<script type="text/template">
# Questions?
* Read [Mastering Spark SQL](https://bit.ly/mastering-spark-sql)
* [https://bit.ly/mastering-spark-sql](https://bit.ly/mastering-spark-sql)
* Follow [@jaceklaskowski](https://twitter.com/jaceklaskowski) on twitter
* Upvote [my questions and answers on StackOverflow](http://stackoverflow.com/users/1305344/jacek-laskowski)
<footer style="font-size: small; text-align: center">©<a href="https://medium.com/@jaceklaskowski">Jacek Laskowski</a> 2018 / <a href="https://twitter.com/jaceklaskowski">@JacekLaskowski</a> / [email protected]</footer>
</script>
</section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
slideNumber: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
menu: {
markers: true,
openSlideNumber: true
},
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal.js/plugin/markdown/marked.js' },
{ src: 'reveal.js/plugin/markdown/markdown.js' },
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true },
{ src: 'reveal.js/plugin/notes/notes.js', async: true },
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-45999426-3', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>