Spark中的RDD的分区算法


def positions(length: Long, numSlices: Int): Iterator[(Int, Int)] = {
      (0 until numSlices).iterator.map { i =>
        val start = ((i * length) / numSlices).toInt
        val end = (((i + 1) * length) / numSlices).toInt
        (start, end)
      }
}


/**

  • numSlices 分区数
  • (0 until numSlices).iterator 是将分区数变成Iterator,再通过map算法将
  • 0->0 转换成 0->(0,n) 这种结构,就是说0号分区读取0到n个数据集。
  • map算法中的实现为:
  • val start = ((i * length) / numSlices).toInt
  • val end = (((i + 1) * length) / numSlices).toInt
  • 最终返回 Iterator[(start,end)]
  • 这样就可以尽量平均的把数据集分配到每一个分区

*
*/

大数据集群中经常碰到需要把数据分区分片,分配到集群中的各个节点执行,以调动集群资源来同步执行同一个任务,这样会大大加快任务的执行效率。所以优秀的分区算法是不可或缺的一部分。


farAway
48 声望6 粉丝

小白也有一颗大牛心~