flink的8个底层ProcessFunction,带示例

4 篇文章 0 订阅

8个底层ProcessFunction

  • ProcessFunction
  • KeyedProcessFunction
  • CoProcessFunction
  • ProcessJoinFunction
  • BroadcastProcessFunction
  • KeyedBroadcastProcessFunction
  • ProcessWindowFunction
  • ProcessAllWindowFunction

KeyedProcessFunction:10秒内温度连续上升报警

package FlinkProject.runMain

import FlinkProject.utils.sensor
import org.apache.flink.api.common.serialization.SimpleStringSchema
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.streaming.connectors.kafka.FlinkKafkaConsumer
import org.apache.flink.util.Collector

import java.util.Properties

object ProcessFunctionTest {

  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    val prop = new Properties()
    prop.setProperty("bootstrap.servers", "localhost:9092")
    prop.setProperty("group.id", "group_id")
    val stream: DataStream[String] = env.addSource(new FlinkKafkaConsumer[String]("flink", new SimpleStringSchema(), prop))
    val result: DataStream[String] = stream.filter((_: String).nonEmpty).map((x: String) => {
      val data: Array[String] = x.split(",")
      sensor(data(0), data(1).toLong, data(2).toDouble)
    }).keyBy((_: sensor).id)
      .process(new myKeyedProcessFunction(10000L)) // 毫秒
    result.print()
    env.execute()
  }
}

class myKeyedProcessFunction(interval: Long) extends KeyedProcessFunction[String, sensor, String] {
  //todo 注册定时器的时间戳,默认值0
  lazy val timer: ValueState[Long] = getRuntimeContext.getState(new ValueStateDescriptor[Long]("timer", classOf[Long], 0))
  //todo 保存上一个状态的温度值,给一个很低不影响结果的初始值
  lazy val lastTempState: ValueState[Double] = getRuntimeContext.getState(new ValueStateDescriptor[Double]("temp", classOf[Double], -1234567876))

  override def processElement(value: sensor, ctx: KeyedProcessFunction[String, sensor, String]#Context, out: Collector[String]): Unit = {
    //todo 当前温度和上一次温度进行判断
    if (value.temperature > lastTempState.value() && timer.value() == 0) {
      //todo 注册定时器
      val ts: Long = ctx.timerService().currentProcessingTime() + interval
      ctx.timerService().registerProcessingTimeTimer(ts)
      //todo 更新状态
      timer.update(ts)
      //      lastTempState.update(value.temperature)
      //todo 如果温度下降:删除定时器且清空时间状态
    } else if (value.temperature < lastTempState.value()) {
      // 如果温度下降
      ctx.timerService().deleteEventTimeTimer(timer.value())
      // 不要用timer.clear(),当清零时变成默认值0会调用定时器
      timer.update(ctx.timerService().currentProcessingTime() + interval)
      //      lastTempState.update(value.temperature)
    }
    lastTempState.update(value.temperature)
  }

  override def onTimer(timestamp: Long, ctx: KeyedProcessFunction[String, sensor, String]#OnTimerContext, out: Collector[String]): Unit = {
    out.collect(timer.value() / 1000 + ">  sensor:" + ctx.getCurrentKey + "连续" + interval / 1000 + "秒连续上升")
    timer.clear()
  }

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值