-
Notifications
You must be signed in to change notification settings - Fork 146
/
02_scala.html
767 lines (642 loc) · 22.5 KB
/
02_scala.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
<!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 | Scala — Just Enough For Developing Spark Applications">
<meta name="author" content="Jacek Laskowski">
<title>Apache Spark 2 Workshop | Scala — Just Enough For Developing Spark Applications</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">
<span class="menu-title" style="display: none">Home</span>
<p>
<img width="5%" style="background:none; border:none; box-shadow:none;" data-src="images/scala-logo.png">
<img width="17%" style="background:none; border:none; box-shadow:none;" data-src="images/spark-logo.png">
<img width="8%" src="images/jacek_laskowski_20141201_512px.png" style="border: 0">
</p>
<h1>Scala</h1>
<h3>Just Enough For Developing Spark Applications</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 / @jaceklaskowski / jacek@japila.pl</footer>
</section>
<section id="agenda" data-markdown>
<script type="text/template">
<!-- .slide: style="font-size: 90%" -->
## Agenda
1. [Introduction to Scala](#/intro)
1. [Hello World Scala App](#/hello-world-app)
1. [Spark Shell](#/spark-shell)
1. [Scala Types](#/types)
1. [Expressions](#/expressions)
1. [Scaladoc](#/scaladoc)
1. [Methods](#/methods)
1. [Functions](#/functions)
1. [for-comprehension](#/for-comprehension)
1. [IntelliJ IDEA](#/intellij-idea)
1. [ScalaTest](#/scalatest)
</script>
</section>
<section>
<section id="intro" data-markdown>
<script type="text/template">
## Scala
* General-Purpose Programming language
* Strongly-Typed Language
* Types known at compile time
* Scala compiler as a safety net
* Application compiles? It then should run fine in production
</script>
</section>
<section data-markdown>
<script type="text/template">
## Scala
* Functional and Object-Oriented Paradigms blended nicely
* Class Hierarchies
* Higher-Order Functions
* The latest major version: **2.12**
* Home page: <a href="http://www.scala-lang.org/">http://www.scala-lang.org</a>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Scala and Spark
* **Apache Spark** is written in **Scala**
* Spark supports up to **2.11.12**
* Support for 2.12 is coming
</script>
</section>
</section>
<section>
<section id="hello-world-app" data-markdown>
<script type="text/template">
## Hello World Scala App
```scala
object Main extends App {
println("Hello, Friends!")
}
```
* Most likely the easiest Scala standalone application
* You _could_ cut the number of lines as a Scala script
</script>
</section>
<section id="object-keyword" data-markdown>
<script type="text/template">
## object Keyword
```scala
// Creates a new type and the one and only value
scala> object Main
defined object Main
scala> object Jacek {
| def name = "Jacek"
| }
defined object Jacek
scala> Jacek.name
res0: String = Jacek
```
</script>
</section>
<section id="extends-keyword" data-markdown>
<script type="text/template">
## extends Keyword
```scala
// Creates "is-a" relationship
scala> object Main extends App
defined object Main
scala> Main. // <-- press TAB after the dot
delayedInit executionStart main
// App type gives main method
// main is the entry point to Scala standalone applications
scala> Main.main(Array.empty)
```
</script>
</section>
<section id="println" data-markdown>
<script type="text/template">
## println Procedure
```scala
// Accepts one input parameter of type String
// Print the argument to the console
scala> println("Hello, Friends!")
Hello, Friends!
```
</script>
</section>
<section id="curly-brackets">
<h2>curly brackets</h2>
<pre><code>
object Main extends App {
println("Hello, Friends!")
}
</code></pre>
</section>
<section>
<h2>Hello World in Action</h2>
<pre><code>
scala> :paste
// Entering paste mode (ctrl-D to finish)
object Main extends App {
println("Hello, Friends!")
}
// Exiting paste mode, now interpreting.
defined object Main
scala> Main.main(null)
Hello, Friends!
</code></pre>
</section>
<section>
<h2>Hello World in Action</h2>
<pre><code>
scala> :load yourScalaCode.txt
defined object Main
Hello, Friends!
</code></pre>
</section>
<section id="val-keyword" data-markdown>
<script type="text/template">
## val keyword
```scala
// Defines a value (aka constant)
scala> val name = "Jacek"
name: String = Jacek
scala> name
res0: String = Jacek
```
</script>
</section>
<section id="string-interpolation" data-markdown>
<script type="text/template">
## String Interpolation
```scala
// Replaces $-prefixed strings with corresponding values
// Allows for templating
scala> val name = "Jacek"
name: String = Jacek
// Note the 's' character before the text
// |
// v
scala> println(s"Hello, $name!")
Hello, Jacek!
// Template
scala> val greeting = s"Hello $name. Nice to meet you."
greeting: String = Hello Jacek. Nice to meet you.
```
</script>
</section>
<section id="app-main-args" data-markdown>
<script type="text/template">
## App, main and args
* **main** is the entry point to a Scala standalone application
* **App** gives **args** to access command-line arguments
* **args** is of type **Array[String]**
```scala
scala> object Main extends App
defined object Main
scala> Main. // <-- press TAB after the dot
delayedInit executionStart main
// Execute main with one input argument
scala> Main.main(Array("Jacek"))
```
</script>
</section>
</section>
<section>
<section id="spark-shell" data-markdown>
# spark-shell
### Learning Scala (and Spark) Interactively
</section>
<section data-markdown>
<script type="text/template">
## spark-shell
* **Scala REPL** is an interactive environment for Scala exploration and prototyping
* Extremely helpful during your first days in Scala
* **spark-shell** is Scala REPL with Scala imports and values
* Gives you both worlds — Scala and Spark
```
$ ./bin/spark-shell
```
</script>
</section>
<section data-markdown>
## Enjoy Scala (Exercises)
1. Get familiar with type literals
* Strings, Ints, Longs, Doubles, Chars, Symbols
1. **Println** your name to the console
1. Define your name as a **val** and use to println
* Use string interpolation #protip
1. Save the code to a file and **:load** it
</section>
</section>
<section>
<section id="types" data-markdown>
# Scala Types
</section>
<section data-markdown>
<script type="text/template">
## Built-In "Primitive" Types
* Numeric types
* Int, Long, Double
* Character types
* Char, Symbol
* String
* Logical type
* Boolean
</script>
</section>
<section data-markdown>
<script type="text/template">
## Standard Types
* Tuple **(1,2)**
* Seq(uence) **Seq(0,1,2,3)**
* Range **0 to 4**
* Iterator **Seq(...).toIterator**
* Function
* <!-- .element: style="font-size: 0.8em;" --> _More in the upcoming [slide](#/standard-functions-datetime)_
</script>
</section>
<section data-markdown>
<script type="text/template">
## Custom Types
* **case class** Keywords
* Auto-generates many methods
* **class** Keyword
* Less often in Spark
```
// Define a custom type
scala> case class Person(name: String, age: Int)
defined class Person
// Create an instance
scala> val jacek = Person("Jacek", 44)
jacek: Person = Person(Jacek,44)
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Types in spark-shell
```
scala> :help type
display the type of an expression without evaluating it
scala> val lang = "Scala"
lang: String = Scala
scala> :type lang
String
// Object defined previously
scala> :type jacek
Person
```
</script>
</section>
</section>
<section>
<section id="expressions" data-markdown>
# Expressions
### ... And Statements
</section>
<section data-markdown>
<script type="text/template">
## Expression
* Unit of code that returns a value
* **Expression block** is a collection of expressions wrapped in curly braces
```scala
scala> val five = 5
five: Int = 5
scala> five + 10
res1: Int = 15
```
```scala
// What's the value of block value?
// Use spark-shell to answer the question.
val block = {
"hello world"
5
}
```
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
## If Expression
* **if (boolean expression) expr1 else expr2**
```scala
scala> val five = if (true) 5 else 10
five: Int = 5
scala> val ten = if (false) 5 else 10
ten: Int = 10
```
<!-- .element: class="fragment" -->
</script>
</section>
</section>
<section>
<section id="scaladoc" data-markdown>
# Scaladoc
## Scala Documentation
</section>
<section data-markdown>
## Scala Standard Library
* https://www.scala-lang.org/api/2.11.12
* Spend as much time with the docs as possible
* That will pay off quickly
</section>
</section>
<section>
<section data-markdown>
# Methods in Scala
## Named executable blocks of code
</section>
<section data-markdown>
## Method Definition
* **def** Keyword
* Optional input parameters (in round brackets)
* **name: Type**
* comma as separator for multiple input params
* Optional return type
```
scala> def greet(name: String): String = s"Hello, $name"
greet: (name: String)String
scala> greet("Jacek")
res8: String = Hello, Jacek
// What's res8?
```
</section>
<section id="methods" data-markdown>
## Methods
* Methods belong to classes
* Class as a container of methods (and state)
* Implicit **this** parameter for the owning object
```
case class Person(name: String) {
def introduceYourself: String = s"I'm $name"
}
// Use :paste or :load
scala> val jacek = new Person("Jacek")
jacek: Person = Person(Jacek)
scala> jacek.introduceYourself
res9: String = I'm Jacek
```
</section>
<section data-markdown>
## Methods in spark-shell
* spark-shell auto-generates an implicit object
* Another reason to use spark-shell early and often
* Makes for a better learning experience
```
scala> def greet(name: String) = s"Hello, $name"
greet: (name: String)String
scala> greet("Jacek")
res8: String = Hello, Jacek
```
</section>
<section data-markdown>
## Named Parameters
* Use parameter names at call site
* Any order of parameters possible
```
scala> def greet(name: String) = s"Hello, $name"
greet: (name: String)String
scala> greet(name = "Jacek")
res8: String = Hello, Jacek
```
</section>
</section>
<section>
<section id="functions" data-markdown>
# Functions in Scala
## (Other) Named executable blocks
</section>
<section data-markdown>
## Function Definition
* Functions are values of a function type
* Part of objects or classes
```
scala> val greet: String => String = { name => s"Hello, $name" }
greet: String => String = <function1>
scala> val greet = { name: String => s"Hello, $name" }
greet: String => String = <function1>
scala> greet("Jacek")
res10: String = Hello, Jacek
```
</section>
<section data-markdown>
## _ Placeholder
* **_** (underscore) to reference one input parameter
* \# underscores = \# input parameters
* Access ordered from left to right
* Very useful notation with higher-order functions
* Higher-order functions defined on the following slide
```
// What's the more proper name of the function?
scala> val f: (Int, Int) => Int = _ + _
f: (Int, Int) => Int = <function2>
```
</section>
</section>
<section>
<section id="for-comprehension" data-markdown>
# for-comprehension
## Functional for Loop
</section>
<section data-markdown>
## Demo
* Generating Coordinates (as Pairs of Numbers)
</section>
</section>
<section id="intellij-idea" data-markdown>
# IntelliJ IDEA
## Integrated development environment
## for Scala and Spark
</section>
<section>
<section>
<h2>IntelliJ IDEA</h2>
<ol>
<li>
<b>Integrated development environment</b>
<ul>
<li>The best development tool for Scala</li>
</ul>
</li>
<li>Support for Scala using separate <b>Scala plugin</b></li>
<li><b>Scala Worksheet</b> for Learning and Prototyping</li>
<li>"IntelliSense" Feature</li>
<li>Toggle <b>Distraction Free mode</b></li>
<li>
Lots of Key Shortcuts
<ul>
<li>You're going to use them quite often!</li>
</ul>
</li>
</ol>
</section>
<section>
<h2>IntelliJ IDEA — Demo</h2>
<ol>
<li>
Creating a Scala sbt-managed project
<ul>
<li>Enable <b>Use auto-import</b> checkbox</li>
<li>Disable <b>Create directories for empty content roots automatically</b> checkbox</li>
<li>Wait till IntelliJ IDEA (and sbt) resolves the project's dependencies that may take some <i>longer</i> time</li>
</ul>
</li>
<li>
Running the Hello World application
<ul>
<li>Right-click the object name and select <b>Run</b></li>
<li>Click <b>double-window icon</b> on the stripe on the left</li>
</ul>
</li>
</ol>
</section>
<section>
<h2>IntelliJ IDEA — Exercise</h2>
<ol>
<li>
Create a Scala standalone application using IntelliJ IDEA
<ul>
<li>Use a Scala sbt-managed project</li>
</ul>
</li>
<li>
Run the Scala application
</li>
<li>Pass command-line arguments
<ul>
<li><b>Run > Edit Configurations</b> menu</li>
<li><b>Program arguments</b> field</li>
<li>Run the application once to have an configuration entry</li>
</ul>
</li>
</ol>
</section>
</section>
<section id="scalatest">
<h1>ScalaTest</h1>
</section>
<section>
<section>
<h2>ScalaTest</h2>
<ol>
<li>
Scala Testing Framework
</li>
<li>
Matchers - Assertions
</li>
<li>
sbt supports ScalaTest
<ul>
<li><b>sbt test</b> (and don't forget about <b>~</b> - a tilde)</li>
</ul>
</li>
<li>
IntelliJ IDEA supports ScalaTest, too.
</li>
<li><a href="http://www.scalatest.org">http://www.scalatest.org</a></li>
<li><b>Demo:</b> Installation and Execution</li>
</ol>
</section>
<section>
<h2>Exercise: Writing a Test</h2>
<ol>
<li>
Follow <a href="http://www.scalatest.org">http://www.scalatest.org</a>
</li>
<li>
Define <b>libraryDependencies</b> in <b>build.sbt</b>
</li>
<li>
Run tests using <b>sbt test</b>
</li>
</ol>
</section>
</section>
<section id="recap" data-markdown>
<script type="text/template">
<!-- .slide: style="font-size: 90%" -->
## Recap
1. [Introduction to Scala](#/intro)
1. [Hello World Scala App](#/hello-world-app)
1. [Spark Shell](#/spark-shell)
1. [Scala Types](#/types)
1. [Expressions](#/expressions)
1. [Scaladoc](#/scaladoc)
1. [Methods](#/methods)
1. [Functions](#/functions)
1. [for-comprehension](#/for-comprehension)
1. [IntelliJ IDEA](#/intellij-idea)
1. [ScalaTest](#/scalatest)
</script>
</section>
<section style="text-align: left" data-markdown id="questions">
<script type="text/template">
# Questions?
* Read [Mastering Apache Spark 2](https://bit.ly/mastering-apache-spark)
* [https://bit.ly/mastering-apache-spark](https://bit.ly/mastering-apache-spark)
* 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)
</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: 'revealjs-plugins/menu/menu.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>