Created
November 21, 2020 10:35
-
-
Save Wapiti08/51a176a47ecea191b6b8233763472c50 to your computer and use it in GitHub Desktop.
Methods on Word Count by Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
object WordCount { def main(args: Array[String]): Unit = { | |
val arr = Array("hello flink", "hello spark", "hello spark") | |
val arr_total = arr.flatMap(x => x.split(" ")) | |
// val counts = arr_total.map(word => word->1).groupBy(_._2).map(x => x._1 -> x._2.size) | |
val counts = arr_total.groupBy(w => w).mapValues(_.size) println(counts) } } | |
``` | |
``` | |
val counts = arr_total.map((_,1)).reduceByKey(_+_).collect() | |
counts.foreach(println) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment