flink(scala)分组计算并在每条数据前贴上当前时间

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

结果:
在这里插入图片描述
若有问题,欢迎留言交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值