Spark体系
    
    
    
    Spark体系
   
    
    
    语言区别
   
    语言分类:编译型,解释型
    
    1.编译型:C
    
    2.解释型:Python
    
    3.Java是啥
    
    1.需要编译 字节码
    
    2.解释执行、直接执行
   
    C:可移植 代码对应不同平台,各自编译
    
    Java:可移动性,一次编译,到处运行
    
     JVM是核心
    
    !
   
    编译器!:
    
     编译型,解释型根本区别在哪?
    
    
    
     是否是强类型,什么是类型:宽度
    
   
SCALA on JVM 一定有一个编译
    JAVA / SCALA
    
    编译器 / 编译器 =>思考一个问题:scala代码和java不一样(编译器做了很多事情)
    
    
     JVM
    
   
    再聊语言:模型:
    
    1. 面向过程的 第一类值:
    
     基本类型 +指针
    
    
    2. 面向对象的 第一类值:
    
     基本类型 + 对象类型
    
    
    3. 函数式的 第一类值:
    
     基本类型 + 对象类型 + 函数
    
   
    
     SCALA 面向对象的函数式编程语言
    
   
    int a;
    
    a=33;
    
    1.明文给出 推断
    
    2.传参 很难发生推断
   
    var a = 1
    
    a=“sdfsdf”
    
    var b = “1”
    
    var c = ‘1’
    
    xx(a) {} //推断不出来
    
    推断不代表糊弄,防止运行期报错
   
    
    
    使用scala
   
- 开发 jdk sdk(编译器)
 - 运行 jdk jre
 
    V: spark 2.3.x > scala 2.11 > jre/jdk 1.8.uxxx
    
    
     https://www.oracle.com/technetwork/java/javase/archive-139210.html
    
   
————–coder————-
    主流:使用 集成工具 :
    
     IDE
    
   
    
   
1.  IDEA  +plugin  +编译器   》创建scala项目了!
2.  启动屏幕:configure-》;plugins  
main,class,object
主方法只能写在 object定义的文件中
object和class啥区别:
回顾一个问题:java中有一个知识点 静态
分号可有可无
scala是包级别区分,类名可以和文件名不一致
    
    
    语法学习
   
    
    
    Object_Class
   
//约等于  static  单例对象
//static
//单例  new    scala的编译器很人性化   让你人少写了很多代码
object ooxx {
  //  private val xo:xxoo = new xxoo()
  private val xo = new ooxx(11)
  //  Integer num = 0;
  //  var/val   var:变量  val常量 取代了final
  /* var a=3
   a=4
   val b=4
   b=5*/
  private val name = "object:zhangsan"
  println("ooxx....up")
  def main(args: Array[String]): Unit = {
    println("hello from ooxx")
    xo.printMsg()
  }
  println("ooxx....down")
}
//类里,裸露的代码是默认构造中的。有默认构造
//个性化构造!!
//类名构造器中的参数就是类的成员属性,且默认是val类型,且默认是private
//只有在类名构造其中的参数可以设置成var,其他方法函数中的参数都是val类型的,且不允许设置成var类型
class ooxx(sex: String) {
  var name = "class:zhangsan"
  def this(xname: Int) {
    //必须调用默认构造
    this("abc")
  }
  var a: Int = 3
  //  private val value = new ooxx()
  println(s"ooxx....up$a....")
  def printMsg(): Unit = {
    println(s"sex: ${ooxx.name}")
  }
  println(s"ooxx....up${a + 4}")
}
    
    
    IF_WHILE_FOR
   
