Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala NoClassDefFoundError:org/apache/spark/sql/SparkSession$在本地运行spark源代码时_Scala_Maven_Apache Spark_Intellij Idea - Fatal编程技术网

Scala NoClassDefFoundError:org/apache/spark/sql/SparkSession$在本地运行spark源代码时

Scala NoClassDefFoundError:org/apache/spark/sql/SparkSession$在本地运行spark源代码时,scala,maven,apache-spark,intellij-idea,Scala,Maven,Apache Spark,Intellij Idea,我已经在本地机器上克隆了spark项目,我使用下面的命令构建了spark项目,构建成功 mvn -DskipTests clean package 我在IntelliJ idea中将spark项目作为maven项目导入。 我在项目中将Scala 2.12.10设置为全局库 但是,当我尝试运行示例模块中的任何示例程序时,我发现了以下错误。 我想这与Scala编译有关,请帮助我理解这里发生了什么 Exception in thread "main" java.lang.NoClassDefFoun

我已经在本地机器上克隆了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
分享我正在运行的示例spark代码

/*
 * 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
/*
*根据一个或多个许可证颁发给Apache软件基金会(ASF)
*贡献者许可协议。请参阅随附的通知文件
*本作品提供了有关版权所有权的更多信息。
*ASF根据Apache许可证2.0版将此文件许可给您
*(以下简称“许可证”);除非符合以下要求,否则不得使用此文件
*执照。您可以通过以下方式获得许可证副本:
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
*/
//scalastyle:关闭打印ln
包org.apache.spark.examples
导入java.util.Random
导入org.apache.spark.sql.SparkSession
/**
*用法:GroupByTest[numMappers][numKVPairs][KeySize][numReducers]
*/
对象GroupByTest{
def main(参数:数组[字符串]):单位={
val spark=火花会话
建设者
.appName(“分组测试”)
.getOrCreate()
val numappers=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 numappers
val pairs1=spark.sparkContext.parallelize(0到numappers,numappers)。flatMap{p=>
val ranGen=新随机变量
val arr1=新数组[(Int,数组[字节]](numKVPairs)

因为(i似乎您正试图从

pom.xml
中,有许多依赖项提供了
作用域。如果您想从IntellijIdea或通过
java-jar
运行示例,您应该将其作用域更改为
compile


如果要使用spark shell或
spark submit
命令运行作业,则使用
provided
作用域。

SparkSession是
spark sql
的一部分。因此,您需要将
provided
作用域更改为
编译此库:

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

org.apache.spark
spark-sql{scala.binary.version}
${spark.version}
编译

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