reduce() vs. fold() in Apache Spark

不问归期 提交于 2020-01-01 09:54:07

问题


What is the difference between reduce vs. fold with respect to their technical implementation?

I understand that they differ by their signature as fold accepts additional parameter (i.e. initial value) which gets added to each partition output.

  • Can someone tell about use case for these two actions?
  • Which would perform better in which scenario consider 0 is used for fold?

Thanks in advance.


回答1:


There is no practical difference when it comes to performance whatsoever:

  • RDD.fold action is using fold on the partition Iterators which is implemented using foldLeft.
  • RDD.reduce is using reduceLefton the partition Iterators.

Both methods keep mutable accumulator and process partitions sequentially using simple loops with foldLeft implemented like this:

foreach (x => result = op(result, x))

and reduceLeft like this:

for (x <- self) {
  if (first) {
    ...
  }
  else acc = op(acc, x)
}

Practical difference between these methods in Spark is only related to their behavior on empty collections and ability to use mutable buffer (arguably it is related to performance). You'll find some discussion in Why is the fold action necessary in Spark?

Moreover there is no difference in the overall processing model:

  • Each partition is processed sequentially using a single thread.
  • Partitions are processed in parallel using multiple executors / executor threads.
  • Final merge is performed sequentially using a single thread on the driver.


来源:https://stackoverflow.com/questions/36056895/reduce-vs-fold-in-apache-spark

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