Windowing is a fundamental concept in stream processing that allows you to group a continuous stream of events into finite chunks for processing. Apache Flink provides powerful windowing capabilities that support various window types and triggers for flexible, real-time data analysis.
Source: Apache Flink
Types of Windows in Flink
Tumbling Windows
Tumbling windows are fixed-size, non-overlapping windows. Each event belongs to exactly one window.
DataStream<Event> stream = ...; // your event stream
DataStream<WindowedEvent> windowedStream = stream
.keyBy(event -> event.getKey())
.window(TumblingEventTimeWindows.of(Time.seconds(10)))
.apply(new WindowFunction<Event, WindowedEvent, Key, TimeWindow>() {
@Override
public void apply(Key key, TimeWindow window, Iterable<Event> input, Collector<WindowedEvent> out) {
// Window processing logic
}
});
Sliding Windows
Sliding windows are also fixed-size but can overlap. Each event can belong to multiple windows depending on the slide interval.
DataStream<Event> stream = ...;
DataStream<WindowedEvent> windowedStream = stream
.keyBy(event -> event.getKey())
.window(SlidingEventTimeWindows.of(Time.seconds(10), Time.seconds(5)))
.apply(new WindowFunction<Event, WindowedEvent, Key, TimeWindow>() {
@Override
public void apply(Key key, TimeWindow window, Iterable<Event> input, Collector<WindowedEvent> out) {
// Window processing logic
}
});