提交Flink作业时出现java.lang.NoClassDefFoundError

时间:2019-06-08 20:40:47

标签: java scala sbt apache-flink flink-streaming

我正在尝试提交由Scala 2.11创建的Flink作业,该作业通过在命令行中运行在本地Flink群集中使用Twitter流API:

flink run -c org.myClass C:\path\to\jarFile.jar

并出现以下错误:

2019-06-09 23:40:47,758 WARN  org.apache.flink.runtime.webmonitor.handlers.JarRunHandler    - Configuring the job submission via query parameters is deprecated. Please migrate to submitting a JSON request instead.
2019-06-09 23:40:47,762 ERROR org.apache.flink.runtime.webmonitor.handlers.JarRunHandler    - Unhandled exception.
org.apache.flink.client.program.ProgramInvocationException: The program caused an error: 
    at org.apache.flink.client.program.OptimizerPlanEnvironment.getOptimizedPlan(OptimizerPlanEnvironment.java:93)
    at org.apache.flink.client.program.PackagedProgramUtils.createJobGraph(PackagedProgramUtils.java:80)
    at org.apache.flink.runtime.webmonitor.handlers.utils.JarHandlerUtils$JarHandlerContext.toJobGraph(JarHandlerUtils.java:126)
    at org.apache.flink.runtime.webmonitor.handlers.JarRunHandler.lambda$getJobGraphAsync$6(JarRunHandler.java:142)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: org/apache/flink/streaming/connectors/twitter/TwitterSource$EndpointInitializer
    at msciss.TwitterHashtagCounter.main(TwitterHashtagCounter.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.flink.client.program.PackagedProgram.callMainMethod(PackagedProgram.java:529)
    at org.apache.flink.client.program.PackagedProgram.invokeInteractiveModeForExecution(PackagedProgram.java:421)
    at org.apache.flink.client.program.OptimizerPlanEnvironment.getOptimizedPlan(OptimizerPlanEnvironment.java:83)
    ... 7 more
Caused by: java.lang.ClassNotFoundException: org.apache.flink.streaming.connectors.twitter.TwitterSource$EndpointInitializer
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 15 more

但是在程序中,我在下面的build.sbt中设置了TwitterSource库:

val flinkDependencies = Seq(
  "org.apache.flink" %% "flink-scala" % flinkVersion % "provided",
  "org.apache.flink" %% "flink-streaming-scala" % flinkVersion % "provided",
  "org.apache.flink" %% "flink-connector-twitter" % flinkVersion,
  "commons-logging" % "commons-logging" % "1.2",
  "org.apache.logging.log4j" % "log4j-core" % "2.11.2",
  "org.apache.commons" % "commons-text" % "1.6")

该应用程序还可以在IntelliJ中正常运行,并且sbt buld /程序包不会产生任何问题。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您需要使用sbt assembly插件或任何其他可以创建Fat Jar(Uber Jar)的插件。当前,您的软件包不包含外部库,并且flink连接器被视为外部库,因为它们不包含在标准二进制版本中。因此,您实际上正在创建的程序包不包含twitter-connector,但Flink本身也不包含,这就是为什么您要获得ClassNotFoundException的原因。

答案 1 :(得分:0)

我有一个胖的(超级)罐子。爆炸时,可以看到连接器依存关系。但是,当我将罐子作为flink作业提交时,我得到了classnotfoundexception。

可能是什么原因?