【问题标题】:using Spark: binning column1 and find mean of column2 based on column1's bins使用 Spark:分箱 column1 并根据 column1 的 bin 查找 column2 的平均值
【发布时间】:2016-04-13 19:16:32
【问题描述】:
我正在学习 apache spark 和 scala 语言。所以请帮忙。我从查询 cassandra 中得到 3 列(c1、c2 和 c3),并将其放在 scala 代码中的数据框中。我必须bin(bin size = 3) (统计,如直方图)c1 并在 c1 箱中找到 c2 和 c3 的平均值。是否有任何预构建的函数可以用来代替传统的 for 循环和 if 条件来实现这一点?
【问题讨论】:
标签:
scala
apache-spark
binning
【解决方案1】:
试试这个
val modifiedRDD = rdd.map{case(c1, c2, c3) => ((c1), (c2, c3, 1))}
val reducedRDD = modifiedRDD.reduceByKey{case(x, y) => (x._1+y._1, x._2+y._2, x._3+y._3)}
val finalRDD = reducedRDD.map{case((c1), (totalC2, totalC3, count)) => (c1, totalC2/count, totalC3/count)}