簡體   English   中英

如何在Google Dataflow中創建個性化WindowFn

[英]How to create a personalised WindowFn in google dataflow

我想以不同的方式創建一個不同的WindowFn ,以便根據另一個字段而不是根據輸入條目的時間戳將Windows分配給我的任何輸入元素。 我知道Google DataFlow SDK中的預定義WindowFn使用時間戳作為分配窗口的條件。

更具體地說,我想創建一種SlidingWindowsSlidingWindows將時間戳記作為Window分配標准,不如將其他字段視為該條件。

如何創建自定義的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.
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM