Scala 如何创建自定义列表累加器,即列表[(Int,Int)]?
我正在尝试使用ApacheSpark中的自定义累加器在列表中累加对。 结果应该有Scala 如何创建自定义列表累加器,即列表[(Int,Int)]?,scala,apache-spark,accumulator,Scala,Apache Spark,Accumulator,我正在尝试使用ApacheSpark中的自定义累加器在列表中累加对。 结果应该有List[(Int,Int)]type。为此,我创建了自定义累加器: import org.apache.spark.AccumulatorParam class AccumPairs extends AccumulatorParam[List[(Int,Int)]] { def zero(initialValue: List[(Int,Int)]): List[(Int,Int)] = {
List[(Int,Int)]
type。为此,我创建了自定义累加器:
import org.apache.spark.AccumulatorParam
class AccumPairs extends AccumulatorParam[List[(Int,Int)]] {
def zero(initialValue: List[(Int,Int)]): List[(Int,Int)] = {
List()
}
def addInPlace(l1: List[(Int,Int)], l2: List[(Int,Int)]): List[(Int,Int)] = {
l1 ++ l2
}
}
但是我不能实例化这种类型的变量
val pairAccum = sc.accumulator(new List():List[(Int,Int)])(AccumPairs)
结果是错误的。请帮忙。这一款有效:
val pairAccum = sc.accumulator(List[(Int,Int)]())( new AccumPairs)
没有参数的类没有多大意义(如果有的话),因为您“隐式”创建了一个值1。将关键字
class
更改为object
,您的示例将起作用
改变
class AccumPairs extends AccumulatorParam[List[(Int,Int)]] {
到
[1] 您仍然可以创建该类的多个实例,但它们实际上是相似的
object AccumPairs extends AccumulatorParam[List[(Int,Int)]] {