Hi, is it possible to get the record's watermark i...
# troubleshooting
c
Hi, is it possible to get the record's watermark in a RichMapFunction?
d
Yes, it’s possible but not directly.
You can get it indirectly by leveraging timers or accessing the Context which can provide the current watermark
via timers. You could create a process function:
Copy code
public class MyProcessFunction extends KeyedProcessFunction<Key, InputType, OutputType> {
    @Override
    public void processElement(InputType value, Context ctx, Collector<OutputType> out) {
        // register a timer to fire at the next watermark
        long timestamp = ctx.timestamp();
        ctx.timerService().registerEventTimeTimer(ctx.timerService().currentWatermark() + 1);
    }

    @Override
    public void onTimer(long timestamp, OnTimerContext ctx, Collector<OutputType> out) {
        // called when the timer fires, effectively at the watermark you set
        // access the current watermark via ctx.timerService().currentWatermark()
    }
}
RichMapFunction is designed for simple transformations and is not really intended to deal with timestamps and watermarks like KeyedProcessFunction or windowing operations. If you need to access a watermark you should consider if RichMapFunction is the right place for this logic or if another type of operator or function might serve your purposes better.