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 #450 (Div. 2) A】Find Extra One

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟. 看看Y左边或右边的点个数是否<=1 [代码] #include <bits/stdc++.h> using ...

  2. 关于http请求指定本地ip

    static void Main(string[] args) { //ssl证书验证问题(没有校验) ServicePointManager.ServerCertificateValidationC ...

  3. angular material dialog应用

    1. 打开弹窗的点击事件 project.component.html <button mat-icon-button class="action-button" (clic ...

  4. 自定义npm包——typeScript版本

    前言 这篇文章是在我之前的文章 [自定义npm包的创建.发布.更新和撤销] 的基础上做的扩展,主要是针对如何创建以及发布一个typeScript语言的npm包. 大纲 1.创建关于typeScript ...

  5. SQL基础总结——20150730

           SQL SQL 指结构化查询语言 SQL 使我们有能力訪问数据库 SQL 是一种 ANSI(美国国家标准化组织) 的标准计算机语言 SQL 是一门 ANSI 的标准计算机语言.用来訪问和 ...

  6. hdu3360National Treasures (最大匹配,拆点法)

    National Treasures Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  7. 【Debug】— C++ 表达式必须包含类类型

    错误一般发生在使用.进行访问时,原因可能在于: 你以为你定义了一个类对象,其实你是声明了一个函数,在编译器看来: 对类对象指针采用.的方式访问其成员变量: 也包括基本类型变量,错误地使用. int a ...

  8. 【29.42%】【POJ 1182】食物链

    Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64875 Accepted: 19085 Description 动物王国中有三 ...

  9. [CSS] Draw Simple Icons with CSS

    Using pseudo-elements like ::before and ::after we can draw some simple icons without having using i ...

  10. [Webpack] Configure Prepack with Webpack

    Great improvements and optimizations can be made to the output of bundled code. Prepack provides the ...