单词计数
步骤:
1、将line数组赋值
2、将数组变成一个一个单词的数组
3、将单词数组变成(word,number)类型的map
4、将map的按key一样的分组
5、将key一样的统计出现次数
6、打印输出
补充:
排序:
1、将map转成list
2、将list按每个元素的第二个元素排序
3、打印输出
scala> val lines = List("hadoop hdfs mr hive","hdfs hive hbase storm kafka","hiv
e hbase storm kafka spark")
lines: List[String] = List(hadoop hdfs mr hive, hdfs hive hbase storm kafka, hiv
e hbase storm kafka spark)
scala> lines.flatMap(_.split(" "))
res28: List[String] = List(hadoop, hdfs, mr, hive, hdfs, hive, hbase, storm, kaf
ka, hive, hbase, storm, kafka, spark)
scala> lines.flatMap(_.split(" ")).map(x => (x,1))
res29: List[(String, Int)] = List((hadoop,1), (hdfs,1), (mr,1), (hive,1), (hdfs,
1), (hive,1), (hbase,1), (storm,1), (kafka,1), (hive,1), (hbase,1), (storm,1), (
kafka,1), (spark,1))