【问题标题】:Error: Failed to Load Class main using Spark-submit错误:无法使用 Spark-submit 加载主类
【发布时间】:2021-03-18 00:51:14
【问题描述】:

我的代码在下面

import org.apache.spark.SparkContext;
import org.apache.spark.SparkConf;
object WordCounter {
   def main(args: Array[String]) {
     val conf = new SparkConf().setAppName("Word Counter").setMaster("local")
     val sc = new SparkContext(conf)
     val textFile = sc.textFile("C:/spark/README.md")
     val tokenizedFileData = textFile.flatMap(line=>line.split(" "))
     val countPrep = tokenizedFileData.map(word=>(word,1))
     val counts = countPrep.reduceByKey((accumValue,newValue)=> accumValue + newValue)
     val sortedCounts = counts.sortBy(kvPair=>kvPair._2,false)
     sortedCounts.saveAsTextFile("C:/Data/WordCountViaApp")
   }
}

有人可以帮忙吗?

C:\Users\workspace\SparkInScala>spark-submit --class "WordCounter" "C:\Users\workspace\SparkInScala\target\scala-2.12\sparkinscala_2.12-0.1.0-SNAPSHOT.jar" 错误:无法加载 WordCounter 类。

【问题讨论】:

  • 您使用哪个 IDE 来执行此操作?是日食吗?
  • 嘿,是的,阿布舍克。在 Eclipse 中它工作正常。但是当我尝试在命令提示符下做一个 sbt 包时,它失败了

标签: scala apache-spark sbt word-count spark-submit


【解决方案1】:

在使用适当版本的 build.sbt 文件中进行更改后,这可以正常工作。 我不再看到无法加载类的错误。

【讨论】:

    【解决方案2】:

    我以前遇到过同样的问题。

    我正在使用idea的Maven插件开发Spark程序,这里是我的pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.2</version>
                <executions>
                    <execution>
                        <id>compile-scala</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile-scala</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <scalaVersion>2.12.11</scalaVersion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>testConf</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>java</executable>
                    <includeProjectDependencies>true</includeProjectDependencies>
                    <includePluginDependencies>false</includePluginDependencies>
                    <classpathScope>compile</classpathScope>
                    <mainClass>testConf</mainClass>
                </configuration>
            </plugin>
        </plugins>
    
    </build>
    

    您可以通过将 mainClass 标签“testConf”替换为您的类名和 scala 版本等来尝试。

    【讨论】:

      【解决方案3】:

      如果你想使用 SBT,那么你必须指定哪个 main 方法开始执行。

      Main-Class: com.WordCounter.main
      

      上面的内容告诉 SBT 在运行 sbt 包时将该行添加到 JAR 中的 META-INF/MANIFEST.MF 文件中。

      使用 SBT 运行应用程序时,

      sbt "run-main com.WordCounter.main"
      

      此外,正如 Wu 所指出的,Maven 也将完成这项工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-30
        • 1970-01-01
        • 2017-11-26
        • 2018-07-02
        • 1970-01-01
        • 1970-01-01
        • 2018-10-19
        相关资源
        最近更新 更多