了解 Spark 中的 treeReduce()

Understanding treeReduce() in Spark

你可以在这里看到实现: https://github.com/apache/spark/blob/ffa05c84fe75663fc33f3d954d1cb1e084ab3280/python/pyspark/rdd.py#L804

它与 'normal' reduce 函数有何不同?
depth = 2 是什么意思?

我不希望 reducer 函数在分区上线性传递, 但是首先减少每个可用的对,然后像这样迭代直到我只有一对并将其减少到 1,如图所示:

treeReduce 实现了吗?

标准 reduce 正在采用函数的包装版本并将其用于 mapPartitions. After that results are collected and reduced locally 驱动程序。如果分区数量很大 and/or 您使用的功能很昂贵,它会给单台机器带来很大的负担。

treeReduce的第一阶段与上面几乎相同,但之后部分结果被并行合并,并且只在驱动程序上执行最终聚合。

depth 在某些情况下是 suggested depth of the tree and since depth of the node in tree is defined as number of edges between the root and the node it should you give you more or less an expected pattern although it looks like a distributed aggregation can be stopped early

值得注意的是,treeReduce得到的不是二叉树。分区的数量在每个级别上进行调整,并且很可能一次合并两个以上的分区。

与标准 reduce 相比,基于树的版本 performs reduceByKey with each iteration 这意味着大量的数据改组。如果分区数量相对较少,使用普通 reduce 会便宜得多。如果您怀疑 reduce 的最后阶段是一个瓶颈 tree* 版本可能值得一试。