3000门徒内部训练绝密视频(泄密版)第1课:大数据最火爆语言Scala光速入门

时间:2021-05-04 03:29:41

大数据最火爆语言Scala光速入门

scala 可以使用java的库
scala 的工厂方法:apply
条件表达式有返回值
数组可以用to ,箭头 <-
最后一行内容的值是整个代码块的返回值
def函数 定义函数,调用不按顺序
函数:def 函数名,参数名:类型名,可以设定默认值,后可跟=号,如def f1 ( param1:String, param2: Int = 30) = param1 + param2
有默认值的参数调用时可以不加参数,另外调用时如果指定参数名的话可以不考虑参数顺序。
参数数量可变:def sum(numbers:Int*) *表示变长 调用时不可sum(0 to 100) ,因为0 to 100是range类型,而参数中要求是变量,但是可以用 0 to 100 _* ,表示变成多个值

过程:无返回值的函数,定义函数返回值为Unit,在参数列表之后加上:Unit ,或者将函数定义后的=改变成花括号

lazy 类型:第一次被定义时计算

异常

try {
val content = fromFile("/usr/local/spark/sfijweoijgr/")
}catch{
case _: FileNotFoundException => println("Ooooops!!! File not found")
} finally {
println("Byebye world!")
}

集合
数组val arr = new ArrayInt

ArrayBuffer的insert,remove方法

scala> val arr1 = Array("Scala","Spark")
arr1: Array[String] = Array(Scala, Spark)

scala> val arr1 = Array.apply("Scala","Spark")
arr1: Array[String] = Array(Scala, Spark)

scala> Array
res3: Array.type = scala.Array$@54d18072

scala> arr1(2) = "Hadoop"
java.lang.ArrayIndexOutOfBoundsException: 2
... 33 elided

scala> val arrbuf = ArrayBuffer[Int]()
<console>:7: error: not found: value ArrayBuffer
val arrbuf = ArrayBuffer[Int]()
^

scala> import scala.collection.mutable.A
AVLIterator AbstractIterable AbstractSet ArrayBuilder ArraySeq
AVLTree AbstractMap AnyRefMap ArrayLike ArrayStack
AbstractBuffer AbstractSeq ArrayBuffer ArrayOps

scala> import scala.collection.mutable.Array
ArrayBuffer ArrayBuilder ArrayLike ArrayOps ArraySeq ArrayStack

scala> import scala.collection.mutable.ArrayBu
ArrayBuffer ArrayBuilder

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val arrbuf = ArrayBuffer[Int]()
arrbuf: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> arrbuf += 10
res5: arrbuf.type = ArrayBuffer(10)

scala> arrbuf
res6: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10)

scala> arrbuf(1)
java.lang.IndexOutOfBoundsException: 1
at scala.collection.mutable.ResizableArray$class.apply(ResizableArray.scala:43)
at scala.collection.mutable.ArrayBuffer.apply(ArrayBuffer.scala:48)
... 33 elided

scala> arrbuf += ( 12,23,35,56)
res8: arrbuf.type = ArrayBuffer(10, 12, 23, 35, 56)

scala> arrbuf
res9: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 12, 23, 35, 56)

scala> arrbuf ++= Array1,2,3,4)
<console>:1: error: illegal character '\uff08'
arrbuf ++= Array1,,2,3,4)
^
<console>:1: error: illegal character '\uff0c'
arrbuf ++= Array1,,2,3,4)
^

scala> arrbuf ++= Array(1,2,3,4)
res10: arrbuf.type = ArrayBuffer(10, 12, 23, 35, 56, 1, 2, 3, 4)

scala> arrbuf
res11: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 12, 23, 35, 56, 1, 2, 3, 4)

scala> arrbuf.t
tail takeWhile toIndexedSeq toMap toString transpose
tails to toIterable toSeq toTraversable trimEnd
take toArray toIterator toSet toVector trimStart
takeRight toBuffer toList toStream transform

scala> arrbuf.t
tail takeWhile toIndexedSeq toMap toString transpose
tails to toIterable toSeq toTraversable trimEnd
take toArray toIterator toSet toVector trimStart
takeRight toBuffer toList toStream transform

scala> arrbuf.trim
trimEnd trimStart

scala> arrbuf.trim
trimEnd trimStart

scala> arrbuf.trimEnd(3)

scala> arrbuf
res13: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 12, 23, 35, 56, 1)

scala> arrbuf
res14: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 12, 23, 35, 56, 1)

scala> arrbuf.in
indexOf indexWhere init insert intersect
indexOfSlice indices inits insertAll

scala> arrbuf.insert(4,100)

scala> arrbuf
res16: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 12, 23, 35, 100, 56, 1)

scala> arrbuf.insert(6,7,8,9)

scala> arrbuf
res18: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 12, 23, 35, 100, 56, 7, 8, 9, 1)

