簡體   English   中英

當 RDD 值是一個元組時 ReduceByKey

[英]ReduceByKey when RDD value is a tuple

我是 Apache Spark 的新手,無法讓它工作。

我有一個 (Int,(Int,Int)) 形式的 RDD,並且想在附加第二個元素的同時總結值的第一個元素。

例如,我有以下 RDD:

[(5,(1,0)), (5,(1,2)), (5,(1,5)))]

我希望能夠得到這樣的東西:

(5,3,(0,2,5))

我試過這個:

sampleRdd.reduceByKey{case(a,(b,c)) => (a + b)}

但我得到這個錯誤:

type mismatch;
[error]  found   : Int
[error]  required: String
[error]     .reduceByKey{case(a,(b,c)) => (a + b)}
[error]                                        ^

我怎樣才能做到這一點?

請試試這個

def seqOp = (accumulator: (Int, List[String]), element: (Int, Int)) =>
    (accumulator._1 + element._1, accumulator._2 :+ element._2.toString)

  def combOp = (accumulator1: (Int, List[String]), accumulator2: (Int, List[String])) => {
    (accumulator1._1 + accumulator2._1, accumulator1._2 ::: accumulator2._2)
  }
 
  val zeroVal = ((0, List.empty[String]))

  rdd.aggregateByKey(zeroVal)(seqOp, combOp).collect

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM