https://tool.oschina.net/apidocs/apidoc?api=scala-docs-2.9.2:API文档
创建累加器
package com.sparksql
import org.apache.spark.util.AccumulatorV2
import scala.collection.mutable
class MyAccumulator extends AccumulatorV2[String,mutable.HashMap[String, Int]] {
//记录数据的容器用于返回
private val resultMap = mutable.HashMap[String,Int]()
//判断是否为空
override def isZero: Boolean = {
//判断是否为空
resultMap.isEmpty
}
//复制一个累加器
override def copy(): AccumulatorV2[String, mutable.HashMap[String, Int]] = {
//创建一个新的
val newAcc = new MyAccumulator
//在原有基础上叠加返回一个新的
resultMap.synchronized{
newAcc.resultMap ++= this.resultMap
}
newAcc
}
//重置
override def reset(): Unit = {
//清除
resultMap.clear()
}
//添加元素 累加在一个元素上的值
override def add(v: String): Unit = {
//在map中添加值