scala - 导入时由 : java. lang.NoClassDefFoundError: org/apache/spark/sql/types/StructType 引起

标签 scala apache-spark

我使用 Spark SQL 依赖项,就像已经导入它的数据结构一样,但没有将 SPark 上下文传递给 Scala 的 main 方法。为什么我会遇到此异常?

Caused by: java.lang.NoClassDefFoundError: org/apache/spark/sql/types/StructType 

代码:

package io.sekai.industry.streaming

import org.apache.spark.sql.types
import org.apache.spark.sql.types.{LongType, StringType, StructField, StructType}

class NonSparkSensorsGeneration {

  val schema = StructType(
    StructField("id", LongType, nullable = false) :: //always skip this if using the operator
      StructField("Energy Data", StringType, nullable = false, metadata = new types.MetadataBuilder().putString("mqtt", "ESP_02/Energy Data").putString("label", "EnergyData").build()) ::
      Nil)

  def readFromCSVFile(shortPath: String): Unit= {

    println("Preparing to load the csv file from jar resource " + shortPath)
    val input = getClass.getResourceAsStream(shortPath)
    println("Loaded file from resource " + shortPath)
    println("Input stream from resource " + shortPath + " details: " + input.toString)
    val source = scala.io.Source.fromInputStream(input)
    val data = source.getLines.map(_.split("\t")).toArray
    source.close
    println(data.getClass)
  }


  def main(args: Array[String]): Unit = {
    readFromCSVFile("generation")
  }
}

那么类是从对象继承的:

package io.sekai.industry.streaming

object NonSparkSensorsGenerationJob extends NonSparkSensorsGeneration

并运行为:

"C:\Program Files\AdoptOpenJDK\jdk-12.0.1.12-hotspot\bin\java.exe" "-javaagent:D:\idea\20\IntelliJ IDEA Community Edition 2020.1.2\lib\idea_rt.jar=50417:D:\idea\20\IntelliJ IDEA Community Edition 2020.1.2\bin" -Dfile.encoding=UTF-8 @C:\Users\eljah32\AppData\Local\Temp\idea_arg_file1955225673 .industry.streaming.NonSparkSensorsGenerationJob

当我删除 val schema 变量时,它运行正常

最佳答案

您可能忘记在 IDE 中启用将具有“provided”范围的依赖项添加到类路径标志(我提到的标志来自 IntelliJ IDEA)

关于scala - 导入时由 : java. lang.NoClassDefFoundError: org/apache/spark/sql/types/StructType 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70516801/

相关文章:

scala - 重载方法值适用于备选方案 :

scala - 用scala编写*类型和编程语言*一书中的函数 `selfApp`

scala - 混合面向对象和函数式编程

scala - Apache Spark、范围连接、数据倾斜和性能

scala - 是否可以在Namenode上执行任务?

algorithm - Scala:测试阵列中等间隔频率的惯用方法是什么?

json - Argonaut,自定义 JSON 格式映射

scala - 多scala数据帧连接

python - 检查类型 : How to check if something is a RDD or a DataFrame?

scala - 如何将包含 (vertexId,edgeId) 的 Map 转换为 GraphX RDD