scala> arrbuf.remove(0)
res19: Int = 10

scala> arrbuf
res20: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(12, 23, 35, 100, 56, 7, 8, 9, 1)

scala> arrbuf.remove(1,2)

scala> arrbuf
res22: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(12, 100, 56, 7, 8, 9, 1)

scala> val arr2 = arrbuf.toArray
arr2: Array[Int] = Array(12, 100, 56, 7, 8, 9, 1)

scala> arr2.toBuffer
res23: scala.collection.mutable.Buffer[Int] = ArrayBuffer(12, 100, 56, 7, 8, 9, 1)

scala> arr2.toString
def toString(): String

scala> arr2.toBuffer
res24: scala.collection.mutable.Buffer[Int] = ArrayBuffer(12, 100, 56, 7, 8, 9, 1)

scala> for ( elem <- arr2) { elem}

scala> for ( elem <- arr2) {println(elem)}
12
100
56
7
8
9
1

scala> for( i <- 1 until (arr2.length,1)) println(arr2(i))
100
56
7
8
9
1

scala> for( i <- 1 until (arr2.length,2)) println(arr2(i))
100
7
9

scala> for( i <- 0 until (arr2.length,2)) println(arr2(i))
12
56
8
1

scala> for( i <- 0 until (arr2.length,2)) println(arr2(i))
12
56
8
1

scala> arr2
res31: Array[Int] = Array(12, 100, 56, 7, 8, 9, 1)

scala> for(i <- (0 until arr2.length).reverse) println(arr2(i))
1
9
8
7
56
100
12

scala> import scala.util.Sorting._
import scala.util.Sorting._

scala> quickSort(arr2)

scala> arr2
res34: Array[Int] = Array(1, 7, 8, 9, 12, 56, 100)

scala> val arr3 = for(i <- arr2) yield i*i
arr3: Array[Int] = Array(1, 49, 64, 81, 144, 3136, 10000)

scala> val arr4 = for(i <- arr2 if i%3 == 0) yield i*i
arr4: Array[Int] = Array(81, 144)

scala> arr2.filter(_%3 ==0).map(i => i*i)
res35: Array[Int] = Array(81, 144)

scala> arr2.filter{_%3 ==0}.map{i => i*i}
res36: Array[Int] = Array(81, 144)

scala> arr2.filter{_%3 ==0}map{i => i*i}
res3: Array[Int] = Array(144, 81)

yield 把后面的每一个元素收集起来并组拼成一个集合

作业:删掉数组中第一个负数后面的所有负数

scala> val person = Map("Spark" ->6, "Hadoop" -> 11)
person: scala.collection.immutable.Map[String,Int] = Map(Spark -> 6, Hadoop -> 11)

scala> person("Hadoop")
res4: Int = 11

scala> val person = scala.collection.mutable.Map("Spark" ->6, "Hadoop" -> 11)
person: scala.collection.mutable.Map[String,Int] = Map(Hadoop -> 11, Spark -> 6)

scala> person += ("Flink" -> 5)
res5: person.type = Map(Hadoop -> 11, Spark -> 6, Flink -> 5)

scala> person -= "Flink"
res6: person.type = Map(Hadoop -> 11, Spark -> 6)

scala> val sparkValue = if (person.contains("Spark")) person("Spark") else 1000
sparkValue: Int = 6

scala> val sparkValue = person.getOrElse
getOrElse getOrElseUpdate

scala> val sparkValue = person.getOrElse("Spark", 1000)
sparkValue: Int = 6

scala> val sparkValue = person.getOrElse("Flink", 1000)
sparkValue: Int = 1000

scala> for((key,value) <- person) println(key+":"+value)
Hadoop:11
Spark:6

scala> for((key,value) <- person) println(key+":")
Hadoop:
Spark:

scala> val person = scala.collection.mutable.S.Map("Spark" ->6, "Hadoop" -> 11)
Seq SetProxy Subscriber SynchronizedSet
SeqLike SortedSet SynchronizedBuffer SynchronizedStack
Set Stack SynchronizedMap
SetBuilder StackProxy SynchronizedPriorityQueue
SetLike StringBuilder SynchronizedQueue

scala> val person = scala.collection.immutable.S.Map("Spark" ->6, "Hadoop" -> 11)
Seq SetProxy SortedSet Stream StreamView StringLike
Set SortedMap Stack StreamIterator StreamViewLike StringOps

scala> val person = scala.collection.immutable.SortedMap("Spark" ->6, "Hadoop" -> 11)
person: scala.collection.immutable.SortedMap[String,Int] = Map(Hadoop -> 11, Spark -> 6)

TUPLE:
scala> val tuple = ("Spark", 6, 99.0)
tuple: (String, Int, Double) = (Spark,6,99.0)

scala> tuple._1
res9: String = Spark

scala> tuple._2
res10: Int = 6

scala> tuple._3
res11: Double = 99.0