Spark Submit提交Java程序时找不到com.crealytics.spark.excel数据源求助
解决Spark提交时找不到com.crealytics.spark.excel数据源的问题
问题原因
本地IntelliJ IDEA运行正常是因为IDE自动加载了所有Maven依赖,但你构建的spark-excel-1.0-SNAPSHOT.jar是瘦包(仅包含自身代码,未打包第三方依赖),导致Spark运行时找不到com.crealytics.spark.excel相关类。
解决方案
方案1:spark-submit直接指定依赖包
提交命令时通过--packages参数让Spark自动下载并加载依赖:
spark-submit --class org.example.Main --packages com.crealytics:spark-excel_2.12:3.4.1_0.19.0 spark-excel-1.0-SNAPSHOT.jar
方案2:构建包含所有依赖的胖包
修改pom.xml,替换原有的maven-jar-plugin为maven-shade-plugin,将第三方依赖打包进最终jar:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>org.example.Main</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> </transformers> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build>
执行mvn clean package后,使用生成的spark-excel-1.0-SNAPSHOT-shaded.jar提交:
spark-submit --class org.example.Main spark-excel-1.0-SNAPSHOT-shaded.jar
方案3:手动添加依赖到Spark全局classpath(不推荐)
下载spark-excel_2.12:3.4.1_0.19.0.jar,放到Spark安装目录的jars文件夹下,再执行原提交命令。此方法会污染Spark全局环境,仅适合临时测试。
额外说明
代码中写入时使用的format("excel")是该库的V2数据源格式名,你的依赖版本3.4.1_0.19.0已支持,无需修改。
内容的提问来源于stack exchange,提问作者Arman
相关产品推荐
相关产品推荐

