运行 flink demo 的时候,我第一个问题是,需要安装 flink ,运行在 flink 环境呢,还是在idea里写好code 直接run 就可以呢?
自己写了个demo 发现直接run 也是可以的。
看官网文档的时候,找到了原因。
程序中获取执行环境时候 调用的是 getExecutionEnvironment(),它可以获取到你执行应用的上下文。
如果是通过IDE 执行,会生成一个本地环境 它会在你的本机执行你的程序。
如果是通过命令行 执行你的 jar ,flink 集群manager 会执行你的程序,通过getExecutionEnvironment() 获取到在集群上执行程序 的执行环境 。
官方文档如下:
The StreamExecutionEnvironment
is the basis for all Flink programs. You can obtain one using these static methods on StreamExecutionEnvironment
:
getExecutionEnvironment()
createLocalEnvironment()
createRemoteEnvironment(String host, int port, String... jarFiles)
Typically, you only need to use getExecutionEnvironment()
, since this will do the right thing depending on the context: if you are executing your program inside an IDE or as a regular Java program it will create a local environment that will execute your program on your local machine. If you created a JAR file from your program, and invoke it through the command line, the Flink cluster manager will execute your main method and getExecutionEnvironment()
will return an execution environment for executing your program on a cluster.
参考地址
https://ci.apache.org/projects/flink/flink-docs-release-1.8/dev/api_concepts.html
网友评论