import scala.collection.immutable
object Lesson01_IF_WHILE_FOR {
  //自己特征:class  object
  //流控
  def main(args: Array[String]): Unit = {
    /*
    if
    while
    for
     */
    var a=0
    if(a <= 0){
      println(s"$a<0")
    }else{
      println(s"$a>=0")
    }
    var  aa=0
    while(aa <10){
      println(aa)
      aa=aa+1
    }
    println("-----------------------------")
    //for
//    for(i=0;i<10;i++)
//    for( P x : xs)
    val seqs = 1 until  10
    println(seqs)
    //循环逻辑,业务逻辑
    for( i <-  seqs if(i%2==0)){
      println(i)
    }
    println("----------------------")
//    for(i <- 1 to 9){
//      for (j <- 1 to 9){
//        if(j<=i) print(s"$i * $j = ${i*j}\t")
//        if(j == i ) println()
//      }
//    }
    var num = 0
    for(i <- 1 to 9;j <- 1 to 9 if(j<=i)){
      num+=1
        if(j<=i) print(s"$i * $j = ${i*j}\t")
        if(j == i ) println()
    }
    println(num)
    val seqss: immutable.IndexedSeq[Int] = for ( i <- 1 to 10) yield {
      var x = 8
      i + x
    }
//    println(seqss)
    for(i <-seqss){
      println(i)
    }
  }
}
    
    
    Functions
   
import java.util
import java.util.Date
object Lesson02_Functions {
  //成员方法
  def ooxx(): Unit ={
    println("hello object")
  }
  def main(args: Array[String]): Unit = {
    //  方法  函数
    println("-------1.basic----------")
    //返回值,参数,函数体
    def fun01() {
      println("hello world")
    }
    fun01()
    var x = 3
    var y = fun01()
    println(y)
    //想有返回
    //    public void sdfsd(){}
    //    public String sdfsdf(){}
    //有return必须给出返回类型
    def fun02() = {
      new util.LinkedList[String]()
    }
    //参数:必须给出类型,是val
    //class 构造,是var,val
    def fun03(a: Int): Unit = {
      println(a)
    }
    fun03(33)
    println("-------2.递归函数----------")
    //递归先写触底!  触发什么报错呀
    def fun04(num: Int): Int = {
      if (num == 1) {
        num
      } else {
        num * fun04(num - 1)
      }
    }
    val i: Int = fun04(4)
    println(i)
    println("-------3.默认值函数----------")
    def fun05(a: Int = 8, b: String = "abc"): Unit = {
      println(s"$a\t$b")
    }
    //    fun05(9,"def")
    fun05(22)
    fun05(b = "ooxx")
    println("-------4.匿名函数----------")
    //函数是第一类值
    //函数:
    //1,签名 :(Int,Int)=>Int :  (参数类型列表)=> 返回值类型
    //2,匿名函数: (a:Int,b:Int) => { a+b }  :(参数实现列表)=> 函数体
    var xx: Int = 3
    var yy: (Int, Int) => Int = (a: Int, b: Int) => {
      a + b
    }
    val w: Int = yy(3, 4)
    println(w)
    println("--------5.嵌套函数---------")
    def fun06(a: String): Unit = {
      def fun05(): Unit = {
        println(a)
      }
      fun05()
    }
    fun06("hello")
    println("--------6.偏应用函数---------")
    def fun07(date: Date, tp: String, msg: String): Unit = {
      println(s"$date\t$tp\t$msg")
    }
    fun07(new Date(), "info", "ok")
    var info = fun07(_: Date, "info", _: String)
    var error = fun07(_: Date, "error", _: String)
    info(new Date, "ok")
    error(new Date, "error...")
    println("--------7.可变参数---------")
    def fun08(a: Int*): Unit = {
      for (e <- a) {
        println(e)
      }
      //      def foreach[U](f: A => U): Unit
      //      a.foreach(   (x:Int)=>{println(x)}   )
      //      a.foreach(   println(_)   )
      a.foreach(println)
    }
    fun08(2)
    fun08(1, 2, 3, 4, 5, 6)
    println("--------8.高阶函数---------")
    //函数作为参数,函数作为返回值
    //函数作为参数
    def computer(a: Int, b: Int, f: (Int, Int) => Int): Unit = {
      val res: Int = f(a, b)
      println(res)
    }
    computer(3, 8, (x: Int, y: Int) => {
      x + y
    })
    computer(3, 8, (x: Int, y: Int) => {
      x * y
    })
    computer(3, 8, _ * _)
    //函数作为返回值:
    def factory(i: String): (Int, Int) => Int = {
      def plus(x: Int, y: Int): Int = {
        x + y
      }
      if (i.equals("+")) {
        plus
      } else {
        (x: Int, y: Int) => {
          x * y
        }
      }
    }
    computer(3, 8, factory("-"))
    println("--------9.柯里化---------")
    def fun09(a: Int)(b: Int)(c: String): Unit = {
      println(s"$a\t$b\t$c")
    }
    fun09(3)(8)("sdfsdf")
    def fun10(a: Int*)(b: String*): Unit = {
      a.foreach(println)
      b.foreach(println)
    }
    fun10(1, 2, 3)("sdfs", "sss")
    println("--------*.方法---------")
    //方法不想执行,赋值给一个引用  方法名+空格+下划线
    val funa = ooxx
    println(funa)
    val func = ooxx _
    func()
    //语法 ->  编译器  ->  字节码   <-  jvm规则
    //编译器,衔接 人  机器
    //java 中 +: 关键字
    //scala中+: 方法/函数
    //scala语法中,没有基本类型,所以你写一个数字  3  编辑器/语法,其实是把 3 看待成Int这个对象
//    3 + 2
//    3.+(2)
//    3:Int
  }
  /*
  学习scala就是为了多学一门语言吧?
  感觉不如python,不仅学了语言,也学了工具。
  理解有哪些偏差? 老师??
  编译型  C   《   贼快
  解释型  python   《   慢  贼慢
  JAVA:其实不值钱,最值钱的是JVM
  JAVA:  解释型,编译过程,类型   比 python 快
  JVM:为什么值钱  是C写的, 【字节码(二进制) >JVM(堆/堆外(二进制))<  kernel(mmap,sendfile) 】  更快!!
   */
}
    
    
    Collections
   
