首先创建一个数组,其中存储三个字符串
val words: Array[String] = Array("Spark,hadoop,Flink,spark","spark,Hadoop,flink,Hive,clickHouse","flink,hive,clickHouse,spark,zooKerPer,hive")
将字符串切割开,并将内部的小数组打开
val wordsArray: Array[String] = words.flatMap(l =>l.split(","))
wordsArray: Array[String] = Array(Spark, hadoop, Flink, spark, spark, Hadoop, flink, Hive, clickHouse, flink, hive, clickHouse, spark, zooKerPer, hive)
按单词分组,并将大写转小写
val wordGb: Map[String, Array[String]] = wordsArray.groupBy(a=>a.toLowerCase)
wordGb: Map[String,Array[String]] = Map(zookerper -> Array(zooKerPer), flink -> Array(Flink, flink, flink), hadoop -> Array(hadoop, Hadoop), spark -> Array(Spark, spark, spark, spark), hive -> Array(Hive, hive, hive), clickhouse -> Array(clickHouse, clickHouse))
将元组中的数组统计数量
val word: Map[String, Int] = wordGb.map(t=>(t._1, t._2.length))
word: Map[String,Int] = Map(zookerper -> 1, flink -> 3, hadoop -> 2, spark -> 4, hive -> 3, clickhouse -> 2)
排序 可以用-号 也可以反转reverse
val wordPx: List[(String, Int)] = word.toList.sortBy(t=> -t._2)
wordPx: List[(String, Int)] = List((spark,4), (flink,3), (hive,3), (hadoop,2), (clickhouse,2), (zookerper,1))