在弗林克,窗口和键在很大程度上是相互独立的。流元素可以按键和窗口分组,这些是正交的维度。 (当我们想谈论窗口与密钥的组合时,这称为窗格。)
窗口实例没有密钥,也没有窗口分配器。相反,键和键分区状态是评估窗口的运行时上下文的一部分。
当我试图了解按键与窗口分配器的关系时,我发现通过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甚至可以从窗口分配器获取这个元素的窗口之后进行检索。窗口分配器完成它的工作,而不用担心键。
尽管如此,键可以通过触发上下文提供给触发器。
非常感谢你,所以首先来到的元素通过windowAssigner.assignWindows获得它自己的窗口或窗口,然后通过getKeyedStateBackend获得关键字,然后通过windowState.add(element.getValue( )); ,我的理解是正确的? –
命名空间又有什么意义?为什么我们需要这个语句windowState.setCurrentNamespace(window);? –