累计器:实际上就是将每个分区进行累加
一。原理
相当于集群中的统筹的大变量。
自定义累计器 ,实现AccumulatorParam<xxx> 实现三个方法zero,addAccumulator,addInPlace
val accumulator = sc.accumulator(xxx,new AccumulatorParam...)
accumulator.add(xxxx)
注意:
累计器只能在Driver定义初始化,在Executor端更新,在Executor不能accumulator.value获取值。
二。图解
三。代码实现
package com.bjsxt
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
object Scala {
def main(args: Array[String]): Unit = {
/**
* 累加求和
*/
val conf=new SparkConf().setAppName("test").setMaster("local")