countByKey
Count the number of elements for each key, collecting the results to a local Map.
This method should only be used if the resulting map is expected to be small, as the whole thing is loaded into the driver's memory. To handle very large results, consider using rdd.mapValues { 1L }.reduceByKey(Long::plus)
, which returns an RDD