2016-04-28 10 views

回答

2

说你的时间序列是(日,值)对:

(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() 
相关问题