Sparkä¹æ¯åºäºJVMï¼æ们æçåå¸å¼ç³»ç»ï¼åå©JVMï¼èä¸ä¸å®æ¯Javaè¯è¨ã Sparkåæ¶æ¯ä¸é´ä»¶KAFKAçé½æ¯ç¨Scalaç¼åçï¼å¦å¥½Scalaæ¯ææ¡Sparkçå
³é®ã
Scalaåºç¡è¯æ³å
¥é¨å®æ
é¦å
ï¼åç
§ç¸å
³æ»ç¥ï¼å¨Linuxä¸åå«ä¸è½½å®è£
JavaãScalaï¼ç¶åé
ç½®JavaåScalaç¯å¢åéãå®è£
å®æ¯ï¼å¨ç»ç«¯æ²å
¥scalaå³å¯è¿å
¥Scalaå½ä»¤è¡ï¼å¦ä¸æ示ï¼
root@Master:~# scala
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66).
Type in expressions to have them evaluated.
Type :help for more information.
ç®åæµè¯
scala> 1+2
res0: Int = 3
scala> 1.5*2
res1: Double = 3.0
scala> 3*res1
res2: Double = 9.0
//æTabé®ï¼å½ä»¤èªå¨è¡¥å
¨
scala> res2.to
toByte toChar toDouble toFloat toInt toLong toShort toString
åé
var声æå¯ååéï¼val声æä¸å¯ååéã
val声æçä¸å¯ååéï¼ä¸å¸ææ°æ®è¢«æ¹åï¼RDDå
é¨çæ°æ®é½æ¯ä¸å¯åï¼æ以å¨Sparkä¸ä¸è¬é½æ¯ä½¿ç¨valã
//ä¸é¢å£°æäºä¸ä¸ªä¸å¯ååéresultï¼resultçå¼ä¸å¯æ¹åã
scala> val result=2+10
result: Int = 12
//åè¥ä¿®æ¹resultçå¼ï¼ä¼æ示åºéï¼å¦ä¸ï¼
scala> result=13
<console>:8: error: reassignment to val
result=13
//var声æå¯ååéï¼
scala> var name="Spark"
name: String = Spark
scala> name="Scala" //å¯ä»¥ä¿®æ¹åénameçå¼
name: String = Scala
scala> name //nameçå¼ç°å¨ä¸ºScala
res4: String = Scala
//val声æä¸å¯ååé
//age被声æ为Intç±»åçä¸å¯ååé
scala> val age: Int=0
age: Int = 0
//声æ为Stringç±»å
scala> val name:String=null
name: String = null
ä¸è¡ä»£ç 声æå¤ä¸ªåé
scala> val age1,age2,age3=0
age1: Int = 0
age2: Int = 0
age3: Int = 0
åºæ¬æ°æ®ç±»åçèªå¨è½¬æ¢æä½
Scalaèªå·±å¯ä»¥å®æåºæ¬æ°æ®ç±»åçèªå¨è½¬æ¢æä½ã
//è¾å
¥æ´æ°10ï¼æTabé®çï¼å¯ä»¥çè§å®æ¬èº«çå¾å¤æ¹æ³ï¼
scala> 10.to
toByte toChar toDouble toFloat toInt toLong toShort toString
scala> 10.toString
res5: String = 10
scala> 0.to(5)
res6: scala.collection.immutable.Range.Inclusive = Range(0, 1, 2, 3, 4, 5)
ç»äºæ们ä¸ä¸ªä¸å¯åçéåï¼è·å¾çç»ææ¯ï¼0~5ï¼æ以Scalaä¸åç对象ï¼
Scalaéç§è½¬æ¢ï¼
å½ç±»åæ¬èº«æ²¡æ没æè¿ä¸ªæ¹æ³ï¼ä½æ¯åéè¦è°ç¨è¿ä¸ªæ¹æ³æ¶ï¼å
é¨å°±ä¼èªå¨è§¦åéå¼è½¬æ¢ãåæç示ä¾ä¸ï¼Intæ¬èº«æ²¡ætoè¿æ ·çæ¹æ³ï¼Scalaå¼æå
é¨ä¼éå¼èªå¨è½¬æ¢æRichIntï¼å°±åä¸é¢0.to(5)è¿æ ·çè°ç¨ï¼RichInt对象类åæætoè¿æ ·çå½æ°ã
scala> 1+1
res7: Int = 2
//å 为Scalaä¸åç对象ï¼æ以以ä¸ç¤ºä¾åå¯ä»¥åæï¼
scala> 1.+(1)
res9: Double = 2.0
è¿ä¸ªç¤ºä¾ï¼å
é¨å
¶å®å°±æ¯Intçä¸ä¸ªéå¼è½¬æ¢ï¼+æ¯ç±»çä¸ä¸ªæ¹æ³ã
Scala没æ++ã---æä½è¿ç®ï¼
scala> var age=10
age: Int = 10
// Scala没æ++ã---æä½è¿ç®
scala> age++
<console>:9: error: value ++ is not a member of Int
age++
^
//ä½æ¯++è¿ç®å¯ä»¥ç¨ä¸é¢æ¹å¼å®ç°ï¼
scala> age +=1
scala> age
res12: Int = 11
æ±æ大ãæå°å¼
scala> min(20,4)
<console>:8: error: not found: value min
min(20,4)
^
该示ä¾å 为没æ导å
¥åºï¼æ以åºéã
scala> import scala.math._ //导å
¥mathåº
import scala.math._
scala> min(20,4)
res14: Int = 4
applyå·¥åæé å®ç°æ¹æ³
å¨Sparkä¸ï¼å¤§éçå®ä¾çæé é½æ¯ä½¿ç¨äºapplyæ¹å¼ã
scala> Array(1,2,3,4)
res15: Array[Int] = Array(1, 2, 3, 4)
scala> val array=Array(1,2,3,4)
array: Array[Int] = Array(1, 2, 3, 4)
//arrayæ¯ä¸ä¸ªå£°ææ´æ°ç±»åçæ°ç»åéï¼ å
¶å®å
é¨æ¯èªå¨è°ç¨äºArray.applyæ¹æ³ï¼çåå¦ä¸ï¼
scala> val array = Array.apply(1,2,3,4)
array: Array[Int] = Array(1, 2, 3, 4)
æ¡ä»¶æ§å¶ã循ç¯
// if表达å¼ç¤ºä¾ï¼
scala> if(age>=18) "æ年人" else "å°å©"
res16: String = æ年人
scala> val result=if(age>=18) "æ年人" else "å°å©"
result: String = æ年人
scala> result
res17: String = æ年人
scala> val result = if(age>=18){
| "adult"
| buffered=10
| buffered
| }
以ä¸ä¸ä¸ªä»£ç åï¼ä»£ç ååé¢æ个è¿åå¼bufferedï¼ä»£ç åçè¿åå¼å°±æ¯æåä¸è¡çå¼ã
æå°å¼
scala> println("Spark") //è¾åºä¸è¡å符串并æ¢è¡
Spark
scala> println("\nSpark") //æ¢è¡ï¼è¾åºä¸è¡å符串åæ¢è¡ã\næ¯æ¢è¡è½¬ä¹ç¬¦ã
Spark
scala> print("Spark") //è¾åºä¸è¡å符串ï¼ä¸æ¢è¡
Spark
scala>
å¡«å
å ä½ç¬¦
scala> printf("%sæ¯å¤§æ°æ®æ¡æ¶çæªæ¥", "Spark") //%sæ¯å ä½ç¬¦
Sparkæ¯å¤§æ°æ®æ¡æ¶çæªæ¥
读åå
容
readLineç¨äºè¯»åè¾å
¥çå
容
scala> readLine //æ¤æ¶æ²å
¥Scalaä¹åï¼ç¶åå车
res28: String = Scala
scala> res28
res29: String = Scala
è¡¥å
说æï¼readLineæ¯ä¸ä¸ªæ¹æ³ï¼å¦ææ¹æ³å¦æ没æåæ°ï¼é£ä¹å¯ä»¥ä¸å¸¦æ¬å·ï¼readLine()è·readLineææä¸æ ·ã
循ç¯
//声æä¸ä¸ªå¯ååéï¼åå§å¼ä¸º100
scala> var element=100
element: Int = 100
//while循ç¯ç¤ºä¾ï¼
scala> while(element>90){
| println(element)
| element -= 1
| }
100
99
98
97
96
95
94
93
92
91
scala> 0 to element
res32: scala.collection.immutable.Range.Inclusive = Range(0, 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)
//for循ç¯éå并æå°
scala> for(i<-80 to element) println(i)
80
81
82
83
84
85
86
87
88
89
90
//循ç¯å¹¶å¢å æ¡ä»¶å¤æ
scala> for(i<-0 to element if i%2==0) print(i+" ")
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90
//for循ç¯ï¼å¹¶breakéåº
scala> import scala.util.control.Breaks._ //æ·»å breakå¼ç¨
import scala.util.control.Breaks._
scala> for(i<-1 to 10){
| if(i==4) break
| println(i)
| }
1
2
3
scala.util.control.BreakControl
//循ç¯ï¼å¹¶return
scala> val n=5
n: Int = 5
scala> def f1:Any = {
| for(i <-1 to 10){
| if(i==n) return i
| println(i)
| }
| }
f1: Any
scala> f1
1
2
3
4
res1: Any = 5
解é以ä¸ä»£ç åï¼defæ¯å®ä¹ä¸ä¸ªå½æ°ãf1å°±æ¯ä¸ä¸ªå½æ°ã
äºã Scalaå½æ°å
¥é¨å®æ
å½æ°ç¤ºä¾
å½æ°çå®ä¹ä½¿ç¨defå
³é®åï¼å¹¶ä¸å½æ°ä½æåæè¿åå¼ã
//声æäºä¸ä¸ªå½æ°f3ï¼ä¸¤ä¸ªåæ°ï¼param1æ¯Stringç±»åï¼param2为Intç±»åãparam2é»è®¤å¼ä¸º30ï¼å 为æäºé»è®¤å¼ï¼é£ä¹å¨è°ç¨ä¸å¯ä»¥ä¸ä¼ param2ã
scala> def f3(param1:String, param2:Int=30) = param1 + param2
f3: (param1: String, param2: Int)String
//è°ç¨å½æ°f3,ä¼ å
¥ç¬¬ä¸ä¸ªåæ°param1å¼ä¸ºSparkï¼æ²¡æä¼ å
¥ç¬¬äºä¸ªåæ°ï¼é»è®¤ä¸º30ã
scala> f3("Spark")
res4: String = Spark30
//带ååæ°è°ç¨ï¼å³å¨å½æ°è°ç¨æ¶ï¼æ¾ç¤ºæå®åæ°åï¼å¹¶ä¸æ顺åºä¼ å
¥ã
scala> f3(param2=100, param1="Scala")
res5: String = Scala100
//åé¿åæ°, å®ä¹äºä¸ä¸ªsumå½æ°ï¼åæ°numbersæ¯åæåæ°ï¼å³ä¼ å
¥çIntåé个æ°ä¸å®ãå¨å½æ°ä½ä¸ï¼å¯¹ä¼ å
¥çå
¨é¨Intåéè¿è¡å¾ªç¯éå并累计æ±åï¼æåæç»æè¿åã
scala> def sum(numbers: Int*)={var result=0; for(element<-numbers) result +=element; result}
sum: (numbers: Int*)Int
scala> sum(1,2,3,4,5,6,7,8,9,10)
res1: Int = 55
//ä¸é¢ç¤ºä¾æ¯ä¸ä¸ªé常ç»å
¸çè¯æ³
scala> sum(1 to 10: _*) // _* 表示æåéé¢çæ¯ä¸ªå
ç´ , ç¶åä½ä¸ºåé¿åæ°ä¼ é
res3: Int = 55
è¿ç¨
没æè¿åå¼çå½æ°å°±æ¯è¿ç¨ã
//è¿æ¯ä¸ä¸ªå½æ°
scala> def morning(content:String) = "Good" + content
morning: (content: String)String
//è¿æ¯ä¸ä¸ªè¿ç¨
scala> def morning(content:String) { println( "Good" + content)}
morning: (content: String)Unit
//强å¶å£°æä¸ä¸ªè¿ç¨
scala> def morning(content:String):Unit = "Good" + content
morning: (content: String)Unit
声ælazyç±»å
scala> import scala.io.Source._ //导å
¥å¼ç¨åº
import scala.io.Source._
//声æä¸ä¸ªlazyç±»åçåécontentï¼æå¼ä¸ä¸ªä¸åå¨çæ件ã
scala> lazy val content = fromFile("/root/txt")
content: scala.io.BufferedSource = <lazy>
以ä¸ç¤ºä¾æ§è¡ä¸ä¼åºéï¼è¡¨æcontentåé并没ææ§è¡ã
//å¦æå»ælazyå
³é®åï¼é£ä¹ä¼åºéï¼æ示æ件ä¸åå¨ã
scala> val content = fromFile("/root/txt")
java.io.FileNotFoundException: /root/txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
èæ¶çæä½ï¼å¨å¤§ååå¸å¼ç³»ç»ä¸ï¼æ¯è¾å¸¸è§ã声ælazyç±»åçåéï¼å¨éè¦çæ¶åæå»æ§è¡ã
å¼å¸¸
//é¦å
导å
¥ç¸å
³å¼ç¨å
scala> import java.io._
import java.io._
scala>
//示ä¾æå¼ä¸ä¸ªåå¨çæ件ï¼ä½¿ç¨tryâ¦catchæè·å¼å¸¸
scala> try{
| val content = fromFile("/root/.bashrc").mkString
| }catch{
| //caseæ¯ä¸ä¸ªåå½æ°
| case _: FileNotFoundException => println("Oh, file not found!")
| }finally{
| println("Ok!")
| }
Ok!
scala>
//示ä¾æå¼ä¸ä¸ªä¸åå¨çæ件ï¼ä½¿ç¨tryâ¦catchæè·å¼å¸¸
scala> try{
| val content = fromFile("/root/.bashrc111").mkString
| }catch{
| //caseæ¯ä¸ä¸ªåå½æ°
| case _: FileNotFoundException => println("Oh, file not found!")
| }finally{
| println("Ok!")
| }
Oh, file not found!
Ok!
ä¸ã Scalaä¸ArrayãMapãTupleå®æ
Array
//声æåéarr为Arrayæ´æ°ç±»åçæ°ç»ï¼å
å«5个å
ç´ ã
scala> val arr= new Array[Int](5)
arr: Array[Int] = Array(0, 0, 0, 0, 0)
//访é®ç¬¬ä¸ä¸ªå
ç´
scala> arr(2)
res15: Int = 0
//ä¿®æ¹ç¬¬ä¸ä¸ªå
ç´
scala> arr(2)=8
//å次æ¥çarræ°ç»ï¼åç°ç¬¬ä¸ä¸ªå
ç´ å¼å·²ç»åæ8äºã
scala> arr
res17: Array[Int] = Array(0, 0, 8, 0, 0)
è¡¥å
说æï¼åæ声æarræ°ç»åéæ¶ï¼æ以æå®å£°æ为valä¸å¯ååéï¼è¿åªæ¯è¡¨æarrçå°åä¸å¯ä»¥åï¼ä½æ¯æ°ç»éé¢çå
ç´ è¿æ¯å¯ä»¥ååçã
//å¨Sparkä¸ï¼æ´å¸¸è§å°å建æ°ç»æ¯ç´æ¥éè¿ç±»å
scala> val arr1 = Array("Scala", "Spark")
arr1: Array[String] = Array(Scala, Spark)
该示ä¾ä¸ï¼å£°æarr1为æ°ç»åéæ¶ï¼æ²¡æ使ç¨newå
³é®åï¼ä¹æ²¡ææå®Stringç±»åï¼ç³»ç»é»è®¤æ ¹æ®å
ç´ å¼ï¼èªå¨æ¨å¯¼åºå
ç´ çç±»å为Stringã
没æ使ç¨newå
³é®åï¼å
¶å®å®å
é¨è°ç¨äºapplyæ¹æ³ï¼ applyæ¯å·¥åç±»æé å¨ãçåäºä¸é¢çåæ³ï¼
scala> val arr1 = Array.apply("Scala", "Spark")
arr1: Array[String] = Array(Scala, Spark)
//ç»Arrayå¢å å
ç´ ãä¸é¢åæ³ä¼åºéï¼ç»arr1æ°ç»å¢å ä¸ä¸ªå
ç´ ï¼æ¯å¦ï¼
scala> arr1(2)="Hadoop"
java.lang.ArrayIndexOutOfBoundsException: 2
at .<init>(<console>:16)
at .<clinit>(<console>)
â¦â¦
å¦æéè¦ç»Arrayå¢å å
ç´ ï¼é£ä¹æ¤æ¶å°±åºè¯¥ä½¿ç¨ArrayBufferç±»åã
ArrayBuffer
//é¦å
导å
¥åº
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
//å®ä¹ä¸ä¸ªArrayBufferç±»åçåéarrbuffer
scala> val arrbuffer=ArrayBuffer[Int]()
arrbuffer: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
//åarrbufferä¸å¢å ä¸ä¸ªå
ç´ ï¼å¼ä¸º10
scala> arrbuffer += 10
res23: arrbuffer.type = ArrayBuffer(10)
//åarrbufferä¸å¢å å¤ä¸ªå
ç´
scala> arrbuffer += (11,1,3,5)
res25: arrbuffer.type = ArrayBuffer(10, 11, 1, 3, 5)
//æ¥çarrbufferçå
容
scala> arrbuffer
res26: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 11, 1, 3, 5)
//åarrbufferä¸å¢å ä¸ä¸ªæ°ç»
scala> arrbuffer ++= Array(1,2,3,4)
res27: arrbuffer.type = ArrayBuffer(10, 11, 1, 3, 5, 1, 2, 3, 4)
//æªæarrbufferåé¢ç3个å
ç´
scala> arrbuffer.trimEnd(3)
//å次æ¥çarrbufferçå
容ï¼åç°å
ç´ ï¼2, 3, 4被æªæ
scala> arrbuffer
res29: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 11, 1, 3, 5, 1)
//å¨ç¬¬5个ä½ç½®ï¼æå
¥å
ç´ å¼100
scala> arrbuffer.insert(5,100)
//æ¥çarrbufferçå
容
scala> arrbuffer
res32: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 11, 1, 3, 5, 100, 1)
//å¨ç¬¬2个ä½ç½®ï¼æå
¥å¤ä¸ªå
ç´ ï¼200,300,400
scala> arrbuffer.insert(2,200,300,400)
//æ¥çarrbufferçå
容
scala> arrbuffer
res34: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 11, 200, 300, 400, 1, 3, 5, 100, 1)
//ä»arrbufferä¸ç§»é¤ç¬¬3个ä½ç½®ä¸çå
ç´
scala> arrbuffer.remove(3)
res35: Int = 300 //被移é¤çå¼æ¯300
//å次æ¥çarrbufferçå
容ï¼åç°ç¬¬3个ä½ç½®ä¸çå
ç´ 300ä¸è§äºã
scala> arrbuffer
res36: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 11, 200, 400, 1, 3, 5, 100, 1)
//ä»arrbufferä¸ç§»é¤ç¬¬2个ä½ç½®å¼å§çï¼3个å
ç´ ï¼å³ï¼200, 400, 1
scala> arrbuffer.remove(2,3)
//å次æ¥çarrbufferçå
容ï¼åç°ä¸ä¸ªå
ç´ ï¼200, 400, 1 ä¸è§äºã
scala> arrbuffer
res38: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 11, 3, 5, 100, 1)
//å¯åæ°ç»åæä¸å¯åæ°ç»,æ¤æ¶arr2æ¯ä¸ä¸ªä¸å¯åæ°ç»
scala> val arr2 = arrbuffer.toArray
arr2: Array[Int] = Array(10, 11, 3, 5, 100, 1)
// Array.toBufferçç»æåæä¸ä¸ªArrayBuffer
scala> arr2.toBuffer
res40: scala.collection.mutable.Buffer[Int] = ArrayBuffer(10, 11, 3, 5, 100, 1)
//éåä¸ä¸ªæ°ç»ï¼
scala> for(elem <- arr2) println(elem)
10
11
3
5
100
1
//éåæ°ç»æ¶å ä¸æ¡ä»¶
scala> arr2
res42: Array[Int] = Array(10, 11, 3, 5, 100, 1)
//éåæ¶çæ¡ä»¶ï¼è·³è¿å¶æ°ä½ä¸çå
ç´
scala> for(i <- 0 until (arr2.length, 2)) println(arr2(i))
10
3
100
æ¤æ¶æå°åºæ¥çç»æï¼è·³è¿äºå
ç´ ï¼11ã5ã1
//ä»å°¾é¨å¼å§éå
scala> for(i <- (0 until arr2.length).reverse) println(arr2(i))
1
100
5
3
11
10
//对æ°ç»è¿è¡æåº
//导å
¥æåºå
scala> import scala.util.Sorting
import scala.util.Sortin
温馨提示:内容为网友见解,仅供参考