提交wordCount程序到spark集群上運行

1、Java版wordCount
1)Java代碼:

/**
 * 將Java開發(fā)的wordcount程序部署到spark集群上運行
 */
public class WordCountCluster {
    public static void main(String[] args) {
        String inputPath = args[0];
        String outputPath =args[1];
        //編寫spark應(yīng)用程序
        //1、創(chuàng)建spark對象,設(shè)置spark應(yīng)用的配置
        SparkConf conf = new SparkConf()
                .setAppName("WordCountCluster");//應(yīng)用程序的名稱
//                .setMaster("local");//本地模式,可以直接運行,不設(shè)置的話,默認連接本地集群
        //2、創(chuàng)建JavaSparkContext對象
        //在spark中,SParkContext是spark所有功能的一個入口
        JavaSparkContext jsc = new JavaSparkContext(conf);
        //3、針對輸入源,創(chuàng)建一個初始的RDD
        JavaRDD<String> lines = jsc.textFile(inputPath);
        //4、對初始RDD進行transferformation操作,也就是一些計算操作
        //把每一行拆分成一個個的單詞
        JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public Iterator<String> call(String s) throws Exception {
                return Arrays.asList(s.split(" ")).iterator();
            }
        });

        //將每個單詞映射為(單詞,1)的這種格式
        JavaPairRDD<String,Integer> pairs = words.mapToPair(new PairFunction<String, String, Integer>() {
            @Override
            public Tuple2<String, Integer> call(String word) throws Exception {
                return new Tuple2<String, Integer>(word,1);
            }
        });

        //以單詞作為key,統(tǒng)計每個單詞出現(xiàn)的次數(shù)
        //使用reduceByKey算子,對每個key對應(yīng)的value,都進行reduce操作
        JavaPairRDD<String,Integer> wordCOunts = pairs.reduceByKey(
                new Function2<Integer, Integer, Integer>() {
            @Override
            public Integer call(Integer v1, Integer v2) throws Exception {
                return v1 + v2;
            }
        });

        //最后,使用一種action操作,比如foreach,來觸發(fā)程序的執(zhí)行
        wordCOunts.foreach(new VoidFunction<Tuple2<String, Integer>>() {
            @Override
            public void call(Tuple2<String, Integer> wordCOunt) throws Exception {
                System.out.println(wordCOunt._1 + " appeared "+ wordCOunt._2 +" times.");
            }
        });
        //action操作,也可以是保存數(shù)據(jù)
        wordCOunts.saveAsTextFile(outputPath);
        jsc.close();
    }
}

2)打包代碼上傳到服務(wù)器

sparkjava-1.0-SNAPSHOT.jar

3)上傳文件到hdfs上去

hdfs dfs  -put englist /user/hadoop/english
圖片.png

4)使用spark-submit提交

bin/spark-submit \
--class cn.spark.core.WordCountCluster \
--num-executors 3 \
--driver-memory 512m \
--executor-memory 100m \
--executor-cores 3 \
/usr/local/hadoop/spark/test/sparkjava-1.0-SNAPSHOT.jar \
/user/hadoop/english \
/user/hadoop/javaoutput/english_output

這里的輸入路徑和輸出路徑可以不是hdfs的路徑,但是也會去hdfs上去找文件
運行結(jié)果:


圖片.png

2、scala版wordcount
1)scala代碼如下:

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object ScalaWorldCount {
  def main(args: Array[String]): Unit = {
    val inputpath = args(0)
    val outputpath = args(1)
    val conf = new SparkConf()
    conf.setAppName("ScalaWorldCount")// //設(shè)置任務(wù)名
    val sc = new SparkContext(conf) //創(chuàng)建SparkCore的程序入口
    val lines: RDD[String] = sc.textFile(inputpath,1)//讀取文件生成RDD
    //把每一行數(shù)據(jù)按照,分割
    val word: RDD[String] = lines.flatMap(_.split(" "))
    //讓每一個單詞都出現(xiàn)一次
    val wordOne: RDD[(String, Int)] = word.map((_,1))
    //單詞計數(shù)
    val wordCount: RDD[(String, Int)] = wordOne.reduceByKey(_+_)
    //按照單詞出現(xiàn)的次數(shù)降序排序
    val sortRdd: RDD[(String, Int)] = wordCount.sortBy(tuple => tuple._2,false)
    //將最終的結(jié)果進行保存
    sortRdd.saveAsTextFile(outputpath)
    sc.stop()
  }
}

2)打包上傳到服務(wù)器

sparkdemo-1.0-SNAPSHOT.jar

3)使用spark-submit提交

bin/spark-submit \
--class ScalaWorldCount \
--num-executors 3 \
--driver-memory 512m \
--executor-memory 100m \
--executor-cores 3 \
/usr/local/hadoop/spark/test/sparkdemo-1.0-SNAPSHOT.jar \
/user/hadoop/english \
/user/hadoop/sparkoutput/english_output

運行結(jié)果:


圖片.png

下載結(jié)果文件查看,如圖所示,這里我做了排序,但是從結(jié)果看,并沒有排序,暫時還不知道原因,知道的兄弟可以跟我說一下:


圖片.png

3、用spark-shell開發(fā)wordcount程序
代碼如下:

scala> import org.apache.spark.rdd.RDD
import org.apache.spark.rdd.RDD

scala> val lines = sc.textFile("/user/hadoop/english")
lines: org.apache.spark.rdd.RDD[String] = /user/hadoop/english MapPartitionsRDD[1] at textFile at <console>:24

scala> val word: RDD[String] = lines.flatMap(_.split(" "))
word: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[2] at flatMap at <console>:26

scala> val wordOne: RDD[(String, Int)] = word.map((_,1))
wordOne: org.apache.spark.rdd.RDD[(String, Int)] = MapPartitionsRDD[3] at map at <console>:26

scala> val wordCount: RDD[(String, Int)] = wordOne.reduceByKey(_+_)
wordCount: org.apache.spark.rdd.RDD[(String, Int)] = ShuffledRDD[4] at reduceByKey at <console>:26

scala> wordCount.take(10)
res0: Array[(String, Int)] = Array((pump,,1), (Let,1), (health,1), (it,2), (oxygen,2), (The,1), (have,5), (carried,1), (unusual,1), (“I,1))

但是在spark-shell上也是可以排序的

scala> val sortRdd: RDD[(String, Int)] = wordCount.sortBy(tuple => tuple._2,false)
sortRdd: org.apache.spark.rdd.RDD[(String, Int)] = MapPartitionsRDD[7] at sortBy at <console>:26

scala> sortRdd.take(10)
res1: Array[(String, Int)] = Array((to,16), (his,15), (in,11), (him,10), (and,8), (he,7), (the,7), (was,7), (she,7), (that,7))

scala> 

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容