Spark提交任务时, 版本问题报错

本文解决在Spark集群部署过程中遇到的编译环境版本不一致导致的错误,包括Scala、JDK版本冲突及Maven编译失败问题,并提供正确的pom配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题一: 提交任务到集群时报错

java.lang.NoSuchMethodError: scala.reflect.api.JavaUniverse.runtimeMirror

此异常有可能是以下的情况造成的

1.编译环境的 scala版本和集群上的scala版本不一致 -- 进入spark-shell可以查看控制台输出的scala版本
	spark的版本和scala的版本请另行查找.或者通过spark-shell确定 scala版本
	本人spark 2.2.3 + hadoop 2.7.6 + scala 2.11.8
	
2.编译环境的jdk版本和集群上的jdk版本不一致 - 本人就是卡在这里, 耗费一天时间
	开发环境上安装了多个jdk版本的同学要小心这一点, 新建项目时IDEA很可能设置的JDK和集群上的是不一致的.
问题二: maven编译时报错

Failed to execute goal net.alchim31.maven:scala-maven-plugin:3.2.0:compile (default) on project XXXX: wrap: org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) -> [Help 1]

解决方案如下:

1.确保maven.compiler.source, maven.compiler.target的版本和所用的JDK版本一致
2.确保scala版本和集群一致
3.确保scala版本和spark版本互相兼容
4.如使用Hadoop, 则需确保hadoop版本和spark版本兼容
5.修改pom文件 --- 重要, 本人在此耗费半天时间

编译通过的pom 如下
注意:
1.要在 <execution> 标签中指定 <id>scala-compile-first</id>
2.在<goal>标签中指定 <goal>add-source</goal>
3.删除这个标签 <arg>-make:transitive</arg>, 下面的pom中注释掉了这个标签
4.添加一个 包含 <id>scala-test-compile</id> 的execution, 详细内容请查看pom

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.version>2.11.8</scala.version>
    <spark.version>2.2.3</spark.version>
    <hadoop.version>2.7.6</hadoop.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>${scala.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.11</artifactId>
        <version>${spark.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-sql_2.11</artifactId>
        <!--// spark-core的版本号-->
        <version>${spark.version}</version>
    </dependency>

    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-reflect</artifactId>
        <version>${scala.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>${hadoop.version}</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
    </dependency>
</dependencies>

<build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>3.2.2</version>
            <configuration>
                <scalaVersion>${scala.version}</scalaVersion>
            </configuration>
            <executions>
                <execution>
                    <id>scala-compile-first</id>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                    <configuration>
                        <args>
                            <!--<arg>-make:transitive</arg>-->
                            <arg>-dependencyfile</arg>
                            <arg>${project.build.directory}/.scala_dependencies</arg>
                        </args>
                    </configuration>
                </execution>
                <execution>
                    <id>scala-test-compile</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <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>
                        <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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值