说明
- 将RDD中的元素去重,并返回到新的RDD中。
- 新RDD分区数量默认和旧的RDD一致。
- 此过程会发生shuffle。
函数签名
代码实现
val conf: SparkConf = new SparkConf().setAppName(this.getClass.getName).setMaster("local[*]")
val sc = new SparkContext(conf)
val rdd: RDD[Int] = sc.makeRDD(List(1, 3, 5, 7, 9, 9, 7, 3), 2)
println("-----------------去重前-------------------")
rdd.mapPartitionsWithIndex {
(index, datas) => {
println(index + "----->" + datas.mkString(","))
datas
}
}.collect()
println("-----------------去重后-------------------")
val newRDD: RDD[Int] = rdd.distinct(3)
newRDD.mapPartitionsWithIndex{
(index, datas) => {
println(index + "----->" + datas.mkString(","))
datas
}
}.collect()
sc.stop()