原文链接-https://www.cnblogs.com/0xcafedaddy/p/7625358.html

先来看一下在PairRDDFunctions.scala文件中reduceByKey和groupByKey的源码

/**
* Merge the values for each key using an associative reduce function. This will also perform
* the merging locally on each mapper before sending results to a reducer, similarly to a
* "combiner" in MapReduce. Output will be hash-partitioned with the existing partitioner/
* parallelism level.
*/
def reduceByKey(func: (V, V) => V): RDD[(K, V)] = {
reduceByKey(defaultPartitioner(self), func)
} /**
* Group the values for each key in the RDD into a single sequence. Allows controlling the
* partitioning of the resulting key-value pair RDD by passing a Partitioner.
* The ordering of elements within each group is not guaranteed, and may even differ
* each time the resulting RDD is evaluated.
*
* Note: This operation may be very expensive. If you are grouping in order to perform an
* aggregation (such as a sum or average) over each key, using [[PairRDDFunctions.aggregateByKey]]
* or [[PairRDDFunctions.reduceByKey]] will provide much better performance.
*
* Note: As currently implemented, groupByKey must be able to hold all the key-value pairs for any
* key in memory. If a key has too many values, it can result in an [[OutOfMemoryError]].
*/
def groupByKey(partitioner: Partitioner): RDD[(K, Iterable[V])] = {
// groupByKey shouldn't use map side combine because map side combine does not
// reduce the amount of data shuffled and requires all map side data be inserted
// into a hash table, leading to more objects in the old gen.
val createCombiner = (v: V) => CompactBuffer(v)
val mergeValue = (buf: CompactBuffer[V], v: V) => buf += v
val mergeCombiners = (c1: CompactBuffer[V], c2: CompactBuffer[V]) => c1 ++= c2
val bufs = combineByKey[CompactBuffer[V]](
createCombiner, mergeValue, mergeCombiners, partitioner, mapSideCombine=false)
bufs.asInstanceOf[RDD[(K, Iterable[V])]]
}

通过源码可以发现:

reduceByKey:reduceByKey会在结果发送至reducer之前会对每个mapper在本地进行merge,有点类似于在MapReduce中的combiner。这样做的好处在于,在map端进行一次reduce之后,数据量会大幅度减小,从而减小传输,保证reduce端能够更快的进行结果计算。

groupByKey:groupByKey会对每一个RDD中的value值进行聚合形成一个序列(Iterator),此操作发生在reduce端,所以势必会将所有的数据通过网络进行传输,造成不必要的浪费。同时如果数据量十分大,可能还会造成OutOfMemoryError。

通过以上对比可以发现在进行大量数据的reduce操作时候建议使用reduceByKey。不仅可以提高速度,还是可以防止使用groupByKey造成的内存溢出问题。

转载-reduceByKey和groupByKey的区别的更多相关文章

  1. reduceByKey和groupByKey的区别

    先来看一下在PairRDDFunctions.scala文件中reduceByKey和groupByKey的源码 /** * Merge the values for each key using a ...

  2. spark:reducebykey与groupbykey的区别

    从源码看: reduceBykey与groupbykey: 都调用函数combineByKeyWithClassTag[V]((v: V) => v, func, func, partition ...

  3. reduceByKey和groupByKey区别与用法

    在spark中,我们知道一切的操作都是基于RDD的.在使用中,RDD有一种非常特殊也是非常实用的format——pair RDD,即RDD的每一行是(key, value)的格式.这种格式很像Pyth ...

  4. 【spark】常用转换操作:reduceByKey和groupByKey

    1.reduceByKey(func) 功能: 使用 func 函数合并具有相同键的值. 示例: val list = List("hadoop","spark" ...

  5. spark RDD,reduceByKey vs groupByKey

    Spark中有两个类似的api,分别是reduceByKey和groupByKey.这两个的功能类似,但底层实现却有些不同,那么为什么要这样设计呢?我们来从源码的角度分析一下. 先看两者的调用顺序(都 ...

  6. 转载>>C# Invoke和BeginInvoke区别和使用场景

    转载>>C# Invoke和BeginInvoke区别和使用场景 一.为什么Control类提供了Invoke和BeginInvoke机制? 关于这个问题的最主要的原因已经是dotnet程 ...

  7. 【Spark算子】:reduceByKey、groupByKey和combineByKey

    在spark中,reduceByKey.groupByKey和combineByKey这三种算子用的较多,结合使用过程中的体会简单总结: 我的代码实践:https://github.com/wwcom ...

  8. spark新能优化之reduceBykey和groupBykey的使用

    val counts = pairs.reduceByKey(_ + _) val counts = pairs.groupByKey().map(wordCounts => (wordCoun ...

  9. 【转载】strlen与sizeof区别

    自己小结: sizeof使用时,若是数组变量,则是数组变量占的大小 char a[10]; sizeof(a)=10 若是指针,则为指针大小,数组变量作为函数参数传递时,会退化成指针,且函数内是不知道 ...

随机推荐

  1. VSCode删除重复的空行

    输入^\s\n 选择使用正则表达式

  2. 【洛谷P1601 A+B Problem(高精)】

    题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b] 输入输出格式 输入格式: 分两行输入a,b<=10^500 ...

  3. SpringCloud-初识

    说道SpringCloud,原来就去了解过,也有很大兴趣,只是当初不知道这是个什么东西.在它之前,我学习Spring,在官网肆无忌惮的逛的时候,发现了SpringBoot,那个时候就打算开始学习Spr ...

  4. Elastic 安装篇(1)

    1.Elasticsearch下载安装 https://www.elastic.co/cn/downloads/elasticsearch 解压: 2.安装head https://github.co ...

  5. 关于处理iis8.0中设置Request.BinaryRead 不允许操作的解决方法

    iis6.0解决方案: 起初我刚开始上传的是小文件运行都是正常的,后来我弄个文件大点的上传看程序运行怎么样?就上面的问题,在网上搜索正好找到跟我一样的问题,拿过来自己记录下.其中行62指的是:oUpF ...

  6. Windows下MySQL下载安装、配置与使用

    用过MySQL之后,不论容量的话,发现比其他两个(sql server .oracle)好用的多,一下子就喜欢上了.下面给那些还不知道怎么弄的童鞋们写下具体的方法步骤. (我这个写得有点太详细了,甚至 ...

  7. 跨域、curl、snoopy、file_get_contents()

    定义:可以称为”信息采集/模拟登录”技术,可以实现对某个地址做请求,同时按照要求传递get或post参数. curl本身是php的一个扩展,同时也是一个利用URL语法规定来传输文件和数据的工具,支持很 ...

  8. DBMS客户端是否安装:Make sure DBMS client is installed and this required library is available for dynamic loading

    Symptom The full error message is as follows:Error logging in.  Unable to process the database trans ...

  9. flask 安装及基础学习(url_for反转,静态文件引入)

    pip3 install flask pycharm 创建项目 默认的代码解释说明(及开启debug模式) #encoding:utf-8 from flask import Flask #从flas ...

  10. Kubernetes基础概念及架构概述

    Kubernetes 架构 Kubernetes是一个全新的基于容器技术的分布式架构,虽然Kubernetes只有三年,但它是谷歌十几年以来大规模应用容器技术的经验积累和升华的一个重要发展成果.确切的 ...