您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何使用spark Context轉成RDD”,在日常操作中,相信很多人在如何使用spark Context轉成RDD問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何使用spark Context轉成RDD”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
在spark rdd轉換算子中join和cogroup是有些需要區分的算子轉換,這里使用示例來說明一下。
List<Tuple2<Integer, String>> studentsList = Arrays.asList( new Tuple2<Integer,String>(1,"xufengnian"), new Tuple2<Integer,String>(2,"xuyao"), new Tuple2<Integer,String>(2,"wangchudong"), new Tuple2<Integer,String>(3,"laohuang") ); List<Tuple2<Integer, Integer>> scoresList = Arrays.asList( new Tuple2<Integer,Integer>(1,100), new Tuple2<Integer,Integer>(2,90), new Tuple2<Integer,Integer>(3,80), new Tuple2<Integer,Integer>(1,101), new Tuple2<Integer,Integer>(2,91), new Tuple2<Integer,Integer>(3,81), new Tuple2<Integer,Integer>(3,71) );
JavaPairRDD<Integer,String> studentsRDD = sc.parallelizePairs(studentsList); JavaPairRDD<Integer,Integer> scoresRDD = sc.parallelizePairs(scoresList); //studentsRDD 為:List<Tuple2<Integer, String>> //(1,xufengnian)(2,xuyao)(2,wangchudong)(3,laohuang),下面進行打印查看 studentsRDD.foreach(new VoidFunction<Tuple2<Integer,String>>(){ public void call(Tuple2<Integer,String> tuple){ System.out.println(tuple._1);//1 2 3 System.out.println(tuple._2);// xufengnian xuyao laohuang } });
/* 前面數據 (1,xufengnian)(2,xuyao)(2,"wangchudong")(3,laohuang) (1,100)(2,90)(3,80)(1,101)(2,91)(3,81)(3,71) join之后: (1,(xufengnian,100))(1,(xufengnian,101))(3,(laohuang,80))(3,(laohuang,81))(3,(laohuang,71)) (2,(xuyao,90))(2,(xuyao,91))(2,(wangchudong,90))(2,(wangchudong,91)) */ JavaPairRDD<Integer, Tuple2<String, Integer>> studentScores = studentsRDD.join(scoresRDD); //join為key相同的join,key不變,value變成(string,integer) studentScores.foreach(new VoidFunction<Tuple2<Integer,Tuple2<String,Integer>>>() { private static final long serialVersionUID = 1L; @Override public void call(Tuple2<Integer, Tuple2<String, Integer>> student) throws Exception { System.out.println("student id: " + student._1);//1 1 3 System.out.println("student name: " + student._2._1);//xufengnian xufengnian laohuang System.out.println("student score: " + student._2._2);//100 101 80 System.out.println("==================================="); } });
/* 前面的數據 (1,xufengnian)(2,xuyao)(2,"wangchudong")(3,laohuang) (1,100)(2,90)(3,80)(1,101)(2,91)(3,81)(3,71) cogroup之后: (1,([xufengnian],[100,101])) (3,([laohuang],[80,81,71])) (2,([xuyao,wangchudong],[90,91])) */ JavaPairRDD<Integer,Tuple2<Iterable<String>,Iterable<Integer>>> studentScores2 = studentsRDD.cogroup(scoresRDD); studentScores2.foreach(new VoidFunction<Tuple2<Integer, Tuple2<Iterable<String>, Iterable<Integer>>>>() { @Override public void call(Tuple2<Integer, Tuple2<Iterable<String>, Iterable<Integer>>> stu) throws Exception { System.out.println("stu id:"+stu._1);//1 3 System.out.println("stu name:"+stu._2._1);//[xufengnian] [laohuang] System.out.println("stu score:"+stu._2._2);//[100,101] [80,81,71] Iterable<Integer> integers = stu._2._2; for (Iterator iter = integers.iterator(); iter.hasNext();) { Integer str = (Integer)iter.next(); System.out.println(str);//100 101 80 81 71 } System.out.println("==================================="); } });
到此,關于“如何使用spark Context轉成RDD”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。