Accumulator
Accumulator简介
Accumulator是spark提供的累加器,顾名思义,该变量只能够增加。
只有driver能获取到Accumulator的值(使用value方法),Task只能对其做增加操作(使用 +=)。你也可以在为Accumulator命名(不支持Python),这样就会在spark web ui中显示,可以帮助你了解程序运行的情况。
Accumulator使用
使用示例
举个最简单的accumulator的使用例子:
//在driver中定义
val accum = sc.accumulator(0, "Example Accumulator")
//在task中进行累加
sc.parallelize(1 to 10).foreach(x=> accum += 1)
//在driver中输出
accum.value
//结果将返回10
res: 10
累加器的错误用法
val accum= sc.accumulator(0, "Error Accumulator")
val data = sc.parallelize(1 to 10)
//用accumulator统计偶数出现的次数,同时偶数返回0,奇数返回1
val newData = data.map{x => {
if(x%2 == 0){
accum += 1
0
}else 1
}}
//使用action操作触发执行
newData.count
//此时accum的值为5,是我们要的结果
accum.value
//继续操作,查看刚才变动的数据,foreach也是action操作
newData.foreach(println)
//上个步骤没有进行累计器操作,可是累加器此时的结果已经是10了
//这并不是我们想要的结果
accum.value
原因分析
官方对这个问题的解释如下描述:
For accumulator updates performed inside actions only, Spark guarantees that each task’s update to the accumulator will only be applied once, i.e. restarted tasks will not update the value. In transformations, users should be aware of that each task’s update may be applied more than once if tasks or job stages are re-executed.
我们都知道,spark中的一系列transform操作会构成一串长的任务链,此时需要通过一个action操作来触发,accumulator也是一样。因此在一个action操作之前,你调用value方法查看其数值,肯定是没有任何变化的。
所以在第一次count(action操作)之后,我们发现累加器的数值变成了5,是我们要的答案。
之后又对新产生的的newData进行了一次foreach(action操作),其实这个时候又执行了一次map(transform)操作,所以累加器又增加了5。最终获得的结果变成了10。
解决办法
看了上面的分析,大家都有这种印象了,那就是使用累加器的过程中只能使用一次action的操作才能保证结果的准确性。
事实上,还是有解决方案的,只要将任务之间的依赖关系切断就可以了。什么方法有这种功能呢?你们肯定都想到了,cache,persist。调用这个方法的时候会将之前的依赖切除,后续的累加器就不会再被之前的transfrom操作影响到了。
val accum= sc.accumulator(0, "Error Accumulator")
val data = sc.parallelize(1 to 10)
//代码和上方相同
val newData = data.map{x => {...}}
//使用cache缓存数据,切断依赖。
newData.cache.count
//此时accum的值为5
accum.value
newData.foreach(println)
//此时的accum依旧是5
accum.value
总结
使用Accumulator时,为了保证准确性,只使用一次action操作。如果需要使用多次则使用cache或persist操作切断依赖。
https://stackoverflow.com/questions/29494452/when-are-accumulators-truly-reliable
链接:http://www.jianshu.com/p/1b7c9a63bc7c
Accumulator的更多相关文章
- flink - accumulator
读accumlator JobManager 在job finish的时候会汇总accumulator的值, newJobStatus match { case JobStatus.FINISHE ...
- TI C66x DSP 系统events及其应用 - 5.1(QM accumulator的配置)
以下解说在详细应用中,event与中断ISR的设置.以对QM的queue监控产生中断(不是EXCEP)为例,主要包含配置QM accumulator(用于监控QM queue)与配置ISR(ISR与e ...
- Spark累加器(Accumulator)陷阱及解决办法
累加器(accumulator)是Spark中提供的一种分布式的变量机制,其原理类似于mapreduce,即分布式的改变,然后聚合这些改变.累加器的一个常见用途是在调试时对作业执行过程中的事件进行计数 ...
- Accumulator<Long> implements of JavaSparkContext in Spark1.x
As we all know , up to Spark 1.6.2, JavaSparkContext only provides two kinds of accumulators: Intege ...
- 08、共享变量(Broadcast Variable和Accumulator)
共享变量工作原理 Spark一个非常重要的特性就是共享变量. 默认情况下,如果在一个算子的函数中使用到了某个外部的变量,那么这个变量的值会被拷贝到每个task中.此时每个task只能操作自己的那份 ...
- 【Spark Java API】broadcast、accumulator
转载自:http://www.jianshu.com/p/082ef79c63c1 broadcast 官方文档描述: Broadcast a read-only variable to the cl ...
- boost的accumulator rolling_mean的使用
Boost.Accumulators is both a library for incremental statistical computation as well as an extensibl ...
- Spark笔记之累加器(Accumulator)
一.累加器简介 在Spark中如果想在Task计算的时候统计某些事件的数量,使用filter/reduce也可以,但是使用累加器是一种更方便的方式,累加器一个比较经典的应用场景是用来在Spark St ...
- spark.Accumulator
scala> val accum = sc.accumulator() accum: org.apache.spark.Accumulator[Int] = scala> sc.paral ...
随机推荐
- java中static、this、super、final用途、用法及实例
一.static 请先看下面这段程序: public class Hello { public static void main(String[] args){ //(1) System.out.pr ...
- JavaScript中的继承与原型链
先看一个例子 function User(){} var u1 = new User(); console.log(u1.prototype);// undefined 使用对象实例无法访问到prot ...
- 用zd1211+Ubuntu 10.04实现的AP
[日期:2010-06-24] zd1211 在Ubuntu 10.04 LTS上的master mode 的问题解决之后,理论上就可以把zd1211 USB网卡用来做一个AP了,实际上还有几个问 ...
- Android开发学习之数据存取
Android系统中提供了一种文件读写的方法,可以将一些数据以文件的形式保存在设备中.比如一些word文档,PDF文档,图片,音频,视频文件等. 使用文件读写方法的步骤: 1.调用Context.op ...
- 【php】基础学习1
其中包括php基础.字符串和正则表达式的学习.具体如下: <html xmlns=http://www.w3.org/1999/xhtml> <head> <meta h ...
- 3.Java基础:String对象的创建和使用
一.常用的创建方式 String s1=”abc“: String s2=”abc“: s1==s2 ==> true 解析:s1和s2指向的是同一个字符串池地址 二.不常用的创建方式 S ...
- MVC图片上传、浏览、删除 ASP.NET MVC之文件上传【一】(八) ASP.NET MVC 图片上传到服务器
MVC图片上传.浏览.删除 1.存储配置信息 在web.config中,添加配置信息节点 <appSettings> <add key="UploadPath" ...
- Java并发和多线程:序
近期,和不少公司的"大牛"聊了聊,当中非常多是关于"并发和多线程"."系统架构"."分布式"等方面内容的.不少问题, ...
- CodeForces 550A Two Substrings(模拟)
[题目链接]click here~~ [题目大意]: You are given string s. Your task is to determine if the given string s ...
- linux extundelete 删除文件恢复
extundelete是基于Linux的一个数据恢复工具,它通过分析文件系统的日志,解析出所有文件的inode信息,从而可以恢复Linux下主流的ext3,ext4文件系统下被误删除的文件. [问题案 ...