repartition和coalesce都是进行RDD的重新分区操作
那么他们有什么区别与各自合适的使用场景呢,我们来看下边的源码

def repartition(numPartitions: Int)(implicit ord: Ordering[T] = null): RDD[T] = withScope {coalesce(numPartitions, shuffle = true)}

可以看到 repartition 内部实现调用的 coalesce 且为coalesce中 shuffle = true的实现

注释中:

  • If you are decreasing the number of partitions in this RDD, consider using coalesce,
  • which can avoid performing a shuffle.

如果父RDD为1000分区,filter转换操作后过滤了50%的数据,想把数据重新分布在500分区中,这个时候是去减少分区这个时候使用coalesce(500)
能够避免引起shuffle

分区由少变多,或者在一些不是键值对的RDD中想要重新分区的话,就需要使用repartition了

coalesce函数的源码实现


view sourceprint?
<pre class="brush:java; toolbar: true; auto-links: true;">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 distributednew CoalescedRDD(new ShuffledRDD[Int, T, T](mapPartitionsWithIndex(distributePartition),new HashPartitioner(numPartitions)),numPartitions).values} else {new CoalescedRDD(this, numPartitions)}}</pre>

Spark中repartition和coalesce的区别与使用场景解析相关推荐

  1. Spark中repartition和coalesce的用法

    repartition(numPartitions:Int):RDD[T]和coalesce(numPartitions:Int,shuffle:Boolean=false):RDD[T] 他们两个都 ...

  2. Spark中 RDD之coalesce与repartition区别

    Spark中 RDD之coalesce与repartition区别 coalesce def coalesce(numPartitions: Int, shuffle: Boolean = false ...

  3. Spark中foreachPartition和mapPartitions的区别

    Spark中foreachPartition和mapPartitions的区别 spark的运算操作有两种类型:分别是Transformation和Action,区别如下: Transformatio ...

  4. php使用hset报错,redis中set和hset的区别和使用场景

    下面由redis教程栏目给大家介绍redis中set和hset的区别和使用场景,希望对需要的朋友有所帮助! redis中存数据时,到底什么时候用 hset 相比于 set 存数据时又有什么不一样? s ...

  5. C#中抽象类和接口的区别与应用场景

    一直对这两个基础概念没有很好的认识,在网上发现一个我能够理解的基础概念理解,存一下,反复看. C#中抽象类和接口的区别与应用场景一. 1.抽象类:抽象类是特殊的类,只是不能被实例化;和普通类一样里面什 ...

  6. spark中repartition, coalesce, partitionBy, repartitionAndSortWithinPartitions 四种重分区算子

    美图欣赏: 一.背景 spark中一共有四种重分区算子: 1.repartition 2.coalesce 3.partitionBy 4.repartitionAndSortWithinPartit ...

  7. Spark中mapToPair和flatMapToPair的区别【附示例源码及运行结果】

    本文重点介绍 Spark 中 [mapToPair]和[flatMapToPair]的区别,请继续看到尾部,后续有示例说明,会理解更加清晰. 函数原型 1.JavaPairRDD<K2,V2&g ...

  8. 【对比分析六】JavaScript中GET和POST的区别及使用场景

    区别: GET:一般用于信息获取,使用URL传递参数,对所发送信息的数量也有限制,一般在2000个字符 POST:一般用于修改服务器上的资源,对所发送的信息没有限制 GET方式需要使用 Request ...

  9. 面试提问vue中v-if与v-show的区别以及使用场景

    目录 区别 使用场景 总结 区别 1.手段:v-if是通过控制dom节点的存在与否来控制元素的显隐:v-show是通过设置DOM元素的display样式,block为显示,none为隐藏: 2.编译过 ...

最新文章

  1. 我去,你写的 switch 语句也太老土了吧
  2. 不会真有人国庆都不打王者吧?
  3. BC:带你温习并解读《中国区块链技术和应用发展白皮书》—国内外区块链发展现状
  4. 石家庄地铁查询PSP0级
  5. js for循环给object数组赋值
  6. 【C语言】成绩统计(结构)
  7. Python+matplotlib绘制三维图形5个精选案例
  8. 你们一年大概可以存多少钱?
  9. Error:Execution failed for task ':app:clean'. Unable to delete directory: /media/file/workspaces/a
  10. mysql 1236错误_MySQL主主同步环境出现1236错误
  11. 免安装mysql环境配置、完全卸载和无法启动问题详解
  12. Cesium 获取经纬度的几种方法
  13. 思科CCNP认证和华为 HCIP认证网络工程师必须了解的NETCONF四个层面的基本框架
  14. Linux 下载百度网盘大文件
  15. c语言摄氏度字符,c语言摄氏度与华氏温度如何转换
  16. 如何快速搞定技术女神
  17. 微信小程序中动态添加删除class类名 使用三元表达式动态设置标签的class名
  18. Tensorflow小白实战系列
  19. 虞凌云:5G网络技术发展将会带来这些变革
  20. python图像处理---python的图像处理模块Image

热门文章

  1. 页面全部按钮变成disabled=true_手机也能制作大片!只需轻松几步就能把照片变成视频。...
  2. 上传图片并显示缩略图的最简单方法(c#)
  3. Python知识点-py2和py3编码
  4. 【李宏毅2020 ML/DL】P26-33 Explainable ML
  5. Android 7.1 App Shortcuts使用
  6. 【数据结构笔记14】微软面试经典 - 逆转链表问题(Reversing Linked List)
  7. [翻译] AnchoredFloatView
  8. oracle日期函数函数,Oracle日期函数
  9. linux sql server调优,SQL SERVER性能优化(转)
  10. mysql 锁 行级_全局锁、表级锁、行级锁mysql 极客评论笔记