美文网首页
Spark开发—JAR包版本冲突问题解决

Spark开发—JAR包版本冲突问题解决

作者: 西二旗老司机 | 来源:发表于2019-03-25 17:33 被阅读0次

问题

在spark程序中,经常需要一些外部的依赖(比如Zookeper、libthrift等),这些依赖可能本身在spark或者Hadoop客户端的jar包中就已经存在。当用户程序依赖的jar包版本和集群上spark/hadoop客户端依赖的jar包版本不一致时,可能会出现编译失败,或者执行过程中加载类失败的问题。

解决

分两种情况:

  • 程序本身不依赖特定版本
    用集群中存在的jar包就可以:这种情况可以在编译时去掉程序本身对这个jar包的依赖。参考POM文件示例中的在依赖时加上:<scope>provided</scope>

  • 程序需要依赖特定版本
    一般是集群中的该jar包版本较低,无法满足需求。这种情况可以利用maven-shade-plugin插件将冲突Jar包中的类重命名,在程序中调用重命名后的类,避免和集群上低版本的jar包冲突。
    maven-shade-plugin
    http://www.jianshu.com/p/7a0e20b30401

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</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>
  ...
</project>

相关文章

网友评论

      本文标题:Spark开发—JAR包版本冲突问题解决

      本文链接:https://www.haomeiwen.com/subject/zbpbvqtx.html