Flink(scala)计算并贴上计算的实时时间
代码:
import java.text.SimpleDateFormat
import kfr.testhr.Sensor
import org.apache.flink.api.common.state.{ValueState, ValueStateDescriptor}
import org.apache.flink.streaming.api.functions.KeyedProcessFunction
import org.apache.flink.streaming.api.scala._
import org.apache.flink.util.Collector
object testw {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setParallelism(1)
// 时间,分组相加
env.readTextFile("datas/sensor.txt")
.map(elem => {
val datas = elem.split(",")
get(datas(0), datas(2).toDouble)
})
.keyBy(_.id)
.process(new dsy)
.print()
env.execute()
}
class dsy extends KeyedProcessFunction[String, get, shu] {
val dateformat = new SimpleDateFormat("yyyy-MM-d HH:mm:ss")
var sumte = 0.0
lazy val lastTemp: ValueState[Double] = getRuntimeContext.getState(new ValueStateDescriptor[Double]("sum", classOf[Double]))
override def processElement(i: get, context: KeyedProcessFunction[String, get, shu]#Context, collector: Collector[shu]): Unit = {
val temp = i.temp
val id = i.id
val d = lastTemp.value()
sumte = temp + d
val curr = context.timerService().currentProcessingTime()
val str = dateformat.format(curr)
lastTemp.update(sumte)
collector.collect(shu(str, id,sumte))
Thread.sleep(1000L)
}
}
case class get(id: String, temp: Double)
case class shu(time: String,id:String, sum: Double)
}
数据
sensor_1,1547718201,35.8
sensor_2,1547718193,45.7
sensor_1,1547718197,32.5
sensor_2,1547718198,50.8
sensor_1,1547718200,36.7
sensor_1,1547718202,58.8
sensor_2,1547718205,35.8
结果:
若有问题,欢迎留言交流。