当在Driver端定义一个变量后,在Executor内对该变量的修改,对Driver端内的变量是无影响的。想要在Executor中对Driver端的变量进行修改,可以使用累加器
//每次累加整型的值,给累加器起名字:aaa,通过aaa.name可以获取
val aaa = sc.longAccumulator("aaa")
val bbb = sc.longAccumulator
//每次累加小数行的值
val ccc = sc.doubleAccumulator("cccc")
val ddd = sc.doubleAccumulator
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setMaster("local").setAppName("test")
val sc = new SparkContext(conf)
sc.setLogLevel("ERROR")
val lines: RDD[String] = sc.textFile("./data/words").repartition(2)
val aaa = sc.longAccumulator("aaa")
val bbb = sc.longAccumulator
val ccc = sc.doubleAccumulator("cccc")
val ddd = sc.doubleAccumulator
var num = 0
lines.map(line => {
aaa.add(1)
bbb.add(1)
ccc.add(1.1)
ddd.add(1.1)
println(num + ": " + line)
num+=1
num + ": " + line
}).collect()
println("aaa: " + aaa.value + " " + aaa.name)
println("bbb: " + bbb.value + " " + bbb.name)
println("ccc: " + ccc.value + " " + ccc.name)
println("ddd: " + ddd.value + " " + ddd.name)