2017-06-05 32 views
0

基於以下文件「全局窗口分配器分配使用相同的密鑰相同的單一全局窗口中的所有元素」GlobalWindows在Flink中的相同GlobalWindows中爲相同的鍵分配元素?

https://ci.apache.org/projects/flink/flink-docs-release-1.2/dev/windows.html

然後我檢查了源代碼,發現GlobalWindows的assignWindows方法只返回全球Window並沒有爲參數元素做任何事情,那麼所有具有相同鍵的元素如何到同一個全局窗口?

https://github.com/apache/flink/blob/12b4185c6c09101b64e12a84c33dc4d28f95cff9/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/windowing/assigners/GlobalWindows.java

@Override 
public Collection<GlobalWindow> assignWindows(Object element, long timestamp, WindowAssignerContext context) { 
    return Collections.singletonList(GlobalWindow.get()); 
} 

回答

2

在弗林克,窗口和鍵在很大程度上是相互獨立的。流元素可以按鍵和窗口分組,這些是正交的維度。 (當我們想談論窗口與密鑰的組合時,這稱爲窗格。)

窗口實例沒有密鑰,也沒有窗口分配器。相反,鍵和鍵分區狀態是評估窗口的運行時上下文的一部分。

當我試圖瞭解按鍵與窗口分配器的關係時,我發現通過WindowOperator's implementation of processElement來讀取它很有幫助。這個代碼在每個流元素到達窗口操作符時被調用。注重關鍵的作用,同時留出了很多其他的細節,我們看到:

public void processElement(StreamRecord<IN> element) throws Exception { 
    final Collection<W> elementWindows = windowAssigner.assignWindows(
     element.getValue(), element.getTimestamp(), windowAssignerContext); 

    ... 

    final K key = this.<K>getKeyedStateBackend().getCurrentKey(); 

    ... 

    for (W window: elementWindows) { 

     ... 

     windowState.add(element.getValue()); 

     triggerContext.key = key; 
     triggerContext.window = window; 

     TriggerResult triggerResult = triggerContext.onElement(element); 
     if (triggerResult.isFire()) { 
      ... 
      emitWindowContents(window, contents); 
     } 

     ... 
    } 
} 

在這裏你可以看到,關鍵是可以通過getKeyedStateBackend()的窗口操作,但是,這不是」 t甚至可以從窗口分配器獲取這個元素的窗口之後進行檢索。窗口分配器完成它的工作,而不用擔心鍵。

儘管如此,鍵可以通過觸發上下文提供給觸發器。

+0

非常感謝你,所以首先來到的元素通過windowAssigner.assignWindows獲得它自己的窗口或窗口,然後通過getKeyedStateBackend獲得關鍵字,然後通過windowState.add(element.getValue( )); ,我的理解是正確的? –

+0

命名空間又有什麼意義?爲什麼我們需要這個語句windowState.setCurrentNamespace(window);? –

相關問題