Word count from list via spark scala
Input :
Output:
Solution:
//Consider here some list
val data = List("mango","apple","mango","orange","apple","banana")
//First we need to make rdd.
val rdd_data = sc.parallelize(data)
//Flatten data
val flat_data = rdd_data.flatMap(x=>x.split(" "))
//add suffix on all as 1
val suf_data = flat_data.map(x=>(x,1))
//reduced and count data
val word_count = suf_data.reduceByKey((x,y)=>x+y)
//print data
word_count.foreach(println)
Comments
Post a Comment