Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //方法一
- val studentScores = allScores.map {
- case (student, score) => (student, (score, 1))
- }
- val totalScores = studentScores.reduceByKey {
- case ((score1, count1), (score2, count2)) => (score1 + score2, count1 + count2)
- }
- val averageScores = totalScores.map {
- case (student, (totalScore, count)) => (student, totalScore.toDouble / count)
- }
- //方法二
- val averageScores = allScores.mapValues(x => (x,1))
- .reduceByKey((x,y) => (x._1+y._1,x._2 + y._2))
- .mapValues(x => (x._1 / x._2)).collect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement