Apache Spark: comparison of map vs flatMap vs mapPartitions vs mapPartitionsWithIndex

喜夏-厌秋 提交于 2019-11-30 10:58:04

map(func) What does it do? Pass each element of the RDD through the supplied function; i.e. func

flatMap(func) “Similar to map, but each input item can be mapped to 0 or more output items (so func should return a Seq rather than a single item).”

Compare flatMap to map in the following

mapPartitions(func) Consider mapPartitions a tool for performance optimization. It won’t do much for you when running examples on your local machine compared to running across a cluster. It’s the same as map, but works with Spark RDD partitions. Remember the first D in RDD is “Distributed” – Resilient Distributed Datasets. Or, put another way, you could say it is distributed over partitions.

mapPartitionsWithIndex(func) Similar to mapPartitions, but also provides a function with an Int value to indicate the index position of the partition.

If we change the above example to use a parallelize’d list with 3 slices, our output changes significantly:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!