問題背景:
使用spark datasource v2 接口,外接存儲源時,發現更改項目版本后,spark-shell報錯
搜索該錯誤無果,網上報什么window util錯,並不是我遇到的問題.
error: not found: value spark
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.spark.package$.SPARK_VERSION()Ljava/lang/String;
分析:
顯然,存在報錯提示包沖突,從spark-2.4.5-bin-hadoop2.7/jars 移除自己的jar包文件后,./bin/spark-shell 可以正常執行。
查看自己的jar包信息,jar -vtf xxx.jar 發現奇怪的org/apache/spark/package.class和org/apache/spark/package$.class,
懷疑這package的.class導致,修改自己代碼的包結構,編譯仍然出現。
解決方法:
通過maven 插件,排除org/apache/spark/package.class和org/apache/spark/package$.class。 不夠還不知道為何會產生這個package.class
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <artifactSet> <excludes> <exclude>org.slf4j:*</exclude> <exclude>log4j:*</exclude> <exclude>org.apache.spark:*</exclude> <exclude>org.apache.hadoop:*:jar:</exclude> </excludes> </artifactSet> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> <exclude>log4j.properties</exclude> <exclude>org/apache/spark/network/util/**</exclude> <exclude>com/fasterxml/jackson/**</exclude> <exclude>org/apache/spark/package$.class</exclude> <exclude>org/apache/spark/package.class</exclude> <!--exclude>org/apache/spark/util/**</exclude--> </excludes> </filter> </filters> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>reference.conf</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"/> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> </transformers> </configuration> </execution> </executions> </plugin>
其他:
1. JVM 加載 jar的順序成迷
XX-1.0-SNAPSHOT.jar 在classpath的末尾,而XX-1.1.1.jar的將在前面,用stat XX-1.1.1.jar 和XX-1.0-SNAPSHOT.jar查看inode,也是大於spark-core_2.11-2.4.5.jar的inode編號。所以網上說的和inode系統有關,也未驗證成功。
修改jar包名也未改變在classpath中的順序。
2. 使用--jars XXX.jar 在classpath的最后,后加載也可以避免該問題。