Difference between map() and flatMap() transformations in Spark

bookmark

map() transformation flatMap() transformation
Transforms each input element into Transforms each input element into
a single output element Zero or more output elements
Produces a one-to-one mapping Produces a one-to-many mapping
Retains the original structure of the RDD Flattens the output by removing any intermediate layers
Useful for simple transformations and Useful for transformations that require
aggregations exploding or splitting elements