2017-06-05 87 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);? –

相关问题