该函数官方的api,说的不是很明白:

aggregate(zeroValueseqOpcombOp)

Aggregate the elements of each partition, and then the results for all the partitions, using a given combine functions and a neutral “zero value.”

The functions op(t1, t2) is allowed to modify t1 and return it as its result value to avoid object allocation; however, it should not modify t2.

The first function (seqOp) can return a different result type, U, than the type of this RDD. Thus, we need one operation for merging a T into an U and one operation for merging two U

>>> seqOp=(lambdax,y:(x[0]+y,x[1]+1))

>>> combOp=(lambdax,y:(x[0]+y[0],x[1]+y[1]))

>>> sc.parallelize([1,2,3,4]).aggregate((0,0),seqOp,combOp)

(10, 4)

>>> sc.parallelize([]).aggregate((0,0),seqOp,combOp)

(0, 0)

下面列出,代码的执行流程:

假设[1,2,3,4]被分成两个分区,为 分区1([1,2]),分区2([3,4])

首先用seqOp对分区1进行操作:

x=(0,0)   y=1    ----->   (1,1)   #对分区进行第一次seqOp操作时,x为zero value

x=(1,1)   y=2    ----->   (3,2)   #对分区进行的第二次及以后的seqOp操作,x为前一次seqOp的执行结果

同样对分区2进行操作:

x=(0,0)   y=3    ----->   (3,1)

x=(3,1)   y=4    ----->   (7,2)

然后用combOp对两个分区seqOp作用后的结果进行操作:

分区1:

x=(0,0)   y=(3,2)   ------> (3,2)  #对第一个分区进行combOp操作时,x为zero value

x=(3,2)   y=(7,2)   ------> (10,4) #对第二个及以后分区进行combOp操作时,x为前一分区combOp处理后的结果

可以看出,例子实际上即 (rdd.sum(),rdd.count())

spark aggregate的更多相关文章

  1. spark aggregate算子

    spark aggregate源代码 /** * Aggregate the elements of each partition, and then the results for all the ...

  2. spark aggregate函数详解

    aggregate算是spark中比较常用的一个函数,理解起来会比较费劲一些,现在通过几个详细的例子带大家来着重理解一下aggregate的用法. 1.先看看aggregate的函数签名在spark的 ...

  3. spark aggregate函数

    aggregate函数将每个分区里面的元素进行聚合,然后用combine函数将每个分区的结果和初始值(zeroValue)进行combine操作.这个函数最终返回的类型不需要和RDD中元素类型一致. ...

  4. 转:Spark User Defined Aggregate Function (UDAF) using Java

    Sometimes the aggregate functions provided by Spark are not adequate, so Spark has a provision of ac ...

  5. 轻松理解 Spark 的 aggregate 方法

    2019-04-20 关键字: Spark 的 agrregate 作用.Scala 的 aggregate 是什么 Spark 编程中的 aggregate 方法还是比较常用的.本篇文章站在初学者的 ...

  6. Spark MLlib 之 aggregate和treeAggregate从原理到应用

    在阅读spark mllib源码的时候,发现一个出镜率很高的函数--aggregate和treeAggregate,比如matrix.columnSimilarities()中.为了好好理解这两个方法 ...

  7. Spark操作:Aggregate和AggregateByKey

    1. Aggregate Aggregate即聚合操作.直接上代码: import org.apache.spark.{SparkConf, SparkContext} object Aggregat ...

  8. Spark笔记之使用UDAF(User Defined Aggregate Function)

    一.UDAF简介 先解释一下什么是UDAF(User Defined Aggregate Function),即用户定义的聚合函数,聚合函数和普通函数的区别是什么呢,普通函数是接受一行输入产生一个输出 ...

  9. Spark RDD的fold和aggregate为什么是两个API?为什么不是一个foldLeft?

    欢迎关注我的新博客地址:http://cuipengfei.me/blog/2014/10/31/spark-fold-aggregate-why-not-foldleft/ 大家都知道Scala标准 ...

随机推荐

  1. CSS基础-引入方法,选择器,继承

    一.CSS引入方法:行内式.嵌入式.导入式.链接式. 1.行内式. 即:在标签的style属性中设定CSS样式. 例子:<div style="行内式</div> 2.嵌入 ...

  2. CentOS 6.3安装Nginx 搭建文件服务器

    转自:http://www.linuxidc.com/Linux/2012-09/70596.htm 1.配置CentOS 6.2 第三方yum源(CentOS默认的标准源里没有nginx软件包): ...

  3. RBF径向基神经网络——乳腺癌医学诊断建模

    案例描述 近年来疾病早期诊断越来越受到医学专家的重视,从而产生了各种疾病诊断的新方法.乳癌最早的表现是患乳出现单发的.无痛性并呈进行性生长的小肿块.肿块位于外上象限最多见,其次是乳头.乳晕区和内上象限 ...

  4. 【LeetCode练习题】Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...

  5. 关于 MVC 字段 默认值

    以下是网友的疑问: ================================================================ Html.EditorFor(model=> ...

  6. cenos 安装 phpredis 扩展

    1. php -m 可以查看 php 所有的已经安装的扩展

  7. 论文:network embedding

    KDD2016: network embedding model: deep walk(kdd 2014): http://videolectures.net/kdd2014_perozzi_deep ...

  8. iOS Get方式带中文不能请求网络

    今天发现一个蛋疼的问题,使用ASIHTTPRequest Get方式请求数据时候带中文,iOS客户端不能正确进行网络请求. NSURL *url = [NSURL URLWithString:@htt ...

  9. 序列变换(Lis变形)

    序列变换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  10. linux内存操作----kernel 3.5.X copy_from_user()和copy_to_user()

    前面的一篇文章中简单的描写叙述了一下内存映射的内容,http://blog.csdn.net/codectq/article/details/25658813,这篇文章作为用户把内存规划好之后,在用户 ...