package com.claroja.watermark;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
import java.sql.Timestamp;
import java.time.Duration;
public class EventTime {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
DataStreamSource<String> stream = env.socketTextStream("localhost", 9999);
stream
.map(r -> Tuple2.of(r.split(" ")[0], Long.parseLong(r.split(" ")[1]) * 1000L))
.returns(new TypeHint<Tuple2<String, Long>>() {})
.assignTimestampsAndWatermarks(
WatermarkStrategy.<Tuple2<String, Long>>forBoundedOutOfOrderness(Duration.ofSeconds(0))
.withTimestampAssigner(new SerializableTimestampAssigner<Tuple2<String, Long>>() {
@Override
public long extractTimestamp(Tuple2<String, Long> stringLongTuple2, long l) {
return stringLongTuple2.f1;
}
})
)
.keyBy(r -> r.f0)
.process(new Keyed())
.print();
env.execute();
}
public static class Keyed extends KeyedProcessFunction<String, Tuple2<String, Long>, String> {
//时间携带的时间戳 加上x秒,触发onTimer,则必须让水位线超过 stringLongTuple2.f1 + 5 * 1000L,既onTimer的第一个参数
@Override
public void processElement(Tuple2<String, Long> stringLongTuple2, Context context, Collector<String> collector) throws Exception {
context.timerService().registerEventTimeTimer(stringLongTuple2.f1 + 5 * 1000L); //根据事件事件触发
// context.timerService().registerProcessingTimeTimer(context.timerService().currentProcessingTime()+ 5 * 1000L); //根据机器时间触发
}
@Override
public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
super.onTimer(timestamp, ctx, out);
out.collect("时间戳是:" + new Timestamp(timestamp) + " 的定时器触发了!"); //事件事件触发
// out.collect("定时器执行的时间是" + new Timestamp(timestamp)); //机器时间触发
}
}
}
Flink EventTime
最新推荐文章于 2025-01-06 17:20:43 发布