ビッグデータシリーズ第4課:scala基礎

5468 ワード

徹底的にScalaモードマッチングとタイプシステムを征服する
第1段階:Spark streaming、spark sql、kafka、sparkカーネル原理(大規模なプロジェクト経験が必要);
第2段階:sparkが運行する各種環境、各種故障の解決、性能の最適化(sparkカーネル、運行原理に精通する);
第3段階:ストリーム処理、機械学習がトップであり、まず前の2段階の内容を把握する必要がある.
王家林先生のゼロ基礎の説明に従って、実戦を重視して、sparkの高数になって、笑ってビッグデータの林になります!
第一部学習ノート
   //scala      java  switch case
    //java  switch case       
    //scala       java                 、  (map,list    )

    //        
    def bigData(data : String): Unit ={
      data match {
        case "Spark" => println("Wow!!!") //   break
        case "Hadoop" => println("Ok")
        case _ => println("Something others")//            
      }
    }
    //bigData("Hadoop")
    //bigData("Flink")

    def bigData2(data : String): Unit ={
      data match {
        case "Spark" => println("Wow!!!") //   break
        case "Hadoop" => println("Ok")
        case _ if data == "Flink" => println("Cool")//     
        case data_ if data_ == "Java" => println("haha" + " : " + data_)//data_             
        case _ => println("Something others")
      }
    }
    //bigData2("Spark")
    //bigData2("Flin")
    //bigData2("Flink")
    //bigData2("Java")

    //         
    def exception(e: Exception): Unit ={
      e match {
        case fileException  : FileNotFoundException => println("File not found :" + fileException)
        case _ : Exception => println("Exception getting thread dump from executor Sex",e)
      }
    }
    //exception(new FileNotFoundException("oops"))

    // scala           //set map   
    def data(array : Array[String]): Unit ={
      array match {
        case Array("Scala") => println("Scala")
        case Array(spark,hadoop,flink) => println(spark + " : " + hadoop + " : " + flink)
        case Array("Spark", _*) => println("Spark...")
        case _ => println("Unknown")
      }
    }
    data(Array("Scala"))
    data(Array("Spark","Hadoop","Flink"))
    data(Array("Spark"))

    //case class        
    //1、case class   java  bean
    //2、   
    //case class Person(name : String)//     val  ,       ,apply  
    case class Person()
    case class Worker(name : String, salary: Double)
    case class Student(name : String, score :Double) extends Person

    def sayHi(person : Person){
      person match{
        case Student(name,score) => println(name + " : " + score)
        case Worker(name,salary) => println(name + " : " + salary)
        case _ => println("Unknown")
      }
    }
    sayHi(Student("Saprk",6.6))

    def sayHi2(person : Person){
      person match{
        case Student(name,score) => println("I am student : " + name + " : " + score)
        case Worker(name,salary) => println("I am worker : " + name + " : " + salary)
        case _ => println("Unknown")
      }
    }
    sayHi2(Student("Saprk",6.6))

    //     Some None
    /*private val restServer =
    if (restServerEnabled) {
      val port = conf.getInt("spark.master.rest.port", 6066)
      Some(new StandaloneRestServer(host, port, conf, self, masterUrl))
    } else {
      None
    }*/

    /**
      *     
      * 1、  
      */
    //        
    class Person1[T](val content : T){
      def getContent(id : T) = id + " _ " + content
    }
    val p = new Person1[String]("Spark")
    println(p.getContent("Scala"))

    //    codec: Class[_ <:] CompressionCodec          (    )
    //    codec: Class[_ :>] CompressionCodec

    //view Bounds                (      )
    //view Bounds    <%          
    //implicit def rddToSequenceFileRDDFunctions[K <% Writable: ClassTag, V <% Writable: ClassTag](
    //Writable: ClassTag
    //T:  
    //  [T]
    //          ,          

    class Compare[T : Ordering](val n1: T, val n2 : T){
      def bigger(implicit ordered : Ordering[T]) = if(ordered.compare(n1,n2) > 0) n1 else n2
    }

    println(new Compare[Int](8,3).bigger)
    println(new Compare[String]("Spark","Hadoop").bigger)

    Ordering[String]
    Ordering[Int]

    new Compare[String]("Spark","Hadoop").bigger
    //Manifest Context Bounds //    
    //[T : Manifest]
    //Array[T]
    /**
      * class Person[+T]          
      */
    //[_]    [T]  Dependency[_]   Dependency[T]

    //Manifest -> ClassTag(Manifest    ClassTag)

    //bstract class RDD[T: ClassTag](
    //@transient private var _sc: SparkContext,
    //@transient private var deps: Seq[Dependency[_]]


    //T:ClassTag           
    /**
      * {{{
      *   scala> def mkArray[T : ClassTag](elems: T*) = Array[T](elems: _*)
      *   mkArray: [T](elems: T*)(implicit evidence$1: scala.reflect.ClassTag[T])Array[T]
      *
      *   scala> mkArray(42, 13)
      *   res0: Array[Int] = Array(42, 13)
      *
      *   scala> mkArray("Japan","Brazil","Germany")
      *   res1: Array[String] = Array(Japan, Brazil, Germany)
      * }}}
      */

第2部作業:SparkソースRDD、HadoopRDD、SparkContext、Master、Workerのソースコードを読み、使用するすべてのパターンマッチングとタイプパラメータの内容を分析する