代码:
val conf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("wordcount")
val sc=new SparkContext(conf)
//[1] [2,3] [4,5]
val rdd: RDD[Int] = sc.makeRDD(List(1,2,3,4,5),3)
//将处理的数据保存分区文件
rdd.saveAsTextFile("output2")
sc.stop()
通过sc.makeRDD(List(1,2,3,4,5),3) 得到3个文件分别存储数据为[1] [2,3] [4,5]
点击makeRDD方法




positions(array.length, numSlices) 有两个参数一个是数组的长度,一个是分区的数量
对应代码为positions(5,3)

0 until numSilces 的结果为0 1 2,进行迭代 i的取值分别去 0 1 2
当i=0时,start=0,end=1*5/3=1 返回(0,1)
当i=1是,start=1*5/3=1,end=2*5/3=3 返回 (1,3)
当i=2时,start=3 ,end=3*5/3=5 返回(3,5)
得到元祖之后进行操作array.slice(start, end).toSeq

i=0 => (0,1)=> slice(0,1) => 0 索引 => 1
i=1 => (1,3)=> slice(1,3) => 1,2 索引 => 2,3
i=2 => (3,5)=> slice(3,5) => 3,4 索引 => 4,5