0

我想以WindowFn这种方式创建一个不同的窗口,以基于另一个字段而不是基于我的输入条目的时间戳将 Windows 分配给我的任何输入元素。我知道来自 Google DataFlow SDK 的预定义WindowFn使用时间戳作为分配窗口的标准。

更具体地说,我想创建一种SlidingWindows但不是将时间戳视为窗口分配标准,而是将另一个字段视为该标准。

我怎样才能创建我的自定义WindowFn?创建自己的 时应该考虑哪些要点WindowFn

谢谢。

4

1 回答 1

2

要创建一个新的 WindowFn,您只需要从WindowFn或子类继承并重写各种抽象方法即可。

在您的情况下,您不需要窗口合并,因此您可以从 NonMergingWindowFn 继承,并且您的代码可能看起来像

public class MyWindowFn extends NonMergingWindowFn<ElementT, IntervalWindow> {
  public Collection<W> assignWindows(AssignContext c) {
    return setOfWindowsElementShouldBeIn(c.element());
  }

  public boolean isCompatible(WindowFn other) {
    return other instanceof MyWindowFn;
  }

  public Coder<IntervalWindow> windowCoder() {
    return IntervalWindow.getCoder();
  }

  public W getSideInputWindow(final BoundedWindow window) {
    // You may not need this if you won't ever be using PCollections windowed 
    // with this as side inputs.  If that's the case, just throw.
    // Otherwise you'll need to figure out how to map the main input windows
    // into the windows generated by this WindowFn.
  }
}
于 2016-06-20T17:52:22.220 回答