aggregateByKey 这个RDD有点繁琐,整理一下使用示例,供参考

直接上代码

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkContext, SparkConf} /**
* Created by Edward on 2016/10/27.
*/
object AggregateByKey {
def main(args: Array[String]) {
val sparkConf: SparkConf = new SparkConf().setAppName("AggregateByKey")
.setMaster("local")
val sc: SparkContext = new SparkContext(sparkConf) val data = List((1, 3), (1, 2), (1, 4), (2, 3))
var rdd = sc.parallelize(data,2)//数据拆分成两个分区 //合并在不同partition中的值,a,b的数据类型为zeroValue的数据类型
def comb(a: String, b: String): String = {
println("comb: " + a + "\t " + b)
a + b
}
//合并在同一个partition中的值, a的数据类型为zeroValue的数据类型,b的数据类型为原value的数据类型
def seq(a: String, b: Int): String = {
println("seq: " + a + "\t " + b)
a + b
} rdd.foreach(println)

//zeroValue 中立值,定义返回value的类型,并参与运算
//seqOp 用来在一个partition中合并值的
//comb 用来在不同partition中合并值的
val aggregateByKeyRDD: RDD[(Int, String)] = rdd.aggregateByKey("100")(seq,comb) //打印输出
aggregateByKeyRDD.foreach(println) sc.stop()
}
}

输出结果说明:

 /*
将数据拆分成两个分区 //分区一数据
(1,3)
(1,2)
//分区二数据
(1,4)
(2,3) //分区一相同key的数据进行合并
seq: 100 3 //(1,3)开始和中立值进行合并 合并结果为 1003
seq: 1003 2 //(1,2)再次合并 结果为 10032 //分区二相同key的数据进行合并
seq: 100 4 //(1,4) 开始和中立值进行合并 1004
seq: 100 3 //(2,3) 开始和中立值进行合并 1003 将两个分区的结果进行合并
//key为2的,只在一个分区存在,不需要合并 (2,1003)
(2,1003) //key为1的, 在两个分区存在,并且数据类型一致,合并
comb: 10032 1004
(1,100321004) * */

参考代码及下面的说明进行理解

官网的说明

aggregateByKey(zeroValue)(seqOpcombOp, [numTasks]) When called on a dataset of (K, V) pairs, returns a dataset of (K, U) pairs where the values for each key are aggregated using the given combine functions and a neutral "zero" value. Allows an aggregated value type that is different than the input value type, while avoiding unnecessary allocations. Like in groupByKey, the number of reduce tasks is configurable through an optional second argument.

源码中函数的说明

/**
* Aggregate the values of each key, using given combine functions and a neutral "zero value".
* This function can return a different result type, U, than the type of the values in this RDD,
* V. Thus, we need one operation for merging a V into a U and one operation for merging two U's,
* as in scala.TraversableOnce. The former operation is used for merging values within a
* partition, and the latter is used for merging values between partitions. To avoid memory
* allocation, both of these functions are allowed to modify and return their first argument
* instead of creating a new U.
*/

Spark RDD aggregateByKey的更多相关文章

  1. Spark RDD 操作

    1. Spark RDD 创建操作 1.1 数据集合   parallelize 可以创建一个能够并行操作的RDD.其函数定义如下: ) scala> sc.defaultParallelism ...

  2. Spark RDD Transformation 简单用例(二)

    aggregateByKey(zeroValue)(seqOp, combOp, [numTasks]) aggregateByKey(zeroValue)(seqOp, combOp, [numTa ...

  3. Spark RDD Transformation 简单用例(一)

    map(func) /** * Return a new RDD by applying a function to all elements of this RDD. */ def map[U: C ...

  4. spark RDD官网RDD编程指南

    http://spark.apache.org/docs/latest/rdd-programming-guide.html#using-the-shell Overview(概述) 在较高的层次上, ...

  5. spark学习13(spark RDD)

    RDD及其特点 1)RDD(Resillient Distributed Dataset)弹性分布式数据集,是spark提供的核心抽象.它代表一个不可变.可分区.里面的元素可并行计算的集合 2)RDD ...

  6. Spark RDD :Spark API--Spark RDD

    一.RDD的概述 1.1 什么是RDD? RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素 ...

  7. Spark RDD 算子总结

    Spark算子总结 算子分类 Transformation(转换) 转换算子 含义 map(func) 返回一个新的RDD,该RDD由每一个输入元素经过func函数转换后组成 filter(func) ...

  8. Spark—RDD编程常用转换算子代码实例

    Spark-RDD编程常用转换算子代码实例 Spark rdd 常用 Transformation 实例: 1.def map[U: ClassTag](f: T => U): RDD[U]  ...

  9. Spark Rdd coalesce()方法和repartition()方法

    在Spark的Rdd中,Rdd是分区的. 有时候需要重新设置Rdd的分区数量,比如Rdd的分区中,Rdd分区比较多,但是每个Rdd的数据量比较小,需要设置一个比较合理的分区.或者需要把Rdd的分区数量 ...

随机推荐

  1. SVN Cornerstone 报错信息 xcodeproj cannot be opened because the project file cannot be parsed.

    svn点击update 之后,打开xcode工程文件,会出现  xxx..xcodeproj  cannot be opened becausethe project file cannot be p ...

  2. CentOS7.2部署OpenStack(一)—环境准备

    1.系统环境 # uname -r 3.10.0-327.el7.x86_64 # cat /etc/redhat-release CentOS Linux release 7.2.1511 (Cor ...

  3. Microsoft Azure 01 (Summarize)

    What is Windows Azure? As a cloud platform from Microsoft that provides a wide range of different se ...

  4. python基础知识---迭代器、生成器、装饰器

    一.迭代器 二.生成器 http://www.cnblogs.com/huxi/archive/2011/07/14/2106863.html def func(): #定义生成器,和普通函数的区别是 ...

  5. 扩展jquery的选择器

    I’m sure you all know that it’s possible to create plugins and extend various aspects of the jQuery ...

  6. python基础学习笔记3

    特殊方法与多范式   Python一切皆对象,但同时,Python还是一个多范式语言(multi-paradigm),你不仅可以使用面向对象的方式来编写程序,还可以用面向过程的方式来编写相同功能的程序 ...

  7. 代理服务器(Proxy)原理

    17.1 什么是代理服务器(Proxy)   以类似代理人的身份去取得用户所需要的数据就是了! 但是由于它的『代理』能力,使得我们可以透过代理服务器来达成防火墙功能与用户浏览数据的分析!   此外,也 ...

  8. Parquet与ORC:高性能列式存储格式(收藏)

    背景 随着大数据时代的到来,越来越多的数据流向了Hadoop生态圈,同时对于能够快速的从TB甚至PB级别的数据中获取有价值的数据对于一个产品和公司来说更加重要,在Hadoop生态圈的快速发展过程中,涌 ...

  9. mobx源码解读1

    mobx是redux的代替品,其本身就是一个很好的MVVM框架.因此花点力气研究一下它. 网上下最新的2.75 function Todo() { this.id = Math.random() mo ...

  10. php查看网页源代码的方法

    这篇文章主要介绍了php查看网页源代码的方法,涉及php读取网页文件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了php查看网页源代码的方法.分享给大家供大家参考.具体实现 ...