累加器
package cn.hanjiaxiaozhi.core
import org.apache.spark.rdd.RDD
import org.apache.spark.util.LongAccumulator
import org.apache.spark.{SparkConf, SparkContext}
object AccumulatorTest {
def main(args: Array[String]): Unit = {
val conf: SparkConf = new SparkConf().setAppName("wc").setMaster("local[*]")
val sc: SparkContext = new SparkContext(conf)
sc.setLogLevel("WARN")
var counter1:Int = 0
val data: List[Int] = List(1,2,3,4,5)
data.foreach(i=>{
counter1 += i
})
println(counter1)
println("===============")
var counter2 :Int = 0
val dataRDD: RDD[Int] = sc.parallelize(List(1,2,3,4,5))
dataRDD.foreach(i=>{
counter2+=i
})
println(counter2)
println("===============")
val counter3: LongAccumulator = sc.longAccumulator
dataRDD.foreach(i=>{
counter3.add(i)
})
println(counter3.value)
}
}