【问题标题】:NoClassDefFoundError: org/apache/spark/sql/SparkSession$ while running spark source code locallyNoClassDefFoundError: org/apache/spark/sql/SparkSession$ 在本地运行 spark 源代码时
【发布时间】:2020-07-02 15:18:28
【问题描述】:

我已经在本地机器上克隆了 spark 项目,并使用以下命令构建了 spark 项目,构建成功。

mvn -DskipTests clean package

我在 IntelliJ idea 中将 spark 项目作为 maven 项目导入。 我在我的项目中将 Scala 2.12.10 设置为全局库

但是当我尝试运行示例模块中的任何示例程序时,我遇到了以下错误。 我想这与 Scala 编译有关,请帮助我理解这里发生了什么?

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/sql/SparkSession$
    at org.apache.spark.examples.GroupByTest$.main(GroupByTest.scala:30)
    at org.apache.spark.examples.GroupByTest.main(GroupByTest.scala)
Caused by: java.lang.ClassNotFoundException: org.apache.spark.sql.SparkSession$
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 2 more

分享我正在运行的示例火花代码

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// scalastyle:off println
package org.apache.spark.examples

import java.util.Random

import org.apache.spark.sql.SparkSession

/**
 * Usage: GroupByTest [numMappers] [numKVPairs] [KeySize] [numReducers]
 */
object GroupByTest {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder
      .appName("GroupBy Test")
      .getOrCreate()

    val numMappers = if (args.length > 0) args(0).toInt else 2
    val numKVPairs = if (args.length > 1) args(1).toInt else 1000
    val valSize = if (args.length > 2) args(2).toInt else 1000
    val numReducers = if (args.length > 3) args(3).toInt else numMappers

    val pairs1 = spark.sparkContext.parallelize(0 until numMappers, numMappers).flatMap { p =>
      val ranGen = new Random
      val arr1 = new Array[(Int, Array[Byte])](numKVPairs)
      for (i <- 0 until numKVPairs) {
        val byteArr = new Array[Byte](valSize)
        ranGen.nextBytes(byteArr)
        arr1(i) = (ranGen.nextInt(Int.MaxValue), byteArr)
      }
      arr1
    }.cache()
    // Enforce that everything has been calculated and in cache
    pairs1.count()

    println(pairs1.groupByKey(numReducers).count())

    spark.stop()
  }
}
// scalastyle:on println

【问题讨论】:

  • 你能分享代码吗?一种简单的解决方案是使用 App 模块扩展您的对象。
  • 我正在使用 spark 项目中示例模块中的示例程序
  • 从你的 maven pom 文件中导入 spark 包。
  • 做不到,我的最终目标是在 spark 源代码中进行一些编辑并进行实验
  • 必须在 Maven 项目中将 Spark 依赖项添加为 compile 范围。

标签: scala maven apache-spark intellij-idea


【解决方案1】:

SparkSession 是 spark-sql 的一部分。因此,您需要将此库的 provided 范围更改为 compile

<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-sql_${scala.binary.version}</artifactId>
    <version>${spark.version}</version>
    <scope>compile</compile>
</dependency>

【讨论】:

    【解决方案2】:

    您似乎正在尝试运行来自 https://github.com/apache/spark/blob/master/examples/src/main/scala/org/apache/spark/examples/GroupByTest.scala 的示例

    pom.xml 中有很多provided 范围的依赖项。如果您想从 IntellijIdea 或通过java -jar 运行示例,您应该将其范围更改为compile

    如果您要使用 spark shell 或spark-submit 命令运行作业,则使用provided 范围。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-05
      • 2020-08-23
      • 1970-01-01
      • 2017-12-20
      • 2018-08-09
      • 1970-01-01
      • 2018-10-08
      相关资源
      最近更新 更多