Spark-读取内部数据分区存储策略源码角度分析
Spark-读取内部数据分区策略(源码角度分析):https://blog.csdn.net/lucklilili/article/details/115432028
针对下面代码片段源码角度进行分析,创建RDD并且调用saveAsTextFile()函数,最终执行结果为part-00000 => 1、part-00000 => 2 3、part-00003 => 4 5
Step1:
调用makeRDD函数,长度size = 5,分区数numSlices = 3。
Step2:
调用parallelize函数,长度size = 5,数据1、2、3、4、5,分区数numSlices = 3。
Step3:
调用parallelize的getPartitions函数,长度size = 5,数据1、2、3、4、5,分区数numSlices = 3。
Step4:
执行slice函数的case模式匹配,长度size = 5,数据1、2、3、4、5,分区数numSlices = 3。
Step5:
调用本函数里面positions方法,长度size = 5,分区数numSlices = 3 。
// Sequences need to be sliced at the same set of index positions for operations
// like RDD.zip() to behave as expected
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)
}
}
1:
var start = (0 * 5) / 3 = 0
var end =((0 + 1) * 5) /3 = 1
2:
var start = (1 * 5) / 3 = 1
var end =((1 + 1) * 5) / 3 = 3
3:
var start = (2 * 5) / 3 = 3
var end =((2 + 1) * 5) / 3 = 5
Step6:
循环遍历调用slice函数
数据:1,2,3,4,5
每次执行slice()后得的结果(0,1),(1,3),(3,5),最终结果如:
(0,1) = 1 => part00000
(1,3) = 2,3 => part00001
(3,5) => 4,5 => part00001