flink事件水位线Watermark样例

code

package com.baiyun.job


import com.alibaba.fastjson.{JSON, JSONObject}
import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.functions.ProcessFunction
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer010
import org.apache.flink.util.Collector
import java.time.{Instant, LocalDateTime, ZoneId}
import java.time.format.DateTimeFormatter
import java.util.Properties


object ReadKF {
  def main(args: Array[String]): Unit = {
    // 创建一个流处理执行环境
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    env.setParallelism(1)
    env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)


    val properties = new Properties()
    properties.setProperty("bootstrap.servers", "hadoop102:9092")
    properties.setProperty("group.id", "consumer-group")
    properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
    properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
    properties.setProperty("auto.offset.reset", "latest")

    val input: DataStream[JSONObject] = env.addSource(new FlinkKafkaConsumer010[String]("test-20230227", new SimpleStringSchema(), properties))
      .map(v => JSON.parseObject(v))
      .assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor[JSONObject](Time.seconds(0)) {
        override def extractTimestamp(t: JSONObject): Long = t.getLong("ts")
      })

    input.process(new ProcessFunction[JSONObject, JSONObject] {
      override def processElement(i: JSONObject, context: ProcessFunction[JSONObject, JSONObject]#Context, collector: Collector[JSONObject]): Unit = {

        val l: Long = context.timerService().currentWatermark()

        val timeFM = DateTimeFormatter
          .ofPattern("yyyy-MM-dd HH:mm:ss")
        val ts = i.getLong("ts")

        val watermark = timeFM.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(l), ZoneId.systemDefault))
        val tsTime = timeFM.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.systemDefault))
        println(i)
        val s =
          s"""
             |   tsTime:$tsTime-$ts
             |watermark:$watermark-$l
             |${"-" * 50}
             |""".stripMargin
        println(s)
      }
    })



    env.execute("ReadKF")

  }

}

log

flink事件水位线Watermark样例_第1张图片

你可能感兴趣的:(实时,flink,kafka,java)