该函数官方的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. Word Ladder 解答

    Question Given two words (beginWord and endWord), and a dictionary's word list, find the length of s ...

  2. 【UVA 11997 K Smallest Sums】优先级队列

    来自<训练指南>优先级队列的例题. 题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702 题意:给定 ...

  3. canvas.js | CLiPS

    canvas.js | CLiPS canvas.js The canvas.js module is a simple and robust JavaScript API for the HTML5 ...

  4. Skulpt

    Skulpt Python. Client side. Skulpt is an entirely in-browser implementation of Python. No preprocess ...

  5. m元素集合的n个元素子集

    理论: 假设有5个元素的集点,取出3个元素的可能子集如下:{1 2 3}.{1 2 4 }.{1 2 5}.{1 3 4}.{1 3 5}.{1 4 5}.{2 3 4}.{2 3 5}.{2 4 5 ...

  6. Error Correct System(模拟)

     Error Correct System Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  7. Linux 下mysql修改数据库存放目录方法和可能遇到的问题

    MySQL版本:5.6.23-enterprise-commercial-advanced ,使用rpm安装linux:Red Hat Enterprise Linux Server release ...

  8. session之退出登陆

    <span style="font-size:32px;">//使用SESSION必须先开启session session_start(); //彻底删除session ...

  9. Extjs 6 MVC开发模式(二)

    1.Extjs MVC开发模式 在JS的开发过程中,大规模的JS脚本难以组织和维护,这一直是困扰前端开发人员的头等问题.Extjs为了解决这种问题,在Extjs4.x版本中引入了MVC开发模式,开始将 ...

  10. java服务器简单实现

    一 HTTP http请求 一般一个http请求包括以下三个部分: 1 请求方法,如get,post 2 请求头 3 实体 一个http请求的实例如下:GET /index.jsp HTTP/1.1H ...