yibo_qa@163.com
--------------------------------------------------
1、Scala编程语言
(1)语言基础
(2)面向对象
(3)重点:函数式编程 和 高级特性(泛型、隐式类、隐式函数、隐式参数)
2、Spark Core:Spark的内核,最重要的部分。Spark是基于scala开发的,对scala支持的最好。
3、Spark SQL:类似于Hive和Pig。数据分析引擎。把sql语句,提交到spark集群中运行。
4、Spark Streaming:类似于storm,用于流式计算、实时计算。本质:是一个离线计算,不是流式计算。
5、Spark MLlib:Spark机器学习工具包。引申:机器学习入门、线性回归、余弦相似性算法讲解。
6、Spark GraphX:Spark 图计算工具。
--------------------Scala编程语言-----------------------
--------------Scala基础------------------
一、Scala语言简介
1、Scala是一个多范式的编程语言(支持多种方式的编程)
(1)使用面向对象编程:封装、继承、多态
(2)使用函数式编程:Scala最大的特点
(*)函数式编程的优点:代码非常简洁。
(*)函数式编程的缺点:可读性太差,尤其隐式类、隐式函数、隐式参数。
2、安装配置Scala
(*)基于JDK,首先安装JDK
(*)安装Scala版本:2.11.8(Spark 2.1.0版本)
(*)设置环境变量:SCALA_HOME scala安装目录
(*)把%SCALA_HOME%/bin 加入 path
官网:https://www.scala-lang.org/
C:\Users\vilibra>scala -version
Scala code runner version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL
安装成功
(*)开发测试环境:
命令行:REPL
IDE:
(1)scala IDE:基于eclipse。
(2)IDEA:安装Scala插件 ---- 本课程使用。
二、Scala中的数据类型和变量常量
1、注意:Scala中任何数据,都是对象,这个和Java不同。
scala> 1.
!= < >>> doubleValue isNaN isValidShort shortValue toDouble toShort
% << ^ floatValue isNegInfinity isWhole signum toFloat unary_+
& <= abs floor isPosInfinity longValue to toHexString unary_-
* == byteValue getClass isValidByte max toBinaryString toInt unary_~
+ > ceil intValue isValidChar min toByte toLong underlying
- >= compare isInfinite isValidInt round toChar toOctalString until
/ >> compareTo isInfinity isValidLong self toDegrees toRadians |
2、基本的数据类型
Byte 8位有符号数字 -128 127
Short 16位有符号数字
Int 32 。。。
Long 64
Float
Double
String 字符串 Char 字符
String支持插值操作:相当于字符串的拼接
scala> var s1 : String = "Hello world"
s1: String = Hello world
scala> "My name is Tom and ${s1}"
res1: String = My name is Tom and ${s1}
scala> s"My name is Tom and ${s1}"
res2: String = My name is Tom and Hello world
3、定义变量 var 和 常量 val 的时候,可以不指定数据类型
scala> var s1:String="Hello"
s1: String = Hello
scala> var s2 = "Hello All"
s2: String = Hello All
scala> var s3 = 3
s3: Int = 3
Scala语言可以自动进行类型的推导。也就是,不需要指定类型,就可以通过后面的值来推导出类型。
常量的值,不可以被改变。
scala> val s = 1
s: Int = 1
scala> s=2
<console>:12: error: reassignment to val
s=2
4、unit类型和Nothing类型
(1)Unit类型:相当于Java的void,就是没有返回值。
scala> val f = ()
f: Unit = ()
解释:() 代表函数
(2)Nothing类型,一般来说,在执行过程中,产生异常Exception。
举例:
scala> def myFunction = throw new Exception("Some Error")
myFunction: Nothing
三、函数:是Scala中的头等公民。
(一)Scala中的内置函数,可以直接使用的函数。
scala> max(1,2)
<console>:12: error: not found: value max
max(1,2)
^
scala> import scala.math._
import scala.math._
说明:_相当于java中的*,代表这个包下所有的东西。
scala> max(1,2)
res3: Int = 2
res3: Int = 2
解释:本质是,定义了一个变量,保存执行结果,推导出res3的类型是Int。
scala> val a2 = max(1,2)
a2: Int = 2
(二)自定义函数:关键字 def
格式:
def 函数名称 ([参数名:参数类型]*) : 返回值类型 = {}
举例:
1、求和
scala> def sum(x:Int,y:Int) : Int = x+y
sum: (x: Int, y: Int)Int
scala> sum(10,20)
res4: Int = 30
scala> var a = sum(10,20)
a: Int = 30
2、求阶乘,采用递归方式
scala> def myFactor(x:Int) : Int = {
| //实现
| if(x<=1)
| 1
| else
| x*myFactor(x-1)
| }
myFactor: (x: Int)Int
scala> myFactor(3)
res5: Int = 6
注意:没有return语句。函数的最后一句话就是函数的返回值。
上面函数有分支,1 和 x*myFactor(x-1) 都有可能是函数的最后一句话。
3、求输入的年份是否是闰年。
普通闰年:能被4整除,但不能被100整除的年份,成为普通闰年。(2004是闰年,1999不是)
世纪闰年:能被400整除的为世纪闰年。(2000是世纪闰年,1900年不是世纪闰年)
scala> def isLeapYear(x:Int) = {
| if( (x%4 == 0 && x%100 != 0) || x%400 == 0 ) true
| else false}
isLeapYear: (x: Int)Boolean
scala> isLeapYear(2008)
res6: Boolean = true
scala> isLeapYear(2009)
res7: Boolean = false
四、循环语句
1、类似于Java:for while dowhile
package day0529 object StudyScala1 { def main(args: Array[String]): Unit = { //for循环 //定义一个集合 var list = List("Mary","Tom","Mike") println("-----------------for循环第一种写法-----------------------") for (s <- list) println(s) println("-----------------for循环第二种写法-----------------------") for{ s <- list if(s.length >3) }println(s) println("-----------------for循环第三种写法-----------------------") for (s <- list if s.length > 3){println(s)} println("-----------------for循环第四种写法-----------------------") var newList = for { s <- list s1 = s.toUpperCase() }yield (s1) for (s <- newList){ println(s)} println("-----------------while循环写法-----------------------") var i:Int = 0 while (i<list.length){ print(list(i)+" ") i += 1 } println("-----------------do while循环写法-----------------------") var j : Int =0; do { print(list(j)+" ") j += 1 }while(j<list.length) } }
2、使用foreach进行迭代(类似spark算子)
println("-----------------foreach 迭代-----------------------")
list.foreach(println)