本節主要內容
- Scala簡單介紹
- 為什么要學習Scala
- Scala語言初步
1. Scala簡單介紹
Scala(Scala Language的簡稱)語言是一種能夠執行於JVM和.Net平台之上的通用編程語言。既可用於大規模應用程序開發,也可用於腳本編程,它由由Martin Odersk於2001開發。2004年開始程序執行在JVM與.Net平台之上。由於其簡潔、優雅、類型安全的編程模式而受到關注。
Scala的創建者——Martin Odersk
在Scala的創建之初,並沒有怎么引起重視,隨着Apache Spark和Apache Kafka這樣基於Scala的大數據框架的崛起,Scala逐步映入大數據從業者的眼簾。
Scala的擁護者們覺得Scala的主要優勢是速度和它的表達性。眼下使用scala的作為支撐公司開發語言的包含Foursquare和Twitter。2009年Twitter把大部分后台系統的開發語言從Ruby換成了Scala。參見這篇文章:Twitter on Scala: A Conversation with Steve Jenson, Alex Payne, and Robey Pointer,” Scalazine, April 3,2009, www.artima.com/scalazine/articles/twitter_on_scala.html.
Scala語言具有例如以下特點:
1 純面向對象編程語言
- (1) Encapsulation/information hiding.
- (2)Inheritance.
- (3)Polymorphism/dynamic binding.
- (4)All predefined types are objects.
- (5) All operations are performed by sending messages to objects.
- (6)All user-defined types are objects.
2 函數式編程語言
定義:Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data.
函數式編程語言應支持以下特性:
(1)高階函數(Higher-order functions)
(2)閉包( closures)
(3)模式匹配( Pattern matching)
(4)單一賦值( Single assignment )
(5)延遲計算( Lazy evaluation)
(6)類型推導( Type inference )
(7)尾部調用優化( Tail call optimization)
(8)類型推導( Type inference )
3 Scala語言具有非常強的兼容性、移植性
Scala執行於JVM上,能夠與JAVA進行互操作,具有與JAVA一樣的平台移植性
4 Scala語法的簡潔
以下給的是java的hadoop wordcount代碼及spark wordcount代碼
能夠看到,spark三行代碼就攻克了hadoop 七八十行代碼做的事情。
2. 為什么要學習Scala
1 開源大數據內存計算框架Spark的流行
- Spark是當前最流行的開源大數據內存計算框架。採用Scala語言實現,由UC 伯克利大學AMPLab實驗室開發(2009)並於2010年開源,在2014年成為Apache基金會的頂級項目http://spark.apache.org/
- Spark有着非常好的性能優勢。
圖片來源:databricks.com/blog/2014/11/05/spark-officiallysets-a-new-record-in-large-scale-sorting.html 社區活躍度
圖片來源:Real-Time Analytics with Spark Streaming
圖片來源:Spark Summit 2015
圖片來源:twitter.com/dberkholz/status/568561792751771648各大公司使用與貢獻情況
圖片來源:Summit Spark 2015 https://spark-summit.org/2015/
- IBM 百萬數據project師計划
【2015年6月17日,北京】IBM(NYSE:IBM)宣布承諾大力推進Apache Spark項目,並稱該項目為:在以數據為主導的,未來十年最為重要的新的開源項目。這一承諾的核心是將Spark嵌入IBM業內率先的分析和商務平台。並將Spark作為一項服務,在IBM Bluemix平台上提供給客戶。IBM還將投入超過3500名研究和開發者在全球十余個實驗室開展與Spark相關的項目,並將為Spark開源生態系統無償提供突破性的機器學習技術——IBM SystemML,同一時候,IBM還將培養超過100萬名Spark數據科學家和數據project師。原文鏈接:http://www.csdn.net/article/a/2015-06-18/15825412
2 Scala是未來大數據處理的主流語言
- 它是Spark框架的開發語言
-
- ”If I were to pick a language to use today other than Java, it would be Scala.” —James Gosling
詹姆斯·高斯林 Java之父
- ”If I were to pick a language to use today other than Java, it would be Scala.” —James Gosling
Scala具有數據處理的天然優勢(語言特點決定)
3. Scala語言初步
1 變量定義
//聲明一個val變量
//與java final關鍵字聲明的變量一樣
//一旦被賦值。便不能更改
//Scala會幫我們進行類型判斷
scala> val helloString="Hello World"
helloString: String = Hello World
//也能夠進行類型指定
scala> val helloString:String="Hello World"
helloString: String = Hello World
//String事實上就是java.lang.String
scala> val helloString:java.lang.String="Hello World"
helloString: String = Hello World
//不能被又一次賦值。由於它是val變量
scala> helloString="Hello Crazy World"
<console>:8: error: reassignment to val
helloString="Hello Crazy World"
^
以下給出的延遲載入變量:
//lazy關鍵字聲明變量
//表示該變量不會立即賦值
//而在真正被使用時才賦值
scala> lazy val helloString="Hello Crazy World"
helloString: String = <lazy>
//在真正使用時被賦值
scala> helloString
res1: String = Hello Crazy World
scala中也存在可變變量。即隨着程序的執行,變量內容能夠動態變化:
//var 聲明可變變量
scala> var helloString="Hello Cruel World"
helloString: String = Hello Cruel World
//又一次賦值
scala> helloString="GoodBye Cruel World"
helloString: String = GoodBye Cruel World
2 函數初步
scala中通過下列方式進行函數定義:
//定義了一個函數,函數中使用return返回結果
scala> def add(a:Int,b:Int):Int={return a+b}
add: (a: Int, b: Int)Int
scala> add(1,2)
res3: Int = 3
//能夠省去return,scala會將最后一個執行語句 //作為函數的返回值
scala> def add(a:Int,b:Int):Int={a+b}
add: (a: Int, b: Int)Int
//省去返回值類型。scala會自己主動進行類型判斷
scala> def add(a:Int,b:Int)={a+b}
add: (a: Int, b: Int)Int
scala> add(1,2)
res4: Int = 3
3 HelloWorld應用程序:
package cn.xtwy.scala.chapter01
//scala應用程序相同採用main方法作為應用程序的入口
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello World")
}
}
加入公眾微信號。能夠了解很多其它最新Spark、Scala相關技術資訊