在 Java 中运行简单的 Spark 程序时 - 出现 NoClassDefFoundError

问题描述 投票:0回答:1

几年前我总是能够使这个程序运行。 阅读 apache 网络日志很简单。

执行时出现此错误

线程“main”中的异常 java.lang.NoClassDefFoundError: scala/$less$colon$less 在 org.example.ApacheLogs.main(ApacheLogs.java:14) 引起原因:java.lang.ClassNotFoundException:scala.$less$colon$less 在 java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) 在 java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) 在 java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ... 1 更多

尝试运行基本的 Spark Java 程序时

package org.example;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;


public class ApacheLogs
{

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        SparkSession spark = SparkSession.builder().appName("Apache Web Logs").master("local[*]").getOrCreate();
        readApacheWebLogs(spark);
        spark.stop();

    }
    private static void readApacheWebLogs(SparkSession spark) 
    {
        Dataset<Row> df = spark.read().text("src/main/resources/apache_web.log");
        df.printSchema();
        //df.select("IP Address");
        df.show();
        
        System.out.println("Head of the Rowset-->"+ df.head());
        //df.printSchema();
                
    }

}

这是 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>Spark</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>20</maven.compiler.source>
        <maven.compiler.target>20</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.12</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.13</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.11.8</version>
        </dependency>
        <!-- Add other dependencies as per your requirements -->
    </dependencies>

    <build>
        <plugins>
            <!-- Maven Compiler Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>

            <!-- Maven Shade Plugin to create an uber-jar -->
            <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>WordCount</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

我这个程序过去总是运行良好,但两年后重新访问它以温习我的 Spark 知识并在运行时出现此错误。构建进展顺利。

noclassdeffounderror
1个回答
0
投票

你能检查一下scala版本吗?我在你的 POM 中看到 2.11、2.12、2.13。

© www.soinside.com 2019 - 2024. All rights reserved.