Spark - How to word count without RDD

Viewed 32
1 Answers

The data you manipulate as tuples using RDD api can be thought of and manipulated as columns/fields in a SQL like manner using DataFrame api.

df.withColumn("word", explode(split(col("lines"), " ")))
  .groupBy("word")
  .count()
  .orderBy(col("count").desc())
  .show()
+---------+-----+
|     word|count|
+---------+-----+
|      foo|    5|
|      bar|    2|
|     toto|    1|
...
+---------+-----+

Notes:

  • This code snippet requires necessary imports from org.apache.spark.sql.functions
  • Relevant examples can be found in this question's answers.
Related