Using 'case' in PairRDDFunctions.reduceByKey()

前端 未结 1 1419
陌清茗
陌清茗 2020-12-10 08:49

This is the syntax for method reduceByKey

def reduceByKey(func: (V, V) ⇒ V): RDD[(K, V)] 

In a word count program I am practic

相关标签:
1条回答
  • 2020-12-10 09:27

    Scala supports multiple ways of defining anonymous functions. The "case" version is so called Pattern Matching Anonymous Functions which is more or less equivalent to:

    (x: Int, y: Int) => (x, y) match { case (x, y) => x + y }
    

    while version without case is pretty much what it looks like:

    (x: Int, y: Int) => x + y
    

    In this case simple _ + _ would be enough though:

    val counts = words.map(word => (word, 1)).reduceByKey(_ + _)
    

    Probably the simplest case when you can benefit from using pattern matching is when you deal with Scala Options:

    (x: Option[Int], y: Option[Int]) => (x, y) match {
        case (Some(xv), Some(yv)) => xv + yv
        case (Some(xv), _) => xv
        case (_, Some(yv)) => yv
        case _ => 0
    } 
    
    0 讨论(0)
提交回复
热议问题