我想创建一个不同的 WindowFn
,以便根据另一个字段而不是根据我的输入条目的时间戳将 Windows 分配给我的任何输入元素。我知道 Google DataFlow SDK 中的预定义 WindowFn
使用时间戳作为分配窗口的标准。
更具体地说,我想创建一种 SlidingWindows
,但我不想将时间戳视为窗口分配标准,而是将另一个字段视为该标准。
如何创建自定义的 WindowFn
?创建自己的 WindowFn
时应该考虑哪些要点?
谢谢。
最佳答案
要创建新的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.
}
}
关于java - 如何在谷歌数据流中创建个性化WindowFn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37897452/