在 EMR 上提交 JAR 时出现 ClassNotFoundException
ClassNotFoundException while spark-submitting a JAR on EMR
我正在使用 eclipse/Maven 创建一个 JAR 并 运行 在 EMR
上安装它
这是我的 pom.xml 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sudarshan</groupId>
<artifactId>SparkApplication</artifactId>
<version>SQL</version>
<packaging>jar</packaging>
<name>SparkApplication</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Assembly Plugin -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>financialLineItem.FinancialLineItem</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase> <!-- packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这就是我在 EMR 集群
中部署和 运行 我的 jar 的方式
spark-submit --deploy-mode cluster --class financialLineItem.FinancialLineItem s3://path/SparkApplication-SQL-jar-with-dependencies.jar
当我 运行 我的代码在 zeppelin note book 中时 运行 没问题,但在 spark-submit
中它抛出以下异常
Exception in thread "main" java.lang.ClassNotFoundException: financialLineItem.FinancialLineItem
这是我的项目设置的样子:
如何纠正?
我还按照以下文档创建了 spark 并在 EMR 中提交
https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-spark-submit-step.html
在这里,他们既没有在 spark 作业配置中设置 master URL,也没有在从 spark-submit
提交时设置
您的 pom.xml
中缺少 sourceDirectory
。
根据 Maven docs for Standard Directory Layout,默认的 sourceDirectory
是 src/main/java
,并且由于您的项目结构是 src/main/scala
,类 没有被编译。
在您的构建配置下添加:
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<finalName>Sample</finalName>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.3</version>
<executions>
<execution>
<goals>
<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>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<useFile>false</useFile>
<disableXmlReport>true</disableXmlReport>
<!-- If you have classpath issue like NoDefClassError,... -->
<!-- useManifestOnlyJar>false</useManifestOnlyJar -->
<includes>
<include>**/*Test.*</include>
<include>**/*Suite.*</include>
</includes>
</configuration>
</plugin>
<!--
Maven Assembly Plugin
-->
</plugins>
</build>
当你在集群模式下 运行 spark-submit 时,驱动程序 运行 与客户端在不同的机器上,所以你在 spark- 中提供的 jar提交脚本需要放在驱动程序的 class 路径上,如下所示:-
--driver-class-path s3://path/SparkApplication-SQL-jar-with-dependencies.jar
因此您可以尝试以下脚本:
spark-submit --deploy-mode cluster --class financialLineItem.FinancialLineItem --driver-class-path s3://path/SparkApplication-SQL-jar-with-dependencies.jar
我正在使用 eclipse/Maven 创建一个 JAR 并 运行 在 EMR
上安装它这是我的 pom.xml 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sudarshan</groupId>
<artifactId>SparkApplication</artifactId>
<version>SQL</version>
<packaging>jar</packaging>
<name>SparkApplication</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Assembly Plugin -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>financialLineItem.FinancialLineItem</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase> <!-- packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这就是我在 EMR 集群
中部署和 运行 我的 jar 的方式spark-submit --deploy-mode cluster --class financialLineItem.FinancialLineItem s3://path/SparkApplication-SQL-jar-with-dependencies.jar
当我 运行 我的代码在 zeppelin note book 中时 运行 没问题,但在 spark-submit
中它抛出以下异常
Exception in thread "main" java.lang.ClassNotFoundException: financialLineItem.FinancialLineItem
这是我的项目设置的样子:
如何纠正?
我还按照以下文档创建了 spark 并在 EMR 中提交 https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-spark-submit-step.html 在这里,他们既没有在 spark 作业配置中设置 master URL,也没有在从 spark-submit
提交时设置您的 pom.xml
中缺少 sourceDirectory
。
根据 Maven docs for Standard Directory Layout,默认的 sourceDirectory
是 src/main/java
,并且由于您的项目结构是 src/main/scala
,类 没有被编译。
在您的构建配置下添加:
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<finalName>Sample</finalName>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.3</version>
<executions>
<execution>
<goals>
<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>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<useFile>false</useFile>
<disableXmlReport>true</disableXmlReport>
<!-- If you have classpath issue like NoDefClassError,... -->
<!-- useManifestOnlyJar>false</useManifestOnlyJar -->
<includes>
<include>**/*Test.*</include>
<include>**/*Suite.*</include>
</includes>
</configuration>
</plugin>
<!--
Maven Assembly Plugin
-->
</plugins>
</build>
当你在集群模式下 运行 spark-submit 时,驱动程序 运行 与客户端在不同的机器上,所以你在 spark- 中提供的 jar提交脚本需要放在驱动程序的 class 路径上,如下所示:-
--driver-class-path s3://path/SparkApplication-SQL-jar-with-dependencies.jar
因此您可以尝试以下脚本:
spark-submit --deploy-mode cluster --class financialLineItem.FinancialLineItem --driver-class-path s3://path/SparkApplication-SQL-jar-with-dependencies.jar