大数据Spark “蘑菇云”行动前传第20课:Scala提取器、注解深度实战详解及Spark源码鉴赏大数据Spark “蘑菇云”行动前传第20课:Scala提取器、注解深度实战详解及Spark源码鉴赏
1 Spark源码提取器、注解
2 Scala提取器、注解深度
这里是个面试题,模式匹配时候一定会用的到是什么方法呢?答 unapply
package com.dt.spark.scala.bascis
@DTCoder(name="scala",salary=1000)
case class Person1(name:String,age:Int){
val content =name
}
class DTCoder(val name:String,val salary:Int) extends annotation.Annotation
object DTCoder{
def apply(name:String,salary:Int) ={
println("DTCoder apply methode invoked !!!")
new DTCoder(name,salary)
}
def unapply(information:DTCoder)= {
Some(( information.name,information.salary ))
/* def unapply(information:String)= {
Some((information.substring(0,information.indexOf(" ")),information.substring(0,information.indexOf(" ")+1)))
}*/
}
}
object HelloExtractor {
def main(args: Array[String]): Unit = {
val person =Person1.apply("spark",6)
val Person1(name,age)=person
println(name+ " : " +age)
person match {
case Person1(name,age)=>println("wow "+name+ " : " +age)
}
val dtcoder = DTCoder("hadoop",200)
val DTCoder(dtname1,dtsalary1) =dtcoder
println("dtname1 "+dtname1+ " : " +dtsalary1)
//val DTCoder(dtname,dtsalary) = "spark 100"
// println("dtname "+dtname+ " : " +dtsalary)
}
}
运行结果
spark : 6
wow spark : 6
DTCoder apply methode invoked !!!
dtname1 hadoop : 200