该函数官方的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. github page使用

    github page介绍: https://help.github.com/categories/20/articles 使用GitHub Pages建立博客 与GitHub建立好链接之后,就可以方 ...

  2. 转:使用C#的HttpWebRequest模拟登陆网站

    这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话的CooKie 并根据该CooKie继续访问登录后的页面,获取后续访问的页面数据. ...

  3. 三 ICE开发初级研究

    http://www.acejoy.com/bbs/viewthread.php?tid=2878&extra=page%3D1 ICE开发初级研究(一) 最近一段一直在忙于工作,事情比较多, ...

  4. code 代码分析 及其解决方案

    官网地址:http://msdn.microsoft.com/zh-cn/library/ms182135.aspx [FxCop.设计规则]11. 不应该使用默认参数 参考地址:http://blo ...

  5. SQL Server 两种判断表名是否存在且删除的方式

      邓老师(老邓)教的 if exists(select * from sysobjects where name='Table_88') drop table Table_88  偷的((*^__^ ...

  6. jquery方法详解

    jquery方法详解 http://www.365mini.com/doc

  7. 深入浅出SlidingMenu

    如果想直接查看源码的话可以从我的Github上下载查看:https://github.com/zhanghuijun0/demo-for-android/tree/master/SlidingMenu ...

  8. A child container failed during start 解决方案

    症状:A child container failed during start 适用问题描述:tomcat挂空可以正常运行,载入项目时挂掉. 相关操作:之前为了省事,由于两个servlet功能类似所 ...

  9. Spring--------web应用中保存spring容器

    ---恢复内容开始--- 问题:在一个web应用中我使用了spring框架,但有一部分模块或组件并没有托管给Spring,比如有的可能是一个webservice服务类,如果我想在这些非托管的类里使用托 ...

  10. hdu2317Nasty Hacks

    Problem Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of mali ...