【Spark Java API】Transformation(4)—coalesce、repartition
来源:互联网 发布:python谷歌浏览器驱动 编辑:程序博客网 时间:2024/06/07 23:38
coalesce
官方文档描述:
Return a new RDD that is reduced into `numPartitions` partitions.
函数原型:
def coalesce(numPartitions: Int): JavaRDD[T]def coalesce(numPartitions: Int, shuffle: Boolean): JavaRDD[T]
源码分析:
def coalesce(numPartitions: Int, shuffle: Boolean = false)(implicit ord: Ordering[T] = null) : RDD[T] = withScope { if (shuffle) { /** Distributes elements evenly across output partitions, starting from a random partition. */ val distributePartition = (index: Int, items: Iterator[T]) => { var position = (new Random(index)).nextInt(numPartitions) items.map { t => // Note that the hash code of the key will just be the key itself. The HashPartitioner // will mod it with the number of total partitions. position = position + 1 (position, t) } } : Iterator[(Int, T)] // include a shuffle step so that our upstream tasks are still distributed new CoalescedRDD( new ShuffledRDD[Int, T, T](mapPartitionsWithIndex(distributePartition), new HashPartitioner(numPartitions)), numPartitions).values } else { new CoalescedRDD(this, numPartitions) }}
从源码中可以看出,当shuffle=false时,由于不进行shuffle,问题就变成parent RDD中哪些partition可以合并在一起,合并的过程依据设置的numPartitons中的元素个数进行合并处理。当shuffle=true时,进行shuffle操作,原理很简单,先是对partition中record进行k-v转换,其中key是由 (new Random(index)).nextInt(numPartitions)+1
计算得到,value为record,index 是该 partition 的索引,numPartitions 是 CoalescedRDD 中的 partition 个数,然后 shuffle 后得到 ShuffledRDD, 可以得到均分的 records,再经过复杂算法来建立 ShuffledRDD 和 CoalescedRDD 之间的数据联系,最后过滤掉 key,得到 coalesce 后的结果 MappedRDD。
实例:
List<Integer> data = Arrays.asList(1, 2, 4, 3, 5, 6, 7);JavaRDD<Integer> javaRDD = javaSparkContext.parallelize(data);// shuffle默认是falseJavaRDD<Integer> coalesceRDD = javaRDD.coalesce(2); System.out.println(coalesceRDD);JavaRDD<Integer> coalesceRDD1 = javaRDD.coalesce(2,true);System.out.println(coalesceRDD1);
注意:
coalesce() 可以将 parent RDD 的 partition 个数进行调整,比如从 5 个减少到 3 个,或者从 5 个增加到 10 个。需要注意的是当 shuffle = false 的时候,是不能增加 partition 个数的(即不能从 5 个变为 10 个)。
repartition
官网文档描述:
Return a new RDD that has exactly numPartitions partitions.Can increase or decrease the level of parallelism in this RDD. Internally, this uses a shuffle to redistribute data.If you are decreasing the number of partitions in this RDD, consider using `coalesce`,which can avoid performing a shuffle.
特别需要说明的是,如果使用repartition对RDD的partition数目进行缩减操作,可以使用coalesce函数,将shuffle设置为false,避免shuffle过程,提高效率。
函数原型:
def repartition(numPartitions: Int): JavaRDD[T]
源码分析:
def repartition(numPartitions: Int)(implicit ord: Ordering[T] = null): RDD[T] = withScope { coalesce(numPartitions, shuffle = true)}
从源码中可以看到repartition等价于 coalesce(numPartitions, shuffle = true)
实例:
List<Integer> data = Arrays.asList(1, 2, 4, 3, 5, 6, 7);JavaRDD<Integer> javaRDD = javaSparkContext.parallelize(data);//等价于 coalesce(numPartitions, shuffle = true)JavaRDD<Integer> repartitionRDD = javaRDD.repartition(2);System.out.println(repartitionRDD);
- 【Spark Java API】Transformation(4)—coalesce、repartition
- Spark Transformation —— coalesce
- Spark Transformation —— repartition算子
- spark coalesce和repartition区别
- Spark算子[02]:coalesce,repartition
- [Spark--基础]--repartition vs coalesce
- Spark重新分区—repartition和coalesce的用法
- Spark中repartition和coalesce的用法
- Spark中repartition和coalesce的用法
- Learning Spark 笔记(五) -- coalesce、repartition
- spark算子(repartition和coalesce)
- spark coalesce和repartition的区别
- Spark中repartition和coalesce的用法
- Spark Rdd coalesce()方法和repartition()
- Spark Rdd coalesce()方法和repartition()方法
- Spark RDD算子【二】coalesce 和 repartition
- Spark中repartition和coalesce的用法
- Spark RDD coalesce()方法和repartition()方法
- memset的使用以及原码反码补码.md
- [Hadoop] Hadoop Yarn显示DEBUG调试信息的配置方法
- iOS实现TextView提示文字并且输入字数限制
- hello word
- 【NOIP提高】公约数
- 【Spark Java API】Transformation(4)—coalesce、repartition
- AlarmManager使用
- 用Python实现网络爬虫
- php几种排序算法
- 往TreeMap和TreeSet里添加自定义对象的两种比较方法
- Unix/Linux系统使用命令wall,write,mesg命令实现快速对话
- python的字符串操作汇总
- Tomcat 序列化
- 【Spark Java API】Transformation(5)—cartesian、distinct