Flink UI部署jar包报错

错误描述:

通过Flink的UI中的Submit New Job菜单添加jar包的时候提示报错。报错信息的关键字是“The LocalStreamEnvironment cannot be used when submitting a program through a client, or running in a TestEnvironment context”,最关键的是“LocalStreamEnvironment”。

我的Flink服务是单机版启动。

问题原因就是以下这两行代码的区别,报错是因为我用的是“createLocalEnvironment()”

ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
// ExecutionEnvironment env = ExecutionEnvironment.createLocalEnvironment();

// getExecutionEnvironment()方法可以根据flink运用程序如何提交判断出是那种模式提交,Local本地提交,Cluster标是standalone提交,Yarn提交好像是YarnCluster
        

解决办法:使用ExecutionEnvironment.getExecutionEnvironment()

//StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment();
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

以下是详细的内容。

FlinkUI中点击Submit后,弹出的Message完整异常信息参考:

org.apache.flink.runtime.rest.handler.RestHandlerException: Could not execute application. 
at org.apache.flink.runtime.webmonitor.handlers.JarRunHandler.lambda$handleRequest$1(JarRunHandler.java:110) 
at java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:822) 
at java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:797) 
at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474) 
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1595) 
at java.lang.Thread.run(Thread.java:748) Caused by: java.util.concurrent.CompletionException: org.apache.flink.util.FlinkRuntimeException: Could not execute application. 
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280) 
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1592) 
... 1 more Caused by: org.apache.flink.util.FlinkRuntimeException: Could not execute application. 
at org.apache.flink.client.deployment.application.DetachedApplicationRunner.tryExecuteJobs(DetachedApplicationRunner.java:88) 
at org.apache.flink.client.deployment.application.DetachedApplicationRunner.run(DetachedApplicationRunner.java:70) 
at org.apache.flink.runtime.webmonitor.handlers.JarRunHandler.lambda$handleRequest$0(JarRunHandler.java:104) 
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590) 
... 1 more Caused by: org.apache.flink.client.program.ProgramInvocationException: The main method caused an error: The LocalStreamEnvironment cannot be used when submitting a program through a client, or running in a TestEnvironment context. 
at org.apache.flink.client.program.PackagedProgram.callMainMethod(PackagedProgram.java:372) 
at org.apache.flink.client.program.PackagedProgram.invokeInteractiveModeForExecution(PackagedProgram.java:222) 
at org.apache.flink.client.ClientUtils.executeProgram(ClientUtils.java:114) 
at org.apache.flink.client.deployment.application.DetachedApplicationRunner.tryExecuteJobs(DetachedApplicationRunner.java:84) 
... 4 more Caused by: org.apache.flink.api.common.InvalidProgramException: The LocalStreamEnvironment cannot be used when submitting a program through a client, or running in a TestEnvironment context. 
at org.apache.flink.streaming.api.environment.LocalStreamEnvironment.validateAndGetConfiguration(LocalStreamEnvironment.java:57) 
at org.apache.flink.streaming.api.environment.LocalStreamEnvironment.(LocalStreamEnvironment.java:52) 
at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.createLocalEnvironment(StreamExecutionEnvironment.java:2259) 
at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.createLocalEnvironment(StreamExecutionEnvironment.java:2247) 
at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.createLocalEnvironment(StreamExecutionEnvironment.java:2230) 
at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.createLocalEnvironment(StreamExecutionEnvironment.java:2218) 
at com.xx.XXApplication.main(XXApplication.java:39) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at org.apache.flink.client.program.PackagedProgram.callMainMethod(PackagedProgram.java:355)

其实异常信息里面已经提示了关键信息,但是初次接触Flink没有意识到,就是里面的

at com.xx.XXApplication.main(XXApplication.java:39) 

39行的代码就是

StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment();

后来参考的网上的信息才意识到问题的根本原因。记录一下,以供参考。

网上还有很多类似的异常信息,需要仔细查看里面的关键字,一定要仔细,问题的原因就在异常信息里面。

以下信息给了我启发

“The RemoteEnvironment cannot be used when submitting a program through a client, or running in a TestEnvironment context.” 这个异常的关键字是“RemoteEnvironment ”

stackoverflow上的一个问答是关于这个异常的,给了我参考。

https://stackoverflow.com/questions/62876301/the-remoteenvironment-cannot-be-used-when-submitting-a-program-through-a-client

你可能感兴趣的:(知新,flink,jar,java)