DataStream
1. map/flatMap
- MapFunction<T, O>
O map(T value) throws Exception;
輸入一個(gè)元素六荒,輸出一個(gè)元素护姆,其中包含了變換邏輯
- RichMapFunction<IN, OUT>
繼承了MapFunction,可以獲取RuntimeContext掏击,用于查詢當(dāng)前算子當(dāng)前并發(fā)的運(yùn)行狀態(tài)卵皂、accumulator以及broadcast variables等。
- FlatMapFunction<T, O>
void flatMap(T value, Collector<O> out) throws Exception;
輸入一個(gè)元素砚亭,輸出若干個(gè)元素(可以是0個(gè))灯变,其中包含了變換邏輯
- RichFlatMapFunction<IN, OUT>
類似RichMapFunction
2. filter
-
FilterFunction<T>
boolean filter(T value) throws Exception;
包含篩選邏輯,需要保留的返回true捅膘,否則返回false
3. process
- ProcessFunction<I, O>
void processElement(I value, Context ctx, Collector<O> out)
用于處理數(shù)據(jù)添祸,out用于向下游發(fā)射數(shù)據(jù),ctx則用于查詢時(shí)間戳寻仗、注冊(cè)TimerService等等刃泌,也可以獲取state用于暫時(shí)存儲(chǔ)數(shù)據(jù)
onTimer(long timestamp, OnTimerContext ctx, Collector<O> out)
注冊(cè)了TimerService后(同一個(gè)時(shí)間戳多次注冊(cè)只會(huì)觸發(fā)一次),當(dāng)watermark沒過這個(gè)時(shí)間戳?xí)r,就會(huì)觸發(fā)事件耙替,調(diào)用onTimer方法亚侠,可以執(zhí)行一些邏輯,比如把統(tǒng)計(jì)的結(jié)果合并成一條記錄俗扇,用out輸出等等
4. project
- <R extends Tuple> SingleOutputStreamOperator<R> project(int... fieldIndexes)
只有Tuple才能這樣操作硝烂,就是將原來的Tuple映射成新的Tuple,fieldIndexes表示原來的Tuple中的數(shù)據(jù)的索引铜幽,取出的數(shù)據(jù)按fieldIndexes的順序滞谢,排成新的Tuple
5. windowAll / countWindowAll / timeWindowAll
表示以不同的方式獲取不分key的AllWindowedStream
6. addSink / print/printToErr / writeAsText / writeAsText / writeToSocket / writeUsingOutputFormat
各種花式輸出~
AllWindowedStream
1. reduce
將一個(gè)流的一個(gè)window的數(shù)據(jù)聚合成一個(gè)數(shù)據(jù),數(shù)據(jù)類型一致
- ReduceFunction<T>
T reduce(T value1, T value2) throws Exception;
輸入兩個(gè)數(shù)據(jù)除抛,輸出一個(gè)數(shù)據(jù)狮杨,其中包含了歸并的邏輯,算子會(huì)不斷重復(fù)镶殷,直至剩下一個(gè)元素禾酱。用戶需要自己保證reduce方法的結(jié)果與元素的處理先后微酬、組合方式無關(guān)绘趋。
2. aggregate
將一個(gè)窗口的數(shù)據(jù)聚合成一條,與reduce類似颗管,但是更靈活
- AggregateFunction<IN, ACC, OUT>
ACC createAccumulator();
創(chuàng)建一個(gè)累加器陷遮,用于保存狀態(tài),最好是增量的垦江,可以節(jié)約存儲(chǔ)帽馋,不用保存所有記錄
ACC add(IN value, ACC accumulator);
增加一個(gè)元素
OUT getResult(ACC accumulator);
從accumulator中獲得輸出元素
ACC merge(ACC a, ACC b);
用于合并accumulator,復(fù)用對(duì)象比吭,調(diào)用這個(gè)方法后绽族,之前的accumulator就不再用了
- AllWindowFunction<IN, OUT, W extends Window>
void apply(W window, Iterable<IN> values, Collector<OUT> out) throws Exception;
用于將AggregateFunction中合并得到的的OUT 數(shù)據(jù)通過out輸出。這里的IN是AggregateFunction的OUT
- ProcessAllWindowFunction<IN, OUT, W extends Window>
public abstract void process(Context context, Iterable<IN> elements, Collector<OUT> out) throws Exception;
與AllWindowFunction類似衩藤,但是多了可以使用context的功能
3. process
也使用ProcessAllWindowFunction吧慢,與aggregate不同的是,處理的是窗口中的每一個(gè)元素赏表,而不是聚合后的元素
4. apply
也使用ReduceFunction检诗、AllWindowFunction,與reduce瓢剿、aggregate不同的是逢慌,處理的是窗口中的每一個(gè)元素,而不是聚合后的元素
5. fold
FoldFunction<O, T>
T fold(T accumulator, O value) throws Exception;
與reduce间狂、aggregate類似攻泼,把每個(gè)數(shù)據(jù)都?xì)w并到一個(gè)accumulator中去,最后產(chǎn)生一個(gè)輸出數(shù)據(jù)
6. sum / min / max / minBy / maxBy
一些預(yù)定義好的聚合方法,按字面意思
7. sideOutputLateData
sideOutputLateData(OutputTag<T> outputTag)
將遲到的數(shù)據(jù)輸出坠韩,outputTag是輸出流的tag
可以通過SingleOutputStreamOperator#getSideOutput(OutputTag)來獲得遲到數(shù)據(jù)的流
KeyedStream
1. reduce
2. aggregate
3. process
4. fold
5. sum / min / max / minBy / maxBy
6. window / countWindow / timeWindow
方法的作用與DataStream類似距潘,表示以不同的方式獲取分key的WindowedStream
WindowedStream
1. reduce
2. aggregate
3. process
4. apply
5. fold
6. sum / min / max / minBy / maxBy
7. sideOutputLateData
方法的作用與AllWindowStream類似,只是作用于某個(gè)pane(也就是window中單獨(dú)的key的數(shù)據(jù))