本文整理汇总了Scala中org.apache.spark.AccumulableParam类的典型用法代码示例。如果您正苦于以下问题:Scala AccumulableParam类的具体用法?Scala AccumulableParam怎么用?Scala AccumulableParam使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AccumulableParam类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: HashMapParam
//设置package包名称以及导入依赖的类
package com.kakao.cuesheet.accumulator
import org.apache.spark.serializer.JavaSerializer
import org.apache.spark.{AccumulableParam, SparkConf}
import spire.math.Numeric
import scala.collection.mutable.{HashMap => MutableHashMap}
case class HashMapParam[K, V: Numeric]() extends AccumulableParam[MutableHashMap[K, V], (K, V)] {
private val add = implicitly[Numeric[V]].additive.op _
def addAccumulator(acc: MutableHashMap[K, V], elem: (K, V)): MutableHashMap[K, V] = {
val (k1, v1) = elem
acc += acc.find(_._1 == k1).map {
case (k2, v2) => k2 -> add(v1, v2)
}.getOrElse(elem)
acc
}
def addInPlace(acc1: MutableHashMap[K, V], acc2: MutableHashMap[K, V]): MutableHashMap[K, V] = {
acc2.foreach(elem => addAccumulator(acc1, elem))
acc1
}
def zero(initialValue: MutableHashMap[K, V]): MutableHashMap[K, V] = {
val ser = new JavaSerializer(new SparkConf(false)).newInstance()
val copy = ser.deserialize[MutableHashMap[K, V]](ser.serialize(initialValue))
copy.clear()
copy
}
}