1 map 或者source時繼承對應的rich function,在其中的open方法中建立連接,對應的close方法中關閉連接。
2 window必須對應keyedstream,也就是說必須在keyby之後。之後在對應相應的增量函數比如ReduceFunction
, AggregateFunction
, FoldFunction
或 ProcessWindowFunction
。前兩個函數執行效率更高,因爲 Flink 可以在每個窗口中元素到達時增量地聚合。後兩個FoldFunction
,ProcessWindowFunction
將獲得一個窗口內所有元素的迭代器以及元素所在窗口的附加元信息。
private static class MyProcessWindowFunction extends
ProcessWindowFunction<Tuple2<String, Integer>, String, String, TimeWindow> {
@Override
public void process(String key, Context context,
Iterable<Tuple2<String, Integer>> input, Collector<String> out) {
3 windowall 對應的是沒有分區的datastream流,其後也可以跟對應的ReduceFunction
, AggregateFunction
, FoldFunction
或 ProcessWindowFunction。
4 每個算子對應的watermark是所有輸入多分區流相應的watermark中最小的一個。(每個輸入的分區流數據都有一個自己的水位)
5 生成watermark也可以自定義內部類,重寫相應的watermark生成函數,比如直接提取日誌時間eventtime減去等待延遲時間爲watermark, 一般繼承的父類有兩種,一類是
AssignerWithPeriodicWatermarks,週期性生成水印,另外一個是AssignerWithPunctuatedWatermarks沒有周期性規律,打斷生成水印。
private static class MyOwnWaterMark implements AssignerWithPeriodicWatermarks<Tuple3<String, Long, Integer>> {
private final long maxOutOfOrderness = 0; // 3 seconds
private long currentMaxTimestamp;
@Override
public long extractTimestamp(Tuple3<String, Long, Integer> element, long previousElementTimestamp) {
return timestamp;
}
@Override
public Watermark getCurrentWatermark() {
// return the watermark as current highest timestamp minus the out-of-orderness bound
return new Watermark(currentMaxTimestamp - maxOutOfOrderness);
}
6 watermark機制一般是爲了window服務的,遇到一個時間戳的信息到達了窗口關閉時間,不應該立即觸發窗口關閉計算,而是等待一段時間,等遲來的數據來了再窗口計算。
watermark表示timestamp小於水位線的數據都已經到達了,因此窗口觸發實際上是由watermark來觸發的。
7 一般比如寫文件的sink task 並行度設置爲1, 前面操作可以多個並行度。寫文件sink如果多個並行度寫一個文件會亂出問題。跟自己寫線程池代碼一樣,多個線程的返回結果同意收集之後再統一寫入一個文件。當然寫入多個文件肯定是沒有影響的。