import java.util
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
object Lesson03_Collections {
  def main(args: Array[String]): Unit = {
    //你是一个javacoder
    val listJava = new util.LinkedList[String]()
    listJava.add("hello")
    //scala还有自己的collections
    //1.数组
    //Java中泛型是<>  scala中是[],所以数组用(n)
    //  val 约等于 final  不可变描述的是val指定的引用的值(值:字面值,地址)
    val arr01 = Array[Int](1,2,3,4)
//    arr01=Array(1,2,3,3,3,3)
    arr01(1)=99
    println(   arr01(0)  )
    for (elem <- arr01) {
      println(elem)
    }
    //遍历元素,需要函数接收元素
    arr01.foreach(println)
    println("--------------list-------------")
    //2.链表
    //scala中collections中有个2个包:immutable,mutable  默认的是不可变的immutable
    val list01 = List(1,2,3,4,5,4,3,2,1)
    for (elem <- list01) {
      println(elem)
    }
    list01.foreach(println)
//    list01.+=(22)
    val list02 = new ListBuffer[Int]()
    list02.+=(33)
    list02.+=(34)
    list02.+=(35)
    //TODO:学习  scala数据集中的  ++ +=  ++:  :++
    list02.foreach(println)
    println("--------------Set-------------")
    val set01: Set[Int] = Set(1,2,3,4,2,1)
    for (elem <- set01) {
      println(elem)
    }
    set01.foreach(println)
    import scala.collection.mutable.Set
    val set02: mutable.Set[Int] = Set(11,22,33,44,11)
    set02.add(88)
    set02.foreach(println)
    val set03: Predef.Set[Int] = scala.collection.immutable.Set(33,44,22,11)
//    set03.add
    println("--------------tuple-------------")
//    val t2 = new Tuple2(11,"sdfsdf")  //2元素的Tuple2  在scala描绘的是K,V
    val t2 = (11,"sdfsdf")  //2元素的Tuple2  在scala描绘的是K,V
    val t3 = Tuple3(22,"sdfsdf",'s')
    val t4: (Int, Int, Int, Int) = (1,2,3,4)
    val t22: ((Int, Int) => Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int) = ( (a:Int,b:Int)=>a+b+8   ,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4)
    println(t2._1)
    println(t4._3)
//    val i: Int = t22._1(8)
//    println(i)
    println(t22._1)
    val tIter: Iterator[Any] = t22.productIterator
    while(tIter.hasNext){
      println(tIter.next())
    }
    println("--------------map-------------")
    val map01: Map[String, Int] = Map( ("a",33) ,  "b"->22  ,("c",3434),("a",3333)  )
    val keys: Iterable[String] = map01.keys
    //option: none  some
    println(map01.get("a").get)
//    println(map01.get("w").get)
    println(map01.get("a").getOrElse("hello world"))
    println(map01.get("w").getOrElse("hello world"))
    for (elem <- keys) {
      println(s"key: $elem   value: ${map01.get(elem).get}")
    }
//    keys.foreach()
    val map02: mutable.Map[String, Int] = scala.collection.mutable.Map(("a",11),("b",22))
    map02.put("c",22)
    println("--------------艺术-------------")
    val list = List(1,2,3,4,5,6)
    list.foreach(println)
    val listMap: List[Int] = list.map( (x:Int) => x+10  )
    listMap.foreach(println)
    val listMap02: List[Int] = list.map(  _*10 )
    list.foreach(println)
    listMap02.foreach(println)
    println("--------------艺术-升华------------")
    val listStr = List(
      "hello world",
      "hello msb",
      "good idea"
    )
//        val listStr = Array(
//      "hello world",
//      "hello msb",
//      "good idea"
//    )
//        val listStr = Set(
//      "hello world",
//      "hello msb",
//      "good idea"
//    )
    val flatMap= listStr.flatMap(  (x:String)=> x.split(" ") )
    flatMap.foreach(println)
    val mapList = flatMap.map( (_,1) )
    mapList.foreach(println)
    //以上代码有什么问题吗?  内存扩大了N倍,每一步计算内存都留有对象数据;有没有什么现成的技术解决数据计算中间状态占用内存这一问题~?
    //iterator!!!!!
    println("--------------艺术-再-升华------------")
    //基于迭代器的原码分析
    val iter: Iterator[String] = listStr.iterator  //什么是迭代器,为什么会有迭代器模式?  迭代器里不存数据!
    val iterFlatMap= iter.flatMap(  (x:String)=> x.split(" ") )
//    iterFlatMap.foreach(println)
    val iterMapList = iterFlatMap.map( (_,1) )
    while(iterMapList.hasNext){
      val tuple: (String, Int) = iterMapList.next()
      println(tuple)
    }
//    iterMapList.foreach(println)
    //1.listStr真正的数据集,有数据的
    //2.iter.flatMap  没有发生计算,返回了一个新的迭代器
  }
}
    
   
- iterMapList想要值发现没有,从iterFlatMap中拿
 - iteratorFlatMap发现没有值,从listStr中拿取
 - 拿到后hasNext有值了,加载到cur中,hello world,根据也去分割后返回hello给iterMapList
 - 调用next方法,打印,继续拿world。
 - 当hello world拿完后,再向iterFlatMap中拿,iterFlatMap向listStr中拿
 
    有点像
    
     双亲委派机制
    
    ,但又不是。这是迭代器的好处,在中间
    
     维护一个指针索引位置,一个cur元素大小
    
    即可,
    
     不需要多个内容保存在缓存中,占用空间
    
    。
   
    
    
    高级
   
    
    
    trait
   
    
     多继承
    
   
