1
我已将1天的数据汇总为1天。我想将数据分成24 X 1小时的数据。Apache Spark:将时间序列数据从一天一行转换为24 x 1小时行
Input
1 24
output
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
...
1 24
我已将1天的数据汇总为1天。我想将数据分成24 X 1小时的数据。Apache Spark:将时间序列数据从一天一行转换为24 x 1小时行
Input
1 24
output
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
...
1 24
说你的时间序列是(日,值)对:
(1,10)
(2,5)
(3,4)
...
而想将它们转换成(小时值)对其中的价值仍然是所有对同一同一天。
(1,10)
(2,10)
(3,10)
...
(24,10)
(25,5)
...
(48,5)
(49,4)
...
(72,4)
...
这里是如何在基本斯卡拉做到这一点:
val timeSeries = Seq(1->10, 2->5, 3->4)
timeSeries.flatMap{ case(day,value) =>
((1 to 24)).map(h => ((h+(day-1)*24),value))
}
这里是如何做到这一点的星火:
val rddTimeSeries = sc.makeRDD(timeSeries)
// Very similar with what we do in Scala
val perHourTs = rddTimeSeries.flatMap{ case(day,value) =>
((1 to 24)).map(hour => ((hour + (day-1)*24), value))
}
// We can print it given that we know the list is small
println(perHourTs.collect().toList)
一个星火复杂的是,数据可能会出来订单可能会扰乱您的时间序列中的订单。为了解决这个问题,最简单的方法是在您调用RDD上的操作之前对数据进行排序。
// Here is how to sort your time series
perHourTs.sortBy(_._1).collect()
你的意思是你有双(x,y),其中x是时间,y是价值,时间是几天,你想把它转换为几小时? – marios
是的,这是正确的 –