def main(args: Array[String]): Unit = {
val spark = new SparkConf().setMaster("local[*]").setAppName("spark")
val context = new SparkContext(spark)
val rdd = context.makeRDD(List(1, 2, 3, 4), 2)
//参数一;分区号;参数二:分区中的数据
rdd.mapPartitionsWithIndex( (index, datas) => {
if (index == 1) {
datas
} else {
Nil.iterator
}
}).collect().foreach(println)
context.stop()
}
mapPatitionsWithIndex 和 mapPartitions 很像都是需要一个迭代器,并返回一个迭代器。但是mpwi需要两个参数,第一个参数是第几个分区(和数组一样是从0开始的),不需要提前定义