在RDD [Array [Int]]上执行Scala Reduce()操作

时间:2018-06-29 12:22:52

标签: eclipse scala apache-spark reduce spire

我有一个一维矩阵的RDD。我正在尝试做一个非常基本的归约运算,以汇总各个分区中矩阵相同位置的值。

我正在使用:

var z=x.reduce((a,b)=>a+b)

var z=x.reduce(_ + _)

但是我收到一个错误消息: 类型不匹配;找到Array [Int],应为:String

我查了一下,找到了链接 Is there a better way for reduce operation on RDD[Array[Double]]

所以我尝试使用 import.spire.implicits._ 所以现在我没有任何编译错误,但是在运行代码之后,我得到了java.lang.NoSuchMethodError。我在下面提供了整个错误。任何帮助将不胜感激。

java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V
at spire.math.NumberTag$Integral$.<init>(NumberTag.scala:9)
at spire.math.NumberTag$Integral$.<clinit>(NumberTag.scala)
at spire.std.BigIntInstances.$init$(bigInt.scala:80)
at spire.implicits$.<init>(implicits.scala:6)
at spire.implicits$.<clinit>(implicits.scala)
at main.scala.com.ucr.edu.SparkScala.HistogramRDD$$anonfun$9.apply(HistogramRDD.scala:118)
at main.scala.com.ucr.edu.SparkScala.HistogramRDD$$anonfun$9.apply(HistogramRDD.scala:118)
at scala.collection.TraversableOnce$$anonfun$reduceLeft$1.apply(TraversableOnce.scala:190)
at scala.collection.TraversableOnce$$anonfun$reduceLeft$1.apply(TraversableOnce.scala:185)
at scala.collection.Iterator$class.foreach(Iterator.scala:893)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
at scala.collection.TraversableOnce$class.reduceLeft(TraversableOnce.scala:185)
at scala.collection.AbstractIterator.reduceLeft(Iterator.scala:1336)
at org.apache.spark.rdd.RDD$$anonfun$reduce$1$$anonfun$15.apply(RDD.scala:1012)
at org.apache.spark.rdd.RDD$$anonfun$reduce$1$$anonfun$15.apply(RDD.scala:1010)
at org.apache.spark.SparkContext$$anonfun$33.apply(SparkContext.scala:2125)
at org.apache.spark.SparkContext$$anonfun$33.apply(SparkContext.scala:2125)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
at org.apache.spark.scheduler.Task.run(Task.scala:108)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:338)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:0)

据我了解,您正在尝试按数组中的位置减少项目。您应该考虑压缩数组,同时减少rdd:

val a: RDD[Array[Int]] = ss.createDataset[Array[Int]](Seq(Array(1,2,3), Array(4,5,6))).rdd

    a.reduce{case (a: Array[Int],b: Array[Int]) =>
        val ziped = a.zip(b)
        ziped.map{case (i1, i2) => i1 + i2}
    }.foreach(println)

输出:

5
7
9