在获取平均星火整个键 [英] Getting average across keys in Spark

查看:152
本文介绍了在获取平均星火整个键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算整个按键平均为星火?

How to calculate averages across keys in Spark?

推荐答案

我们可以计算或者使用Spark中跨项平均 combineByKey foldByKey

We can calculate averages across keys in Spark either using combineByKey or foldByKey.

foldByKey(initialValue)((initialValue,inputDataValue) => { //code })

输入数据:

employee,department,salary
e1,d1,100
e2,d1,500
e5,d2,200
e6,d1,300
e7,d3,200
e7,d3,500

在端

1是用于计数。依据折叠输入的类型和初值必须匹配

1 at the end is for counts. Being fold the type of input and initialValue must match

val depSalary = data.map(_.split(',')).map( x=> (x(1),(x(2).toInt,1)))   

val dummy = (0,0)
val depSalarySumCount = depSalary.foldByKey(dummy)((startValue,data)  => ( startValue._1 + data._1 , startValue._2 +data._2  ) )   

val result =  depSalarySumCount.map(x => (x._1, (x._2._1/x._2._2) ))
result.collect

这篇关于在获取平均星火整个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