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. 【 Codeforces Round #430 (Div. 2) A 】 Kirill And The Game

    [链接]点击打开链接 [题意] 水题 [题解] 枚举b从x..y看看k*i是不是在l..r之间就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <cstdio> ...

  2. Spring HandlerInterceptor的使用

    http://blog.csdn.net/joeyon1985/article/details/49903761

  3. 硬件——nrf51822第二篇,如何设置keil用来下载程序

    转自电子发烧友论坛 未完,待续...... 这里就是根据自己的项目了,并不一定是按照下面的图片去做.

  4. element ui源码解析 -- button篇

    要看源码就得从最简单的开始,button够简单的了,就从他开始吧. 安装依赖后源码目录在:node_modules/element-ui/packages中,可以看到这里的文件夹命名是不是很熟悉,就是 ...

  5. mysql :Native table 'performance_schema'.'cond_instances' has the wrong structure

    err: 150418 13:25:06 [ERROR] Native table 'performance_schema'.'cond_instances' has the wrong struct ...

  6. windows下, nginx 提示错误 "No input file specified"

    https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80386035

  7. 使用前端后台管理模板库admin-lte(转)

    使用前端后台管理模板库admin-lte 使用前端后台管理模板库admin-lte 安装 搭建环境 安装 安装admin-lte,可以通过以下几种办法安装,下图是GitHub中admin-lte的主页 ...

  8. linux下查看动态链接库依赖关系的命令 x86: ldd *.so arm: arm-linux-readelf -d *.so 实际例子: 以项目中用到的库librtsp.so分析: lijun@ubuntu:~/workspace$ arm-hisiv100nptl-linux-ld -d librtsp.so arm-hisiv100nptl-linux-ld:

    linux下查看动态链接库依赖关系的命令 x86:ldd    *.so arm:arm-linux-readelf    -d    *.so 实际例子:以项目中用到的库librtsp.so分析:l ...

  9. 如何调试Javascript代码

    转自原文如何调试Javascript代码 目前,常用的浏览器IE.Chrome.Firefox都有相应的脚本调试功能.作为我们.NET 阵营,学会如何在IE中调试JS就足够了,在掌握了IE中的调试方法 ...

  10. [Recompose] Transform Props using Recompose --mapProps

    Learn how to use the 'mapProps' higher-order component to modify an existing component’s API (its pr ...