一定会有shuffer,因为不能保证相同的key的数据在同一个分区中。
groupByKey
package com.atguigu
import org.apache.spark.rdd.RDD
import org.apache.spark.{HashPartitioner, Partitioner, SparkConf, SparkContext}
object Trans {
def main(args: Array[String]): Unit = {
val conf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("Spark01_Partition")
//构建spark上下文对象
val sc = new SparkContext(conf)
val rdd: RDD[(String, Int)] = sc.makeRDD(Array(("aaa",1),("cc",2),("b",4),("b",1),("b",1)))
val rdd2: RDD[(String, Iterable[Int])] = rdd.groupByKey()
rdd2.collect().foreach(println)
sc.stop()
}
}
(cc,CompactBuffer(2))
(aaa,CompactBuffer(1))
(b,CompactBuffer(4, 1, 1))
网友评论