一個Flink作業(yè)阶剑,從client提交到真正的執(zhí)行趾诗,其 Graph 的轉(zhuǎn)換會經(jīng)過下面三個階段(第四個階段是作業(yè)真正執(zhí)行時的狀態(tài)句旱,都是以 task 的形式在 TM 中運(yùn)行):
StreamGraph:根據(jù)編寫的代碼生成最初的 Graph魄眉,它表示最初的拓?fù)浣Y(jié)構(gòu);
JobGraph:這里會對前面生成的 Graph燃逻,做一些優(yōu)化操作(比如: operator chain 等)序目,最后會提交給 JobManager;
ExecutionGraph:JobManager 根據(jù) JobGraph 生成 ExecutionGraph伯襟,是 Flink 調(diào)度時依賴的核心數(shù)據(jù)結(jié)構(gòu)猿涨;
物理執(zhí)行圖:JobManager 根據(jù)生成的 ExecutionGraph 對 Job 進(jìn)行調(diào)度后,在各個 TM 上部署 Task 后形成的一張?zhí)摂M圖姆怪。
首先要熟悉以下概念:DataStream叛赚、Transformation、StreamOperator稽揭、Function
DataStream
DateStream實際上就是對相同類型的數(shù)據(jù)流操作進(jìn)行的封裝俺附,主要是通過Transformations將數(shù)據(jù)流轉(zhuǎn)換成另一個流,常用的api:
keyBy()溪掀、join()事镣、union()、map()揪胃、filter()璃哟、flatMap()等。
Transformation
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// get input data by connecting to the socket
DataStream<String> text = env.socketTextStream(hostname, port, "\n");
// parse the data, group it, window it, and aggregate the counts
DataStream<WordWithCount> windowCounts =
text.flatMap(
new FlatMapFunction<String, WordWithCount>() {
@Override
public void flatMap(
String value, Collector<WordWithCount> out) {
for (String word : value.split("\\s")) {
out.collect(new WordWithCount(word, 1L));
}
}
})
.keyBy(value -> value.word)
.window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
.reduce(
new ReduceFunction<WordWithCount>() {
@Override
public WordWithCount reduce(WordWithCount a, WordWithCount b) {
return new WordWithCount(a.word, a.count + b.count);
}
});
// print the results with a single thread, rather than in parallel
windowCounts.print().setParallelism(1);
env.execute("Socket Window WordCount");
首先看env.socketTextStream()只嚣,調(diào)用了StreamExecutionEnvironment的addSource()方法沮稚,會講SourceFunction封裝到StreamSource中,之后會講StreamSource封裝到SourceTransformation中册舞。所以最終執(zhí)行業(yè)務(wù)核心邏輯的是各種Function,經(jīng)過各種轉(zhuǎn)換會生成對應(yīng)的Transformation障般。
private <OUT> DataStreamSource<OUT> addSource(
final SourceFunction<OUT> function,
final String sourceName,
@Nullable final TypeInformation<OUT> typeInfo,
final Boundedness boundedness) {
checkNotNull(function);
checkNotNull(sourceName);
checkNotNull(boundedness);
TypeInformation<OUT> resolvedTypeInfo =
getTypeInfo(function, sourceName, SourceFunction.class, typeInfo);
boolean isParallel = function instanceof ParallelSourceFunction;
clean(function);
final StreamSource<OUT, ?> sourceOperator = new StreamSource<>(function);
//DataStreamSource 其實是SingleOutputStreamOperator的子類
return new DataStreamSource<>(
this, resolvedTypeInfo, sourceOperator, isParallel, sourceName, boundedness);
}
再看flatMap()操作调鲸,其實和上述類似,會將Function封裝成StreamOperator挽荡,再封裝成Transformation藐石,最終都返回?fù)碛挟?dāng)前算子和環(huán)境的DataStream對象,OneInputTransformation算法擁有當(dāng)前算子的上游算子對象定拟。整個封裝過程Function->StreamOperator->Transformation
public <R> SingleOutputStreamOperator<R> flatMap(
FlatMapFunction<T, R> flatMapper, TypeInformation<R> outputType) {
return transform("Flat Map", outputType, new StreamFlatMap<>(clean(flatMapper)));
}
protected <R> SingleOutputStreamOperator<R> doTransform(
String operatorName,
TypeInformation<R> outTypeInfo,
StreamOperatorFactory<R> operatorFactory) {
// read the output type of the input Transform to coax out errors about MissingTypeInfo
transformation.getOutputType();
OneInputTransformation<T, R> resultTransform =
new OneInputTransformation<>(
/**
* han_pf
* 記錄當(dāng)前transformation的輸入transformation
*/
this.transformation,
operatorName,
operatorFactory,
outTypeInfo,
environment.getParallelism());
@SuppressWarnings({"unchecked", "rawtypes"})
SingleOutputStreamOperator<R> returnStream =
new SingleOutputStreamOperator(environment, resultTransform);
/**
* han_pf
* 存儲所有的stransformation到env中于微。
*/
getExecutionEnvironment().addOperator(resultTransform);
return returnStream;
}
StreamOperator
Operator 基類的是 StreamOperator逗嫡,它表示的是對 Stream 的一個 operation,它主要的實現(xiàn)類如下:
AbstractUdfStreamOperator:會封裝一個 Function株依,真正的操作是在 Function 中的實現(xiàn)驱证。
OneInputStreamOperator:如果這個 Operator 只有一個輸入,實現(xiàn)這個接口即可恋腕,processElement() 方法需要自己去實現(xiàn)抹锄,主要做業(yè)務(wù)邏輯的處理;
TwoInputStreamOperator:如果這個 Operator 是一個二元操作符荠藤,是對兩個流的處理伙单,比如:雙流 join,那么實現(xiàn)這個接口即可哈肖,自己去實現(xiàn) processElement1() 和 processElement2() 方法吻育。
Function
Function 是 Transformation 最底層的封裝,用戶真正的處理邏輯是在這個里面實現(xiàn)的淤井,包括前面示例中實現(xiàn)的 FlatMapFunction 對象等扫沼。
以上熟悉各個概念之后,以及各個類之間的關(guān)系庄吼,對于后邊StreamGraph的生成過程有極大的幫助缎除。