Skip to content

Instantly share code, notes, and snippets.

@Wapiti08
Created November 21, 2020 10:35
Show Gist options
  • Save Wapiti08/51a176a47ecea191b6b8233763472c50 to your computer and use it in GitHub Desktop.
Save Wapiti08/51a176a47ecea191b6b8233763472c50 to your computer and use it in GitHub Desktop.
Methods on Word Count by Scala
```
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