import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;
import java.util.Arrays;
import java.util.List;
/**
* groupbykey([numTasks]) 算子:
* 将rdd中的算子按照key进行分组操作,所有的key对应的是一个iterable
* 可以设置并行度,如果没有设置并行默认与父RDD保持一直,也就是父RDD有多少partitions,它的并行度就是多少
* 是对RDD中的所有数据做shuffle,根据不同的Key映射到不同的partition中再进行aggregate
*/
public class GroupByKeyOperator {
public static void main(String[] args) {
SparkConf conf = new SparkConf().setMaster("local").setAppName("groupbykey");
JavaSparkContext sc = new JavaSparkContext(conf);
List<Tuple2<String,String>> list = Arrays.asList(
new Tuple2("W1","1"),
new Tuple2("W2","2"),
new Tuple2("W3","3"),
new Tuple2("W2","22"),
new Tuple2("W1","11")
);
JavaPairRDD<String,String> listRdd = sc.parallelizePairs(list,2);
JavaPairRDD<String,Iterable<String>> result = listRdd.groupByKey(3);
result.foreach(new VoidFunction<Tuple2<String, Iterable<String>>>() {
@Override
public void call(Tuple2<String, Iterable<String>> stringIterableTuple2) throws Exception {
System.err.println(stringIterableTuple2._1+":"+stringIterableTuple2._2);
}
});
}
}
微信扫描下图二维码加入博主知识星球,获取更多大数据、人工智能、算法等免费学习资料哦!