scala - 为什么使用案例类对 JSON 进行编码时会出现错误 "Unable to find encoder for type stored in a Dataset"?

标签 scala apache-spark apache-spark-dataset apache-spark-encoders

我写过 Spark 工作:

object SimpleApp {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("Simple Application").setMaster("local")
    val sc = new SparkContext(conf)
    val ctx = new org.apache.spark.sql.SQLContext(sc)
    import ctx.implicits._

    case class Person(age: Long, city: String, id: String, lname: String, name: String, sex: String)
    case class Person2(name: String, age: Long, city: String)

    val persons = ctx.read.json("/tmp/persons.json").as[Person]
    persons.printSchema()
  }
}

在 IDE 中,当我运行 main 函数时,出现 2 个错误:
Error:(15, 67) Unable to find encoder for type stored in a Dataset.  Primitive types (Int, String, etc) and Product types (case classes) are supported by importing sqlContext.implicits._  Support for serializing other types will be added in future releases.
    val persons = ctx.read.json("/tmp/persons.json").as[Person]
                                                                  ^

Error:(15, 67) not enough arguments for method as: (implicit evidence$1: org.apache.spark.sql.Encoder[Person])org.apache.spark.sql.Dataset[Person].
Unspecified value parameter evidence$1.
    val persons = ctx.read.json("/tmp/persons.json").as[Person]
                                                                  ^

但在 Spark Shell 中,我可以运行此作业而不会出现任何错误。问题是什么?

最佳答案

错误消息说 Encoder无法采取Person案例类。

Error:(15, 67) Unable to find encoder for type stored in a Dataset.  Primitive types (Int, String, etc) and Product types (case classes) are supported by importing sqlContext.implicits._  Support for serializing other types will be added in future releases.

将案例类的声明移到 SimpleApp 范围之外.

关于scala - 为什么使用案例类对 JSON 进行编码时会出现错误 "Unable to find encoder for type stored in a Dataset"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34715611/

相关文章:

java - 在 Apache Spark 中跨执行器共享数据

java - 在 Apache Spark Dataset<Row> 上应用 flatMap 操作时出现意外的编码器行为

scala - 使用 (...)(...) 函数时参数过多

Java 嵌套 Map 到 Scala 嵌套序列

scala - 使用 Scala 类作为 UDF 与 pyspark

apache-spark - Spark RDD 映射 1 到多个

scala - IntelliJ 源代码编辑器显示错误的编译错误

unit-testing - 如何在IntelliJ idea中运行scala ScalaTest?

java - Spark CSV Reader 引用数字

json - 将 JSON 文件读入 Spark 数据集并从单独的 Map 添加列