問題
在spark程序中,經(jīng)常需要一些外部的依賴(比如Zookeper、libthrift等),這些依賴可能本身在spark或者Hadoop客戶端的jar包中就已經(jīng)存在。當(dāng)用戶程序依賴的jar包版本和集群上spark/hadoop客戶端依賴的jar包版本不一致時(shí),可能會(huì)出現(xiàn)編譯失敗,或者執(zhí)行過程中加載類失敗的問題。
解決
分兩種情況:
程序本身不依賴特定版本
用集群中存在的jar包就可以:這種情況可以在編譯時(shí)去掉程序本身對(duì)這個(gè)jar包的依賴。參考POM文件示例中的在依賴時(shí)加上:<scope>provided</scope>程序需要依賴特定版本
一般是集群中的該jar包版本較低,無法滿足需求。這種情況可以利用maven-shade-plugin插件將沖突Jar包中的類重命名,在程序中調(diào)用重命名后的類,避免和集群上低版本的jar包沖突。
maven-shade-plugin
http://www.itdecent.cn/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>