trait  God{
  def say(): Unit ={
    println("god...say")
  }
}
trait Mg{
  def ku(): Unit ={
    println("mg...say")
  }
  def haiRen():Unit
}
class Person(name:String)  extends   God with Mg{
  def hello(): Unit ={
    println(s"$name say hello")
  }
  override def haiRen(): Unit = {
    println("ziji shixian ....")
  }
}
object Lesson04_trait {
  def main(args: Array[String]): Unit = {
    val p = new Person("zhangsan")
    p.hello()
    p.say()
    p.ku()
    p.haiRen()
  }
}
    
    
    case_class
   
    
     样例类
    
   
case class Dog(name:String,age:Int){
}
object Lesson05_case_class {
  def main(args: Array[String]): Unit = {
    val dog1 =  Dog("hsq",18)
    val dog2 =  Dog("hsq",18)
    println(dog1.equals(dog2))
    println(dog1 == dog2)
  }
}
    
    
    match
   
    
     匹配
    
   
object Lesson06_match {
  def main(args: Array[String]): Unit = {
    val tup: (Double, Int, String, Boolean, Int) = (1.0,88,"abc",false,44)
    val iter: Iterator[Any] = tup.productIterator
    val res: Iterator[Unit] = iter.map(
      (x) => {
        x match {
          case 1 => println(s"$x...is 1")
          case 88 => println(s"$x ...is 88")
          case false => println(s"$x...is false")
          case w: Int if w > 50 => println(s"$w...is  > 50")
          case _ => println("wo ye bu zhi dao sha lei xing ")
        }
      }
    )
    while(res.hasNext)  println(res.next())
  }
}
    
    
    PartialFunction
   
    
     偏移函数
    
   
