我在Spark(Python)上做我的第一步,我在一个groupByKey()
中与迭代器做斗争。我无法对值求和:我的代码如下所示:
example = sc.parallelize([('x',1), ('x',1), ('y', 1), ('z', 1)])
example.groupByKey()
x [1,1]
y [1]
z [1]
如何在Iterator
上求和?我试过下面这样的方法,但没用
example.groupByKey().map(lambda (x,iterator) : (x,sum(iterator))
example.groupByKey().map(lambda (x,iterator) : (x,list(sum(iterator)))
你也可以这样做:
有点晚了,但我刚找到解决办法
你可以简单地用
sum
来mapValues
:尽管在这种特殊情况下
reduceByKey
更有效:或者
加上@zero323的答案,另一个解决方案是:
相关问题 更多 >
编程相关推荐