简体   繁体   English

Flink 中的 java.lang.NoSuchMethodError

[英]java.lang.NoSuchMethodError in Flink

I trying to read the a file using :我尝试使用以下方法读取文件:

final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
DataSet<String> line = env.readTextFile("file:///pathtofile/myfile.txt");

I get following error:我收到以下错误:

java.lang.NoSuchMethodError: org.apache.flink.api.common.io.DelimitedInputFormat: method <init>(Lorg/apache/flink/core/fs/Path;)V not found

I'm using flink version 1.3.2, java version "1.8.0_91"我正在使用 flink 版本 1.3.2,Java 版本“1.8.0_91”

There is a conflict with dependencies.存在依赖冲突。 Apache Flink loads many classes by default into its classpath.默认情况下,Apache Flink 将许多类加载到其类路径中。

Please read this article https://ci.apache.org/projects/flink/flink-docs-release-1.3/monitoring/debugging_classloading.html the last section请阅读这篇文章https://ci.apache.org/projects/flink/flink-docs-release-1.3/monitoring/debugging_classloading.html最后一节

Resolving Dependency Conflicts with Flink using the maven-shade-plugin使用 maven-shade-plugin 解决与 Flink 的依赖冲突

Apache Flink loads many classes by default into its classpath.默认情况下,Apache Flink 将许多类加载到其类路径中。 If a user uses a different version of a library that Flink is using, often如果用户使用 Flink 正在使用的库的不同版本,通常

IllegalAccessExceptions

or

NoSuchMethodError

are the result.是结果。

So, I suggest to play with your pom.xml and use maven-shade-plugin and add correct relocation, as we have in example因此,我建议使用您的 pom.xml 并使用 maven-shade-plugin 并添加正确的重定位,就像我们在示例中所做的那样

<relocation>
  <pattern>org.codehaus.plexus.util</pattern>
  <shadedPattern>org.shaded.plexus.util</shadedPattern>
  <excludes>
    <exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude>
    <exclude>org.codehaus.plexus.util.xml.pull.*</exclude>
  </excludes>
</relocation>

Are you getting this error in IntelliJ or Dashboard, if you are getting this error in IntelliJ then make sure you use the same Flink version in your pom.xml and also add dependency shading in the build like this您是否在 IntelliJ 或 Dashboard 中收到此错误,如果您在 IntelliJ 中收到此错误,请确保在 pom.xml 中使用相同的 Flink 版本,并在构建中添加依赖阴影,如下所示

 <build>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>

                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerId>jdt</compilerId>
                </configuration>

                <dependencies>
                    <dependency>
                        <groupId>org.eclipse.tycho</groupId>
                        <artifactId>tycho-compiler-jdt</artifactId>
                        <version>0.21.0</version>
                    </dependency>
                </dependencies>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <relocations>
                                <relocation>
                                    <pattern>org.codehaus.plexus.util</pattern>
                                    <shadedPattern>org.shaded.plexus.util</shadedPattern>
                                    <excludes>
                                        <exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude>
                                        <exclude>org.codehaus.plexus.util.xml.pull.*</exclude>
                                    </excludes>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


        </plugins>


</build>>

make sure to run maven clean install in the terminal after you make changes .进行更改后,请确保在终端中运行 maven clean install。 On the other hand, If you are having this issue only in Dashboard not in intelliJ , then have a look here另一方面,如果您仅在 Dashboard 中遇到此问题,而不在 intelliJ 中,请查看此处

I faced the similar issue, for me the problem was flink minor version mismatch.我遇到了类似的问题,对我来说问题是 flink 次要版本不匹配。 My local flink cluster was running flink-1.8.0 and my code expected the version to be flink-1.8.3.我的本地 flink 集群运行的是 flink-1.8.0,我的代码预期版本为 flink-1.8.3。 Switching to newer version solved this issue.切换到新版本解决了这个问题。

您需要检查您的构建路径,确保库在那里并正确导入它们

One possible cause for error "java.lang.NoSuchMethodError" is when we use different version of flink then what we have installed on our system.错误“java.lang.NoSuchMethodError”的一个可能原因是当我们使用与系统上安装的不同版本的 flink 时。 For me, I have Flink 1.3.2 and the version I was using was 1.1.1 .对我来说,我有 Flink 1.3.2 而我使用的版本是 1.1.1 。 So I updated my pom file to have same version.所以我更新了我的 pom 文件以具有相同的版本。

