【发布时间】: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