How to create custom list accumulator, i.e. List[(Int, Int)]?

自作多情 提交于 2019-12-11 00:01:53

问题


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)

results in error. Please help.


回答1:


This one works:

val pairAccum = sc.accumulator(List[(Int,Int)]())( new AccumPairs)



回答2:


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.

Change

class AccumPairs extends AccumulatorParam[List[(Int,Int)]] {

to

object AccumPairs extends AccumulatorParam[List[(Int,Int)]] {

[1] You still could create multiple instances of the class but they effectively be alike.



来源:https://stackoverflow.com/questions/34798578/how-to-create-custom-list-accumulator-i-e-listint-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!