分组求平均值
sc.makeRDD(Array(1,2,2,3,3,3),2) .map(x=>(x,1)) .map(a => (a._1, (a._2, 1))) .reduceByKey((a,b) => (a._1+b._1,a._2+b._2)) .map(t => (t._1,t._2._1/t._2._2))
求一组数的平均值
val rdd = Array(1,2,2,3,3,3) println(rdd.sum() / rdd.count())
求value的平均值
val rdd = Array(("tom",1),("tom",2),("alice"2)) val map = rdd.map(x => (1,x._2)) //求总和 val res = map.reduceByKey(_+_) //求总个数 val l = map.count()