// 获取运行环境
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
这行代码会返回一个可用的执行环境,是flink程序执行的上下文,记录了相关配,如并行度等,并提供了一系列方法,如输入流的读入方法,运行整个程序的execute方法等,对于分步式流处理程序来说,flatMap,keyBy等等操作,都可以理解为一种声明,告诉整个程序采用了什么样的算子(这段文字参考自https://www.cnblogs.com/bethunebtj/p/9168274.html),接下来我们开始进入到代码内部,看看运行环境的获取过程。
代码讲解
我们开始看代码:
/**
* Creates an execution environment that represents the context in which the
* program is currently executed. If the program is invoked standalone, this
* method returns a local execution environment, as returned by
* {@link #createLocalEnvironment()}.
*
* @return The execution environment of the context in which the program is
* executed.
*/
public static StreamExecutionEnvironment getExecutionEnvironment() {
return Utils.resolveFactory(threadLocalContextEnvironmentFactory, contextEnvironmentFactory)
.map(StreamExecutionEnvironmentFactory::createExecutionEnvironment)
.orElseGet(StreamExecutionEnvironment::createStreamExecutionEnvironment);
}
其中threadLocalContextEnvironmentFactory的定义如下:
/** The ThreadLocal used to store {@link StreamExecutionEnviro