Class SlidingWindows


  • public final class SlidingWindows
    extends Object
    A sliding window used for aggregating events.

    Sliding Windows are defined based on a record's timestamp, the window size based on the given maximum time difference (inclusive) between records in the same window, and the given window grace period. While the window is sliding over the input data stream, a new window is created each time a record enters the sliding window or a record drops out of the sliding window.

    Records that come after set grace period will be ignored, i.e., a window is closed when stream-time > window-end + grace-period.

    For example, if we have a time difference of 5000ms and the following data arrives:

     +--------------------------------------+
     |    key    |    value    |    time    |
     +-----------+-------------+------------+
     |    A      |     1       |    8000    |
     +-----------+-------------+------------+
     |    A      |     2       |    9200    |
     +-----------+-------------+------------+
     |    A      |     3       |    12400   |
     +-----------+-------------+------------+
     
    We'd have the following 5 windows:
    • window [3000;8000] contains [1] (created when first record enters the window)
    • window [4200;9200] contains [1,2] (created when second record enters the window)
    • window [7400;12400] contains [1,2,3] (created when third record enters the window)
    • window [8001;13001] contains [2,3] (created when the first record drops out of the window)
    • window [9201;14201] contains [3] (created when the second record drops out of the window)

    Note that while SlidingWindows are of a fixed size, as are TimeWindows, the start and end points of the window depend on when events occur in the stream (i.e., event timestamps), similar to SessionWindows.

    For time semantics, see TimestampExtractor.

    See Also:
    TimeWindows, SessionWindows, UnlimitedWindows, JoinWindows, KGroupedStream.windowedBy(SlidingWindows), CogroupedKStream.windowedBy(SlidingWindows), TimestampExtractor
    • Method Detail

      • withTimeDifferenceAndGrace

        public static SlidingWindows withTimeDifferenceAndGrace​(Duration timeDifference,
                                                                Duration grace)
                                                         throws IllegalArgumentException
        Return a window definition with the window size based on the given maximum time difference (inclusive) between records in the same window and given window grace period. Reject out-of-order events that arrive after grace. A window is closed when stream-time > window-end + grace-period.
        Parameters:
        timeDifference - the max time difference (inclusive) between two records in a window
        grace - the grace period to admit out-of-order events to a window
        Returns:
        a new window definition
        Throws:
        IllegalArgumentException - if the specified window size is < 0 or grace < 0, or either can't be represented as long milliseconds
      • timeDifferenceMs

        public long timeDifferenceMs()
      • gracePeriodMs

        public long gracePeriodMs()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object