1. 程式人生 > >Flink ---視窗機制

Flink ---視窗機制

背景

  在流計算中,資料流是無限的,無法直接進行計算,因此Flink提出了window的概念(若干元素的集合)作為流計算的基本單元進行資料處理。

視窗機制

  視窗機制實質上是Flink的運算元operator對資料流的處理過程:資料流如何被拆分成window,何時觸發計算邏輯等,如下圖所示。
處理過程: 當資料流中的元素到達運算元operator後,首先由WindowAssigner決定將該元素分配到哪個視窗(包括建立視窗);每個視窗對應一個Trigger,當有新的元素插入或者定時器超時後,如果存在Evictor則通過它對視窗中的元素進行過濾;否則,直接呼叫視窗函式進行邏輯計算輸出結果;
在這裡插入圖片描述

關鍵元件

  1. WindowAssigner
    作用:將流中的元素分配到對應視窗(零個或多個);

A WindowAssigner assigns zero or more Windows to an element;

  1. Window
    作用:若干元素的集合;
    建立:資料流的元素到達時由WindowAssigner分配和建立;

Generally speaking, a window defines a finite set of elements on an unbounded stream. This set can be based on time (as in our previous examples), element counts, a combination of counts and time, or some custom logic to assign elements to windows.

  1. Trigger
    作用:決定視窗何時被計算或清除;
    特點:每個視窗都繫結一個Trigger;
    觸發時機:有新的元素插入或者Trigger上的定時器超時;
    執行動作:continue(不做任何操作),fire(處理視窗資料),purge(移除視窗和視窗中的資料),或者 fire + purge;

A Trigger determines when a pane of a window should be evaluated to emit the results for that part of the window.

  1. Evictor(可選
    作用:過濾視窗中的元素,相當於filter;
    執行時機:trigger後,window function後;

An Evictor can remove elements from a pane before/after the evaluation of WindowFunction and after the window evaluation gets triggered by a Trigger;

  1. Window Functions
    作用:具體的處理邏輯;

參考:

  1. 雲邪部落格:http://wuchong.me/blog/2016/05/25/flink-internals-window-mechanism/
  2. 官網:https://flink.apache.org/news/2015/12/04/Introducing-windows.html
  3. https://ci.apache.org/projects/flink/flink-docs-release-1.7/dev/stream/operators/windows.html#windows