Scala 學習 進擊大資料Spark生態圈----個人筆記
學習Scala 進擊大資料Spark生態圈
總結:
第一章:
簡單講解了一下Scala的優劣勢
第二章:
val:常量(值) 會自動生成get方法
var: 變數 自動生成get/set方法
lazy屬性是遇到Action操作才會執行
優點:大資料需要提前載入的時候不會佔用整個系統很多資源
缺點:不判斷程式或載入資料的對錯,只有在Action執行時才發現
IDEA開發工具的使用:
1、windows下載maven,配置環境變數
2、plugins 可以手動新增Scala包,也可以idea下載
第三章:
def 函式名(可以無引數,也可以有引數): 返回型別(Unit無返回型別) {
//函式體
如果有返回型別,最後一行被當做是返回型別,不需要return
}
如果函式名(無引數) 呼叫時可以直接函式名
預設引數:可以寫一個預設引數函式名(a:Int = 3),如果沒有賦予其它引數,預設a=3
命名引數:引數名對應好了,順序可以錯亂
迴圈:
1 to 10 (1,2,3,4,5,6,7,8,9,10) 1.to(10)
1 until 10 (1,2,3,4,5,6,7,8,9) 1.until(10)
Range(1,10) (1,2,3,4,5,6,7,8,9)
for(變數 <- 要遍歷的資料)
for(變數 <- 要遍歷的資料 條件)
x.foreach()等價於for迴圈
while(迴圈條件)
根據輸入的資料不定引數進行計算
例:
println(num(1,2,3))
println(num(1,2,3,4))
def num(no:Int*) = {
var result = 0
for(i <- no){
result += i
}
result
}
第四章
封裝:屬性、方法封裝到類中(類是被抽象出來的)
屬性:person
private int id, String name, date birthday geter/seter
方法:eat sleep run
繼承:
父類和子類之間的關係
子類繼承父類,並且還可以重寫override
子類繼承父類,如果父類有的屬性,子類可以不加var/val,父類的方法會執行
重寫:子類需要修改父類的方法、屬性時需要進行重寫 override xxx
抽象類:類的一個或者多個方法、屬性沒有實現(只有定義)
想要呼叫,必須實現所有的類、方法,否則不能被呼叫
多型:父類的引用指向子類
Person person = new Person()
User user = new User()
val p = new User() //父類介面,子類實現功能
類的定義:
private 只在當前類中呼叫
_ 佔位符,使用佔位符必須指定var的資料型別
構造器:
主構造器:
跟在類名後面的屬性叫主構造器 class Person(val name:String, val age:Int)
附屬構造器:
def this(name:String, age:Int, gender:String){
this(name,age) //附構造器第一句話必須呼叫主構造器或者其它附屬構造器
this.gender=gender
}
伴生類、伴生物件:
如果有一個class,還有一個和class同名的object, class是object的伴生類,object是class的伴生物件
object類可以直接val p = Person()
APPLY方法:
在object的apply方法中new class
類名() 呼叫的object.apply
物件() 呼叫的class.apply
case class:通常用在模式匹配中,呼叫時不用new,直接呼叫即可
trait:需要多個繼承的時候with連結
第五章
Array 定長陣列,定義以後陣列長度不能改變
可變陣列:scala.collection.mutable.ArrayBuffer +增加資料,++需要加入Array
不可變陣列:scala.collection.immutable._
List: Nil 是一個不可變的空集合List,是有序的可以重複的 :_* 可以將Seq轉化為list
Buffer:ListBuffer、ArrayBuffer 帶Buffer都是可變的
Set:資料是不能重複的
Map:(key-value)鍵值對操作
第六章
匹配模式:
變數 match{
case x => xxx
case _ => xxx //相當於java的 default
}
條件模式匹配:
變數 match{
case x => xxx
case _ if(xxx) => xxx //注意執行的先後順序
case _ => xxx //相當於java的 default
}
陣列匹配:
def NameScore(array: Array[String]): Unit ={
array match{
case Array("zhangsan") => println("zhangsan")
case Array(x,y) => println("x,y")
case Array("zhangsan", _*) => println("Hi:zhangsan other one") // _* 無限匹配
case _ => println("erveryone")
}
}
NameScore(Array("zhangsan"))
NameScore(Array("zhangsan","lisi", "wangwu"))
NameScore(Array("zhangwu"))
List匹配
def NameScroe(list: List[String]): Unit ={
list match{
case "zhangsan"::Nil => println("zhangsan")
case x::y::Nil => println("x,y")
case "zhangsan":: tail => println("Hi:zhangsan other one")
case _ => println("erveryone")
}
}
NameScroe(List("zhangsan","wangwu"))
NameScroe(List("zhangsan","lisi", "wangwu"))
NameScroe(List("zhangwu"))
型別匹配:
def ObjType(obj: Any){
obj match{
case x:Int => println("Int")
case x:String => println("String")
case x:Map[_,_] => x.foreach(print)
case _ => println("other")
}
}
ObjType(1)
ObjType("1")
ObjType(Map("key"->"value"))
異常處理:
try{
//要執行的程式碼
}catch{
//可以程式碼執行錯誤後,要進行提示或其它資訊寫到這裡
}finally{
//一定會執行,記得要把資源關閉
}
case class 對類進行模式匹配,必須要定義一個頂級的類
Some&None 模式匹配:
val b = Map("PK" -> 18, "zhangsan" -> 12, "lisi" -> 20)
def gredeTest(name:String): Unit ={
val getGred = b.get(name)
getGred match{
case Some(getGred) => println("this some")
case None => println("this None")
}
}
gredeTest("PK")
gredeTest("lisi")
gredeTest("wangwu")
第七章
字串高階操作:
多行:藉助於s"$常量或$變數" 進行列印
val a = "Hello"
val b = "World"
val c = s"$a $b!"
println(c)
使用""""""進行多行列印 Shift + "" 按3次
val a1 =
"""
|Hello
|Wolrd
|!
""".stripMargin
println(a1)
匿名函式:匿名函式沒有函式名,可以賦值給變數或常量或匿名函式
val a = (x:Int) => x+1 a(10) 結果:11
val add = (x:Int, y:Int) => x + y add(2,3) 結果:5
柯里化:將原來接收多個引數的函式變換成接收一個單一的引數
高階函式:
Map:Map(x => (x,1)) 可以把資料進行Tuple(key-value)操作、柯里化
filter:根據條件過濾資料
faltten:將資料進行shuffle,
val l = List(List(1,2), List(3,4))
l.flatten
結果:List[Int] = List(1, 2, 3, 4)
FlatMap:結合了flatten和map的特點
reduce:資料進行計算,例如加法:reduce(_+_)
reduceLeft、reduceRight 從左或右開始計算
fold: 資料進行計算,可以再進行單獨加一些資料,例如加法,累加後額外再加1:reduce(1)(_+_)
foldLeft、foldRight 資料進行計算,例如加法:reduce(_+_)
foreach:迴圈遍歷與for迴圈類似
偏函式:{}內的沒有match的一組case語句可以進行模式匹配
//PartialFunction[String, String] 偏函式特徵[輸入引數,輸出引數]
def moshi:PartialFunction[String, String] = {
case "wangwu" => "王五"
case "lisi" => "李四"
case _ => "None"
}
println(moshi("wangwu"))
第八章
隱士轉換:通過implicit進行隱士轉換,最好把隱士轉換都放到一個類裡面,方便呼叫------------面試重點
implicit def manToSuperMan(man: Man):SuperMan = new SuperMan(man.name)
val superMan = new SuperMan("lisi")
superMan.fly()
class Man(val name:String){
def eat(): Unit ={
println(s"$name is eat.....")
}
}
class SuperMan(val name:String){
def fly(): Unit ={
println(s"$name is fly ......")
}
}
隱士引數:對引數增加implicit
def testParam(implicit name:String){
println(name + "====================")
}
implicit val name = "zhangsan"
testParam
隱士類:對類增加implicit
implicit class classTest(x:String){
def add(a:Int) = a + x
}
println("12345".length)
第九章
讀取檔案:Source.fromFIle(檔案路徑)(檔案編碼可以不寫),getLines整行讀取
讀取MySQL資料:classOf[com.mysql.jdbc.Driver]
讀取XML:this.getClass.getClassLoader.getResource("xml檔案")
\ 某一層
\\ 所有