package demo04
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
/**
* Created by 76549 on 2018/9/13.
*/
object SparkWC {
def main(args: Array[String]): Unit = {
val conf: SparkConf = new SparkConf().setAppName("SparkWC").setMaster("local[*]");
val sc: SparkContext = new SparkContext(conf);
//得到包含n个line的lines集合
val lines = sc.textFile(args(0));
//flatMap里面的匿名函数_.split(" ")会对lines集合里面的每个元素line执行line.split(" ")生成一个小的words的集合,
// 得到n个小words集合后,此时flatMap把ngewords集合拍扁得到一个总的大words集合
val words: RDD[String] = lines.flatMap(_.split(" "));
//val words:RDD[String]=lines.flatMap(line=>line.split(" "));
//map函数的作用就是把words里面的每个元素,执行一下map里面的内置函数,然后得到一个新的结果集合。
// 比如words.map((_, 1))就是把words里面的每个word都执行一下匿名函数(_,1),得到(word,1),
// 然后n个(word,1)组成一个大的集合List((word0,1),(word1,1),.....)
val paired: RDD[(String, Int)] = words.map((_, 1));
//val paired:RDD[(String,Int)]=words.map(word=>(word,1));
//reduceByKey是根据Key分组,然后执行_+_得到sum,最终得到,List((key0,value0),(key1,value1),....)
val reduced:RDD[(String,Int)] =paired.reduceByKey(_+_);
//val reduced:RDD[(String,Int)]=paired.reduceByKey((a,b)=>a+b);
//reduced.sortBy(_._2,false),_._2第一个_代表的是集合里的每个map(key0,value0),
//_2则代表取map(key0,value0)的第二个元素。 false代表是逆向排序。
val res:RDD[(String,Int)]=reduced.sortBy(_._2,false);
//res.saveAsTextFile(args(1));
println(res.collect().toBuffer);
sc.stop();
}
}
下面是maven的注意事项
1.maven配置没有设置default setting ,导致下载依赖包的时候变慢了。以下是设置方式,一劳永逸。

2.scala-maven-plugin作用
The scala-maven-plugin is used for compiling/testing/running/documenting scala code in maven.
常见的组件作用,可以参照https://blog.csdn.net/sivolin/article/details/46877659
9247




被折叠的 条评论
为什么被折叠?



