Spark源码学习笔记(随笔)-groupByKey()是宽依赖吗
来源:互联网 发布:淘宝拍图技巧 编辑:程序博客网 时间:2024/05/29 03:22
最近从源码角度温习之前学的Spark的基础,在RDD的Dependency这一节中,关于一些Transition操作是Narrow Dependency还是Shuffle Dependency。
对于map/filter等操作我们能很清晰的知道它是窄依赖,对于一些复杂的或者不是那么明确的转换操作就不太能区分是什么依赖,如groupByKey()。较多博客直接说这个转换操作是宽依赖,真的是宽依赖吗?
我们看看源码:
def groupByKey(): RDD[(K, Iterable[V])] = self.withScope { groupByKey(defaultPartitioner(self)) }
取默认分区方式(该RDD如有分区方式则使用该分区方式)作为参数调用另一个带参数的groupByKey:
def groupByKey(partitioner: Partitioner): RDD[(K, Iterable[V])] = self.withScope { // 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 = combineByKeyWithClassTag[CompactBuffer[V]]( createCombiner, mergeValue, mergeCombiners, partitioner, mapSideCombine = false) bufs.asInstanceOf[RDD[(K, Iterable[V])]] }
最后调用了函数combineByKeyWithClassTag,看看这个函数:
def combineByKeyWithClassTag[C]( createCombiner: V => C, mergeValue: (C, V) => C, mergeCombiners: (C, C) => C, partitioner: Partitioner, mapSideCombine: Boolean = true, serializer: Serializer = null)(implicit ct: ClassTag[C]): RDD[(K, C)] = self.withScope { require(mergeCombiners != null, "mergeCombiners must be defined") // required as of Spark 0.9.0 if (keyClass.isArray) { if (mapSideCombine) { throw new SparkException("Cannot use map-side combining with array keys.") } if (partitioner.isInstanceOf[HashPartitioner]) { throw new SparkException("HashPartitioner cannot partition array keys.") } } val aggregator = new Aggregator[K, V, C]( self.context.clean(createCombiner), self.context.clean(mergeValue), self.context.clean(mergeCombiners)) if (self.partitioner == Some(partitioner)) { self.mapPartitions(iter => { val context = TaskContext.get() new InterruptibleIterator(context, aggregator.combineValuesByKey(iter, context)) }, preservesPartitioning = true) } else { new ShuffledRDD[K, V, C](self, partitioner) .setSerializer(serializer) .setAggregator(aggregator) .setMapSideCombine(mapSideCombine) } }
上述函数中可以看到,如果该RDD的分区方式与参数中的分区方式相同,则调用mapPartitions函数,该函数生成MapPartitionsRDD,为窄依赖。分区方式不同,才生成ShuffledRDD,为宽依赖。
groupByKey还有另外两个函数:groupByKey(numPartitions: Int)和groupByKey(partitioner: Partitioner)。这两个函数会有新的分区方式。
因此,groupByKey()应该不一定是宽依赖吧!
阅读全文
0 0
- Spark源码学习笔记(随笔)-groupByKey()是宽依赖吗
- 为什么groupbykey是宽依赖
- Spark学习笔记 --- Spark中宽依赖(Wide deps)与窄依赖(Narrow deps)
- spark学习-21-Spark的groupByKey
- Spark源码之reduceByKey与GroupByKey
- spark学习笔记之二:宽依赖和窄依赖
- spark源码学习笔记
- jquery源码学习笔记随笔
- Spark算子[12]:groupByKey、cogroup、join、lookup 源码实例详解
- Spark学习笔记(22)Spark Streaming架构源码图解
- Spark源码编译---Spark学习笔记1
- [spark]groupbykey reducebykey
- spark groupByKey操作
- spark--transform算子--groupByKey
- spark源码阅读笔记RDD(六) RDD的依赖关系
- spark【例子】同类合并、计算(主要使用groupByKey)
- spark【例子】同类合并、计算(主要使用groupByKey)
- spark【例子】同类合并、计算(主要使用groupByKey)
- android控件03---EditText
- 开发者所需知道的iOS11 SDK新特性
- [LeetCode]Arithmetic Slices
- 一个GISER 6.7的祝福
- awk(一)
- Spark源码学习笔记(随笔)-groupByKey()是宽依赖吗
- win7系统重启后ip地址丢失怎么办
- 究竟啥才是互联网架构“高并发”
- 许巍—执着
- 关于IE不兼容和 url转码解码总结
- 闪回数据归档+闪回数据归档区+创建闪回数据归档区+创建闪回数据归档区案例+为数据归档区添加表空间+为数据归档区删除表空间+数据归档区修改数据保留时间+删除数据归档区
- JDBC连接不同数据库的写法
- 从零开始学习音视频编程技术(五) 使用FFMPEG解码视频之保存成图片
- LinearLayout源码简单查看