1

我是相当新的 Spark Streaming

我有包含两个值 x y 的流数据。例如

1 300

2 8754

3 287

等等

在流数据中,我想得到最小的 y 值、最大的 y 值和 x 值的平均值。这需要输出如下(使用上面的示例):

287 8754 4

我已经能够在单个变换/减少上计算这些值,但无法使用单个变换

下面是我当前的代码

val transformedStream = windowStream.map(line => {
  Array(line.split(" ")(0).toLong, line.split(" ")(1).toLong)

val smallest: DStream[Double]  = transformedStream.reduce((a,b) => {
  Array(0, math.min(a(1), b(1)))
}).map(u => u(1).toDouble)

val biggest  = transformedStream.reduce((a,b) => {
  Array(0, math.max(a(1), b(1)))
}).map(u => u(1).toDouble)

val mean = transformedStream.reduce((a, b) => Array( (a(0) + b(0))/2 )).
  map(u => u(0).toDouble)
4

1 回答 1

2

尝试这个:

val spark: SparkSession = ???
import spark.implicits._

windowStream.transofrm( rdd => {
  val xy = rdd.map(_.split(" ")).collect {
    case Array(x, y) => (x.toLong, y.toLong)
  }
  xy.toDF("x", "y").agg(min("y"), max("y"), mean("x"))
  .as[(Long, Long, Double)].rdd
})

重要的:

transformedStream.reduce((a, b) => Array( (a(0) + b(0))/2 )  

不计算 的平均值x

于 2016-11-07T01:25:43.210 回答