java 累加器_Spark基础教程——累加器的使用(JAVA)

通常在向Spark传递函数时,比如使用map()函数或者用filter()传条件时,可以使用驱动器程序中定义的变量,但是集群中运行的每个任务都会得到这些变量的一份新的副本,更新这些副本的值也不会影响驱动器中的对应变量。

Spark中存在两个共享变量,累加器与广播变量,分别为结果聚合与广播这两种常见的通信模式突破了这一限制。

累加器,提供了将工作节点中的值聚合到驱动器程序中的简单语法。累加器的一个常见用途是在调试时对作业执行过程中的事件进行计数。

自Spark2.0版本以后,Spark内置支持三种类型的累加器,分别是集合累加器CollectionAccumulator, 浮点型累加器DoubleAccumulator, 整型累加器LongAccumulator。

使用方法如下:SparkSession spark = SparkSession.builder().master("local[*]").getOrCreate();

SparkContext sc = spark.sparkContext();

// 内置的累加器有三种,LongAccumulator、DoubleAccumulator、CollectionAccumulator

// LongAccumulator: 整型累加

LongAccumulator longAccumulator = sc.longAccumulator("longAccumulator");

// DoubleAccumulator: 浮点型累加

DoubleAccumulator doubleAccumulator = sc.doubleAccumulator("doubleAccumulator");

// CollectionAccumulator:集合累加

CollectionAccumulator collectionAccumulator = sc.collectionAccumulator("collectionAccumulator");

Dataset intSet = spark.createDataset(Arrays.asList(5, 4, 3, 10), Encoders.INT());

Dataset result = intSet.map((MapFunction) x -> {

longAccumulator.add(x);

doubleAccumulator.add(x);

collectionAccumulator.add(x);

return x;

}, Encoders.INT()).cache();

result.count();

System.out.println("::::: longAccumulator: " + longAccumulator.value());

System.out.println("::::: doubleAccumulator: " + doubleAccumulator.value());

System.out.println("::::: collectionAccumulator: " + collectionAccumulator.value());

上述代码输出结果如下:::::: longAccumulator: 22

::::: doubleAccumulator: 22.0

::::: collectionAccumulator: [4, 3, 5, 10]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值