I am new to Spark and scala and working on a simple wordCount example.
So for that i am using countByValue as follows:
val words = lines.flatMap(x => x.split("\\W+")).map(x => x.toLowerCase())
val wordCount = words.countByValue();
which works fine.
And the same thing can be achieved like :
val words = lines.flatMap(x => x.split("\\W+")).map(x => x.toLowerCase())
val wordCounts = words.map(x => (x, 1)).reduceByKey((x, y) => x + y)
val sortedWords = wordCounts.map(x => (x._2, x._1)).sortByKey()
which also works fine.
Now, my question is when to use which methods? Which one is preferred over the other?