For the ones who're stuggling with the above issue, while using Flink 1.3.2 , here's the entire pom which I was able to successfully build:对于那些在使用Flink 1.3.2时遇到上述问题的人,这是我能够成功构建的整个 pom:

<build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
               <!-- get all project dependencies -->
               <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
               </descriptorRefs>
               <!-- MainClass in mainfest make a executable jar -->
               <archive>
                  <manifest>
                     <mainClass>FlinktoLambda</mainClass>
                  </manifest>
               </archive>
            </configuration>
            <executions>
               <execution>
                  <id>make-assembly</id>
                  <!-- bind to the packaging phase -->
                  <phase>package</phase>
                  <goals>
                     <goal>single</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
               <archive>
                  <manifest>
                     <mainClass>FlinktoLambda</mainClass>
                  </manifest>
               </archive>
            </configuration>
         </plugin>
         <!--added newly-->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
               <source>1.8</source>
               <target>1.8</target>
               <compilerId>jdt</compilerId>
            </configuration>
            <dependencies>
               <dependency>
                  <groupId>org.eclipse.tycho</groupId>
                  <artifactId>tycho-compiler-jdt</artifactId>
                  <version>0.21.0</version>
               </dependency>
            </dependencies>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
               <execution>
                  <phase>package</phase>
                  <goals>
                     <goal>shade</goal>
                  </goals>
                  <configuration>
                     <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                           <mainClass>FlinktoLambda</mainClass>
                        </transformer>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                           <resource>reference.conf</resource>
                        </transformer>
                     </transformers>
                     <relocations>
                        <relocation>
                           <pattern>org.codehaus.plexus.util</pattern>
                           <shadedPattern>org.shaded.plexus.util</shadedPattern>
                           <excludes>
                              <exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude>
                              <exclude>org.codehaus.plexus.util.xml.pull.*</exclude>
                           </excludes>
                        </relocation>
                     </relocations>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>
   <name>my-app</name>
   <url>http://maven.apache.org</url>
   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>FlinktoLambda</groupId>
            <artifactId>my-app</artifactId>
            <exclusions>
               <exclusion>
                  <groupId>com.typesafe.akka</groupId>
                  <artifactId>akka-remote_2.10</artifactId>
               </exclusion>
               <exclusion>
                  <groupId>com.typesafe.akka</groupId>
                  <artifactId>akka-actor_2.10</artifactId>
               </exclusion>
               <exclusion>
                  <groupId>com.typesafe.akka</groupId>
                  <artifactId>akka-slf4j_2.10</artifactId>
               </exclusion>
            </exclusions>
         </dependency>
      </dependencies>
   </dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>org.apache.flink</groupId>
         <artifactId>flink-table_2.10</artifactId>
         <version>1.3.2</version>
      </dependency>
      <dependency>
         <groupId>org.apache.flink</groupId>
         <artifactId>flink-java</artifactId>
         <version>1.3.2</version>
      </dependency>
      <dependency>
         <groupId>org.apache.flink</groupId>
         <artifactId>flink-streaming-java_2.10</artifactId>
         <version>1.3.2</version>
      </dependency>
      <dependency>
         <groupId>org.apache.flink</groupId>
         <artifactId>flink-clients_2.10</artifactId>
         <version>1.3.2</version>
      </dependency>
      <dependency>
         <groupId>org.apache.flink</groupId>
         <artifactId>flink-scala_2.10</artifactId>
         <version>1.3.2</version>
      </dependency>
      <dependency>
         <groupId>org.apache.flink</groupId>
         <artifactId>flink-streaming-scala_2.10</artifactId>
         <version>1.3.2</version>
      </dependency>
   </dependencies>

Please change your main class accordingly in the shade plugin.请在shade插件中相应地更改您的主类。

Another source of this error could be a mismatch of scala version between Flink and your application code (or your application dependencies that uses scala).此错误的另一个来源可能是 Flink 和您的应用程序代码(或使用 scala 的应用程序依赖项)之间的 scala 版本不匹配。

For instance, in my case, I was using Flink 1.7.1 and I had to update my scala dependencies from 2.11 to 2.12;例如,就我而言,我使用的是 Flink 1.7.1,我不得不将我的 scala 依赖项从 2.11 更新到 2.12; I updated the artifcatId of the concerned dependencies as follows: from flink-scala_2.11 to flink-scala_2.12 , flink-table_2.11 to flink-table_2.12 , etc.我将相关依赖项的 artifcatId 更新如下:从flink-scala_2.11flink-scala_2.12flink-table_2.11flink-table_2.12等。

See here for more info.请参阅此处了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM