java

 /**
* accumulator可以让多个task共同操作一份变量,主要进行多个节点对一个变量进行共享性的操作,accumulator只提供了累加的功能
* 只有driver可以获取accumulator的值
* @author Tele
*/
public class AccumulatorDemo {
private static SparkConf conf = new SparkConf().setMaster("local").setAppName("AccumulatorDemo");
private static JavaSparkContext jsc = new JavaSparkContext(conf); public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6); JavaRDD<Integer> rdd = jsc.parallelize(list); /*
* Accumulator<Integer> accumulator = jsc.accumulator(10);
*
* rdd.foreach(new VoidFunction<Integer>() {
*
* private static final long serialVersionUID = 1L;
*
* @Override public void call(Integer t) throws Exception { accumulator.add(t);
* } }); System.out.println(accumulator.value());
*/ LongAccumulator la = new LongAccumulator();
la.setValue(100L); jsc.sc().register(la, "数值累加器"); rdd.foreach(new VoidFunction<Integer>() { private static final long serialVersionUID = 1L; @Override
public void call(Integer t) throws Exception {
// 不能在算子内部获得accumulator.value()
la.add(t);
}
}); System.out.println(la.value());
jsc.close();
}
}

scala

 object AccumulatorDemo {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setMaster("local").setAppName("accumulator");
val sc = new SparkContext(conf); val arr = Array(1, 2, 3, 4, 5);
val rdd = sc.parallelize(arr, 1); val accumulator = new LongAccumulator;
accumulator.add(100); sc.register(accumulator); rdd.foreach(accumulator.add(_)); println(accumulator.value); }
}

spark accumulator累加器的更多相关文章

  1. spark.Accumulator

    scala> val accum = sc.accumulator() accum: org.apache.spark.Accumulator[Int] = scala> sc.paral ...

  2. Spark RDD概念学习系列之rdd持久化、广播、累加器(十八)

    1.rdd持久化 2.广播 3.累加器 1.rdd持久化 通过spark-shell,可以快速的验证我们的想法和操作! 启动hdfs集群 spark@SparkSingleNode:/usr/loca ...

  3. 【Spark篇】---Spark中广播变量和累加器

    一.前述 Spark中因为算子中的真正逻辑是发送到Executor中去运行的,所以当Executor中需要引用外部变量时,需要使用广播变量. 累机器相当于统筹大变量,常用于计数,统计. 二.具体原理 ...

  4. Spark共享变量(广播变量、累加器)

    转载自:https://blog.csdn.net/Android_xue/article/details/79780463 Spark两种共享变量:广播变量(broadcast variable)与 ...

  5. 【Spark Java API】broadcast、accumulator

    转载自:http://www.jianshu.com/p/082ef79c63c1 broadcast 官方文档描述: Broadcast a read-only variable to the cl ...

  6. Spark累加器

    spark累计器 因为task的执行是在多个Executor中执行,所以会出现计算总量的时候,每个Executor只会计算部分数据,不能全局计算. 累计器是可以实现在全局中进行累加计数. 注意: 累加 ...

  7. pyspark中使用累加器Accumulator统计指标

    评价分类模型的性能时需要用到以下四个指标 最开始使用以下代码计算,发现代码需要跑近一个小时,而且这一个小时都花在这四行代码上 # evaluate model TP = labelAndPreds.f ...

  8. spark累加器、广播变量

    一言以蔽之: 累加器就是只写变量 通常就是做事件统计用的 因为rdd是在不同的excutor去执行的 你在不同excutor中累加的结果 没办法汇总到一起 这个时候就需要累加器来帮忙完成 广播变量是只 ...

  9. spark 变量使用 broadcast、accumulator

    broadcast 官方文档描述: Broadcast a read-only variable to the cluster, returning a [[org.apache.spark.broa ...

随机推荐

  1. 在设置了android:parentActivityName后,点击子Activity返回键,父Activity总会调用OnDestroy()的解决方式

    近期查了非常久这个事情.分享给大家, 原理非常easy,一个Activity在manifet里声明了android:parentActivityName:这时候通过Activity左上角的返回butt ...

  2. cocos2dx--vs2012+lua开发环境搭建

    cocos2dx版本号:cocos2dx2.2.3 lua插件:babelua 1.5.3  下载地址:http://pan.baidu.com/s/1i3mPD8h 第一步:先关闭vs,双击下载下来 ...

  3. (转)Oracle EXP-00091解决方法

    转自:http://blog.csdn.net/dracotianlong/article/details/8270136 EXP-: 正在导出有问题的统计信息. . . 正在导出表 WF_GENER ...

  4. C# 泛型特化

    C# 泛型不是 C++ 的模板类,并不支持特化和偏特化,但是使用一些技巧可以在一定程度上达到相同的目的. 原文是 po 在 stackoverflow 上的一个回答:A: Generic indexe ...

  5. 微信小程序实现运动步数排行(可删除)

    wxml <!-- 向左滑动删除功能 --> <view class="item-box"> <view class="items" ...

  6. Android中ImageView.ScaleType属性值

    1 android:scaleType="center" (1)当图片大于ImageView的宽高:以图片的中心点和ImageView的中心点为基准,按照图片的原大小居中显示,不缩 ...

  7. ZOJ 1108 FatMouse's Speed (HDU 1160) DP

    传送门: ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=108 HDU :http://acm.hdu.edu.cn/s ...

  8. 编程算法 - 远征队(expedition) 代码(C)

    远征队(expedition) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 远征队有一辆卡车须要行驶L单位的距离, 開始时, 车上有P单位的 ...

  9. google analytics是什么(免费的网站流量分析服务:比如分析有多少个人来了你的网站,告诉你怎么样才能在网站上面实现最大收益。)

    google analytics是什么(免费的网站流量分析服务:比如分析有多少个人来了你的网站,告诉你怎么样才能在网站上面实现最大收益.) 一.总结 免费的网站流量分析服务:比如分析有多少个人来了你的 ...

  10. [React] Update Component State in React With Ramda Lenses

    In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. ...