【发布时间】:2018-02-15 15:22:10
【问题描述】:
当调用 reduceByKey 时,它将所有具有相同键的值相加。有什么方法可以计算每个键的平均值吗?
// I calculate the sum like this and don't know how to calculate the avg
reduceByKey((x,y)=>(x+y)).collect
Array(((Type1,1),4.0), ((Type1,1),9.2), ((Type1,2),8), ((Type1,2),4.5), ((Type1,3),3.5),
((Type1,3),5.0), ((Type2,1),4.6), ((Type2,1),4), ((Type2,1),10), ((Type2,1),4.3))
【问题讨论】:
-
我猜你的意思是 reduceByKey (如标题)。请显示您尝试过的代码示例。 ReduceByKey 没有默认聚合,所以如果它求和,你问它。
-
您需要分别求和和计数,参见例如stackoverflow.com/questions/29930110/…
-
在 scala 中用于 aggregateByKey 的文档:spark.apache.org/docs/latest/api/scala/…
-
不久前我问过并回答了一个类似的问题,它(以下网址)可能会对您和其他人有所帮助:stackoverflow.com/questions/29930110/…
标签: scala apache-spark