spark accumulator累加器
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累加器的更多相关文章
- spark.Accumulator
scala> val accum = sc.accumulator() accum: org.apache.spark.Accumulator[Int] = scala> sc.paral ...
- Spark RDD概念学习系列之rdd持久化、广播、累加器(十八)
1.rdd持久化 2.广播 3.累加器 1.rdd持久化 通过spark-shell,可以快速的验证我们的想法和操作! 启动hdfs集群 spark@SparkSingleNode:/usr/loca ...
- 【Spark篇】---Spark中广播变量和累加器
一.前述 Spark中因为算子中的真正逻辑是发送到Executor中去运行的,所以当Executor中需要引用外部变量时,需要使用广播变量. 累机器相当于统筹大变量,常用于计数,统计. 二.具体原理 ...
- Spark共享变量(广播变量、累加器)
转载自:https://blog.csdn.net/Android_xue/article/details/79780463 Spark两种共享变量:广播变量(broadcast variable)与 ...
- 【Spark Java API】broadcast、accumulator
转载自:http://www.jianshu.com/p/082ef79c63c1 broadcast 官方文档描述: Broadcast a read-only variable to the cl ...
- Spark累加器
spark累计器 因为task的执行是在多个Executor中执行,所以会出现计算总量的时候,每个Executor只会计算部分数据,不能全局计算. 累计器是可以实现在全局中进行累加计数. 注意: 累加 ...
- pyspark中使用累加器Accumulator统计指标
评价分类模型的性能时需要用到以下四个指标 最开始使用以下代码计算,发现代码需要跑近一个小时,而且这一个小时都花在这四行代码上 # evaluate model TP = labelAndPreds.f ...
- spark累加器、广播变量
一言以蔽之: 累加器就是只写变量 通常就是做事件统计用的 因为rdd是在不同的excutor去执行的 你在不同excutor中累加的结果 没办法汇总到一起 这个时候就需要累加器来帮忙完成 广播变量是只 ...
- spark 变量使用 broadcast、accumulator
broadcast 官方文档描述: Broadcast a read-only variable to the cluster, returning a [[org.apache.spark.broa ...
随机推荐
- [Ramda] Simple log function for debugging Compose function / Using R.tap for logging
const log = function(x){ console.log(x); return x; } const get = R.curry(function(prop, obj){ return ...
- HttpWatch--简介及使用技巧
一 概述: HttpWatch强大的网页数据分析工具.集成在Internet Explorer工具栏.包括网页摘要.Cookies管理.缓存管理.消息头发送/接受.字符查询.POST 数据和目录管理功 ...
- IDEACould not autowire. No beans of 'xxxMapper' type found.
作为一名刚开始使用idea的新手,最近在使用maven+springMVC框架时遇到了这样一个问题:Could not autowire. No beans of 'xxxMapper' type f ...
- (嵌入式开发)自己写bootloader之编写第一阶段
最简单的bootloader的编写步骤: 1. 初始化硬件:关看门狗.设置时钟.设置SDRAM.初始化NAND FLASH 2. 如果bootloader比较大,要把它重定位到SDRAM 3. 把内核 ...
- VSX(翻译)Moving Code Blocks Among Code Regions using VS 2010 Extensions
Moving Code Blocks Among Code Regions using VS 2010 Extensions (翻译)使用VS 2010 扩展性将代码块移至Region区域中 Down ...
- eclipse编译器错误、警告设置
颜色配置步骤:Window->Preferences->General->Editors->Text Editors->Annotations
- GCD下载后清除缓存
//GCD下载后清除缓存1 —(void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; //清除缓存 [self.cache re ...
- 又在折腾cygwin
apt-cyg https://github.com/transcode-open/apt-cyg/blob/master/README.md cygwin 163镜像 http://mirrors. ...
- 26、驱动调试之根据oops信息和堆栈确定出错的代码
a.驱动作为模块:1. 根据pc值确定该指令属于内核还是外加的模块pc=0xbf000018 它属于什么的地址?是内核还是通过insmod加载的驱动程序?先判断是否属于内核的地址: 看System.m ...
- Android shape画圆点
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...