您的位置:首页 > 大数据

大数据Spark “蘑菇云”行动前传18课:Scala偏函数、异常、Lazy值编码实战及Spark源码鉴赏

2016-08-08 20:56 846 查看
大数据Spark “蘑菇云”行动前传18课:Scala偏函数、异常、Lazy值编码实战及Spark源码鉴赏

1 Spark源码中Scala偏函数、异常、Lazy值源码鉴赏

2 Scala偏函数、异常、Lazy值编码操作实战

 



 



 



 

package com.dt.spark.scala.bascis

object HelloPartionalFunction {

  def main(args: Array[String]): Unit = {

    val sample =1 to 10

    val isEven:PartialFunction[Int,String]={

      case x if x % 2 == 0 =>   x + "is even"

        }

    isEven(4)

    val evenNumbers = sample collect isEven

    evenNumbers.foreach { println }

     val isOdd:PartialFunction[Int,String]={

      case x if x % 2 == 1 =>   x + "is Odd"

        }

    

     val numbers =sample map {isEven orElse isOdd}

     numbers.foreach { println }

  }

}

 

 

2is even

4is even

6is even

8is even

10is even

1is Odd

2is even

3is Odd

4is even

5is Odd

6is even

7is Odd

8is even

9is Odd

10is even

 

 

 

 

Exception in thread "main" scala.MatchError: 3 (of class java.lang.Integer)

 at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:248)

 at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:246)

 at com.dt.spark.scala.bascis.HelloPartionalFunction$$anonfun$1.applyOrElse(HelloPartionalFunction.scala:6)

 at com.dt.spark.scala.bascis.HelloPartionalFunction$$anonfun$1.applyOrElse(HelloPartionalFunction.scala:6)

 at scala.runtime.AbstractPartialFunction$mcVI$sp.apply$mcVI$sp(AbstractPartialFunction.scala:33)

 at com.dt.spark.scala.bascis.HelloPartionalFunction$.main(HelloPartionalFunction.scala:10)

 at com.dt.spark.scala.bascis.HelloPartionalFunction.main(HelloPartionalFunction.scala)

4is even

 



 

 

 

 

package com.dt.spark.scala.bascis

import java.io.IOException

object HelloExceptionAndLazyValue {

  def main(args: Array[String]): Unit = {

    try {

      1/0

    }catch {

      case ioException:IOException => println("IOException" + ioException.toString())

      case illegaArgs:IllegalArgumentException => println("IllegalArgumentException" + illegaArgs.toString())

      case arithmeticException:ArithmeticException => println ("ArithmeticException"+arithmeticException.toString())

   

    }

   

    lazy val score =100

    println(score)

  }

}

 

ArithmeticExceptionjava.lang.ArithmeticException: / by zero

100
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