Hap*_*ane 4 scala apache-spark
鉴于以下内容:
val rdd = List(1,2,3)
Run Code Online (Sandbox Code Playgroud)
我假设rdd.reduce((x,y) => (x - y))会返回-4(即(1-2)-3=-4),但它返回2.
为什么?
从RDD源代码(和文档):
/**
* Reduces the elements of this RDD using the specified commutative and
* associative binary operator.
*/
def reduce(f: (T, T) => T): T
Run Code Online (Sandbox Code Playgroud)
reduce是一个幺半数减少,因此它假定函数是可交换和关联的,这意味着不能保证将它应用于元素的顺序.
显然,你的功能(x,y)=>(x-y)不是可交换的,也不是联想的.
在您的情况下,reduce可能是以这种方式应用的:
3 - (2 - 1) = 2
Run Code Online (Sandbox Code Playgroud)
要么
1 - (2 - 3) = 2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
678 次 |
| 最近记录: |