package test.wyh.wordcount
import org.apache.spark.util.AccumulatorV2
import org.apache.spark.{SparkConf, SparkContext}
import scala.collection.mutable
object TestAccumulator {
def main(args: Array[String]): Unit = {
//建立Spark连接
val sparkConf = new SparkConf().setMaster("local").setAppName("TestAccumulator")
val sc = new SparkContext(sparkConf)
val rdd = sc.makeRDD(List("hello", "world", "spark", "hello", "scala"))
//创建自定义累加器对象
val myAcc = new MyAccumulator()
//向Spark注册累加器对象,参数二为累加器名称
sc.register(myAcc, "WordCountAcc")
//对原始的RDD遍历执行累加
rdd.foreach(
word => {
//原始RDD每个元素都执行累加器
myAcc.add(word)
}
)