天天看點

Spark 資源和資料并行度優化分析3 | 學習筆記

開發者學堂課程【大資料實時計算架構 Spark 快速入門:Spark資源和資料并行度優化分析3】學習筆記,與課程緊密聯系,讓使用者快速學習知識。

課程位址:

https://developer.aliyun.com/learning/course/100/detail/1686

Spark 資源和資料并行度優化分析3

内容簡介:

一、FlatMap 算子

二、Collect 算子

public static void main(String[] args) {

SparkConfconf = new SparkConf().setAppName("FlatMapOperator").setMaster("local");

JavaSparkContext sc = new JavaSparkContext(conf);

List linelist = Arrays.aslist("hello xuruyun","hello xuruyun","hello JavaRDD lines=sc.parallelize(lineList);

// flatMap = flat+ map

JavaRDD words = lines.flatMap(new FlatMapFunction(){private static final long serialVersionUID = 1L;

@Override

public Iterable call(String line) throws Exception {

returnArrays.aslist(line.split(""));

}};

words. foreach(new VoidFunction(){private static final long serialVersionUID = 1L;

@Override

public void call(String result) throws Exception {

public class Collectoperator {

publicstatic void main(String[] args) {

SparkConfconf = new SparkConf().setAppName("ReduceOperator").setMaster("local");

JavaSparkContext sc = new JavaSparkContext(conf);

//有一個集合,裡面有1到10,10個數字,現在我們通過 reduce 來進行累加

List numberList = Arrays. asList(1, 2, 3, 4, 5);

JavaRDD numbers = sc.parallelize(numberList);

JavaRDD doubleNumbers = numbers. map(new Function

@Override

public Integer call(Integer v) throws Exception {

returnv* 2;

});

// 用 foreach action 操作, collect 在遠端叢集上周遊 RDD 的元素// 用collect 操作,将分布式的在遠端叢集裡面的資料拉取到本地!!!//這種方式不建議使用,如果資料量大,走大量的網絡傳輸//甚至有可能 OOM 記憶體溢出,通常情況下你會看到用 foreach 操作 List doubleNumberList = doubleNumbers.

collect();for(Integer num: doubleNumberList){

System.out.println(num);

sc.close();

繼續閱讀