Spark中算子aggregate的用法总结

  • 官方解释

/**
   * Aggregate the elements of each partition, and then the results for all the partitions, using
   * given combine functions and a neutral "zero value". This function can return a different result
   * type, U, than the type of this RDD, T. Thus, we need one operation for merging a T into an U
   * and one operation for merging two U's, as in scala.TraversableOnce. Both of these functions are
   * allowed to modify and return their first argument instead of creating a new U to avoid memory
   * allocation.
   *
   * @param zeroValue the initial value for the accumulated result of each partition for the
   *                  `seqOp` operator, and also the initial value for the combine results from
   *                  different partitions for the `combOp` operator - this will typically be the
   *                  neutral element (e.g. `Nil` for list concatenation or `0` for summation)
   * @param seqOp an operator used to accumulate results within a partition
   * @param combOp an associative operator used to combine results from different partitions
   */
  def aggregate[U: ClassTag](zeroValue: U)(seqOp: (U, T) => U, combOp: (U, U) => U): U = withScope {...}
  • 翻译成白话

aggregate首先合并RDD中每个partition中的元素,每个partition得到一个结果,然后将所有partition得到的结果再做一次最终合并,得到最终的结果。aggregate方法第一个参数称之为初始值,官方解释,初始值参与每个partition中元素的合并,并参与所有partition的最终合并。

  • 举例说明

Talk is cheap. Show me the code.

  • 举例一
val rdd1 = sc.parallelize(List("12", "23", "345", "4567"), 2)//指定是2个partition
val result = rdd1.aggregate("")((x, y) => math.max(x.length, y.length).toString, (x, y) => x + y) //24

分析:
1.假设2个partition分别为p0和p1,元素“12”、“23”在p0中,“345”、“4567”在p1中(可以使用mapPartitionsWithIndex方法查看,Spark有对应算法决定去往哪个partition的元素是均衡的)
2.在p0中执行
初始值参与运算

<伪代码>
math.max("".length,"12".length).toString()   //结果是"2"
math.max("2".length,"23".length).toString()   //结果是"2",p0最终结果是"2"

3.在p1中执行
初始值参与运算

<伪代码>
math.max("".length,"345".length).toString()   //结果是"3"
math.max("3".length,"4567".length).toString()   //结果是"4",p1最终结果是"4"

4、将p0和p1结果汇总
初始值参与运算

<伪代码>
""+"2"="2"
"2"+"4"="24"

5、注意一点:最终结果可能不一样
p0和p1最终结果返回可能存在顺序不同,所以上边的结果还有一种情况可能是"42"

  • 举例二
val rdd1 = sc.parallelize(List("12", "23", "345", "4567"), 2)//指定是2个partition
val result = rdd1.aggregate("12")((x, y) => math.min(x.length, y.length).toString, (x, y) => x + y)//1211

分析:
1.假设2个partition分别为p0和p1,元素“12”、“23”在p0中,“345”、“4567”在p1中(可以使用mapPartitionsWithIndex方法查看,Spark有对应算法决定去往哪个partition的元素是均衡的)
2.在p0中执行
初始值参与运算

<伪代码>
math.min("12".length,"12".length).toString()   //结果是"2"
math.min("2".length,"23".length).toString()   //结果是"1",p0最终结果是"1"

3.在p1中执行
初始值参与运算

<伪代码>
math.min("12".length,"345".length).toString()   //结果是"2"
math.min("2".length,"4567".length).toString()   //结果是"1",p1最终结果是"1"

4、将p0和p1结果汇总
初始值参与运算

<伪代码>
"12"+"1"="121"
"121"+"1"="1211"
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎。Spark是UC Berkeley AM...
    大佛爱读书阅读 7,808评论 0 20
  • --- layout: post title: "如果有人问你关系型数据库的原理,叫他看这篇文章(转)" date...
    蓝坠星阅读 4,262评论 0 3
  • ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。 O...
    我想起个好名字阅读 10,791评论 0 9
  • ORA-13000: 维数超出范围 ORA-13001: 维数不匹配错误 ORA-13002: 指定的级别超出范围...
    thinkact阅读 19,691评论 1 5
  • 指针是C语言中广泛使用的一种数据类型。 运用指针编程是C语言最主要的风格之一。利用指针变量可以表示各种数据结构; ...
    朱森阅读 8,803评论 3 44