簡體   English   中英

多個時間窗口的唯一計數 - 處理或減少 function 與 ProcessWindowFunction 結合?

[英]Unique Count for Multiple timewindows - Process or Reduce function combined with ProcessWindowFunction?

我們需要在多個時間窗口的輸入 stream 中找到唯一元素的數量。 輸入數據 Object 的定義如下 InputData(ele1: Integer,ele2: String,ele3: String) Stream 由 ele1 和 ele2 鍵入。要求是在過去 1 小時、過去 12 小時和 24 小時內查找唯一 ele3 的數量小時,結果應每 15 分鍾刷新一次。 我們使用 SlidingTimewindow,滑動間隔為 15 分鍾,Streaming 間隔為 1,12 和 24。 輸入流多窗口處理

由於我們需要找到唯一元素,我們使用進程 function 作為 window function,它將存儲每個鍵的所有元素(事件)直到 window 結束以處理和計算唯一元素。我們認為可以優化其memory消費

取而代之的是,我們嘗試使用減少function和流程function,以增量匯總,以在減少function中存儲獨特的元素,然后計算882347039191919191919191919191919191919788888888888888888888888888888888888888888888888888.8888888888888888.8888888888888888888.8888888888888888888.488點flink/flink-docs-master/docs/dev/datastream/operators/windows/#processwindowfunction-with-incremental-aggregation

public class UserDataReducer implements ReduceFunction<UserData> {
    @Override
   public UserData reduce(UserData u1, UserData u2) {
        u1.getElement3().addAll(u2.getElement3());
        return new UserData.Builder(u1.getElement1(), u1.getElement2(),)
                .withUniqueUsers(u1.geElement3())
                .createUserData();
}
}

public class UserDataProcessor extends ProcessWindowFunction<UserData,Metrics,
                                         Tuple2<Integer, String>,TimeWindow> {
 @Override
    public void process(Tuple2<Integer, String> key,
                        ProcessWindowFunction<UserData, Metrics, Tuple2<Integer, String>, TimeWindow>.Context context,
                        Iterable<UserData> elements,
                        Collector<Metrics> out) throws Exception {
        if (Objects.nonNull(elements.iterator().next())) {
            UserData aggregatedUserAttribution = elements.iterator().next();
            out.collect(new Metrics(
                    key.ele1,
                    key.ele2,
                    aggregatedUserAttribution.getElement3().size(),
                    ));
        }
    }
    }

我們預計堆 memory 的消耗會減少,因為我們現在每張幻燈片每個鍵只存儲一個 object 作為 state。但是堆 memory 的消耗沒有減少,它幾乎相同或更高。 我們在新進程的 heapdump 中觀察到大量 hashmap 個實例,消耗的 memory 比輸入數據對象占用的要多,在較早的作業中。

解決這個問題的最佳方法是什么? 處理 function 或結合減少和處理 function 的增量聚合?

State 后端:Hashmap Flink 版本:1.14.2 on Yarn

在這種情況下,我不確定部分聚合是否會減少堆大小。 它應該允許您根據數據集的唯一性將 state 的大小減少一些因素。 這是因為(據我所知)您正在為分配給 window 的每個元素有效地復制HashSet ,而當它們被垃圾收集時,它不會立即發生,因此您會在中看到相當多的HashSets堆轉儲。

總體而言, ProcessFunction很可能會生成更大的 state,但就堆大小而言,它們可能與您注意到的非常相似。

您可能會考慮的一件事是嘗試應用更高級的處理。 您可以嘗試閱讀觸發器並嘗試以您將擁有 24 小時 window 的方式實現觸發器,但它會在 y 1 小時、12 小時和 24 小時發出結果(之后 window 將被清除)。 請注意,在這種情況下,您需要在ProcessFunction中做一些工作以確保結果正確。 您可以查看的另一件事是這篇文章

請注意,這兩個建議的解決方案都需要對 Flink 有一定的了解,並且需要對 window 元素進行更多的手動處理。

暫無
暫無

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

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