1铆隘、Java版wordCount
1)Java代碼:
/**
* 將Java開發(fā)的wordcount程序部署到spark集群上運(yùn)行
*/
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");//本地模式害驹,可以直接運(yùn)行因苹,不設(shè)置的話苟耻,默認(rèn)連接本地集群
//2、創(chuàng)建JavaSparkContext對象
//在spark中扶檐,SParkContext是spark所有功能的一個入口
JavaSparkContext jsc = new JavaSparkContext(conf);
//3凶杖、針對輸入源,創(chuàng)建一個初始的RDD
JavaRDD<String> lines = jsc.textFile(inputPath);
//4款筑、對初始RDD進(jìn)行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,都進(jìn)行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上去找文件
運(yùn)行結(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é)果進(jìn)行保存
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
運(yùn)行結(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>