如何创建自定义列表累加器,即List [(Int,Int)]? [英] How to create custom list accumulator, i.e. List[(Int, Int)]?
问题描述
我正在尝试在Apache Spark中使用自定义累加器来累加列表中的对。
结果应为 List [(Int,Int)]
类型。为此,我创建了自定义累加器:
I am trying to use custom accumulator in Apache Spark to accumulate pairs in a list.
The result should have List[(Int, Int)]
type. For this I creat custom accumulator:
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
}
}
但是我无法实例化这种类型的变量。
Yet I can not instantiate variable of this type.
val pairAccum = sc.accumulator(new List():List[(Int,Int)])(AccumPairs)
结果错误。
推荐答案
没有参数的类没有多大意义(如果有的话),因为您隐式创建了一个仍然是单个值 1 。将关键字 class
更改为 object
,您的示例将可用。
A class without parameters doesn't make much sense (if at all) as you "implicitly" create a single value anyway1. Change the keyword class
to object
and your example will work.
更改
class AccumPairs extends AccumulatorParam[List[(Int,Int)]] {
至
object AccumPairs extends AccumulatorParam[List[(Int,Int)]] {
[1]您仍然可以创建多个实例的班级,但他们实际上是相似的。
[1] You still could create multiple instances of the class but they effectively be alike.
这篇关于如何创建自定义列表累加器,即List [(Int,Int)]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!