object Lesson07_PartialFunction {
  def main(args: Array[String]): Unit = {
    def xxx:PartialFunction[  Any,String] ={
      case "hello"  => "val is hello"
      case x:Int => s"$x...is int"
      case _ => "none"
    }
    println(xxx(44))
    println(xxx("hello"))
    println(xxx("hi"))
  }
}
    
    
    Implicit
   
    
     隐式转换
    
   
    
     扩展
    
   
import java.util
object Lesson08_implicit {
  def main(args: Array[String]): Unit = {
    val listLinked = new util.LinkedList[Int]()
    listLinked.add(1)
    listLinked.add(2)
    listLinked.add(3)
    val listArray = new util.ArrayList[Int]()
    listArray.add(1)
    listArray.add(2)
    listArray.add(3)
//    list.foreach(println)   //3个东西:  list数据集  foreach 遍历行为  println 处理函数
//    def foreach[T](list:util.LinkedList[T], f:(T)=>Unit): Unit ={
//      val iter: util.Iterator[T] = list.iterator()
//      while(iter.hasNext) f(iter.next())
//    }
//    foreach(list,println)
//    val xx = new XXX(list)
//    xx.foreach(println)
    //隐式转换:  隐式转换方法
    implicit def sdfsdf[T](list:util.LinkedList[T]) ={
      val iter: util.Iterator[T] = list.iterator()
      new XXX(iter)
    }
    implicit def sldkfjskldfj[T](list:java.util.ArrayList[T]) ={
      val iter: util.Iterator[T] = list.iterator()
      new XXX(iter)
    }
    listLinked.foreach(println)
    listArray.foreach(println)
    //spark  RDD N方法 scala
    //隐式转换类
//    implicit  class KKK[T](list:util.LinkedList[T]){
//      def foreach( f:(T)=>Unit): Unit ={
//        val iter: util.Iterator[T] = list.iterator()
//        while(iter.hasNext) f(iter.next())
//      }
//    }
//    list.foreach(println) //必须先承认一件事情:  list有foreach方法吗?  肯定是没有的~! 在java里这么写肯定报错。。
    //这些代码最终交给的是scala的编译器!
    /*
    1,scala编译器发现 list.foreach(println)  有bug
    2,去寻找有没有implicit  定义的方法,且方法的参数正好是list的类型!!!
    3,编译期:完成你曾经人类:
    //    val xx = new XXX(list)
//    xx.foreach(println)
  *,编译器帮你把代码改写了。。。!
     */
    implicit val sdfsdfsd:String = "werwe"
//    implicit val sdfsdfs:String = "wangwu"
    implicit val sssss:Int = 88
    def ooxx(age:Int)(implicit name:String ): Unit ={
      println(name+" "+age)
    }
//    def sdfsf(name:String = "wangwu")
//    ooxx("zhangsan")
    ooxx(66)("jkljkl")
    ooxx(66)
  }
}
class XXX[T](list:util.Iterator[T]){
  def foreach( f:(T)=>Unit): Unit ={
    while(list.hasNext) f(list.next())
  }
}
//class XXX[T](list:util.LinkedList[T]){
//
//  def foreach( f:(T)=>Unit): Unit ={
//    val iter: util.Iterator[T] = list.iterator()
//    while(iter.hasNext) f(iter.next())
//  }
//
//}