【问题标题】:Why is the error "Unable to find encoder for type stored in a Dataset" when encoding JSON using case classes?为什么使用案例类对 JSON 进行编码时出现错误“无法找到存储在数据集中的类型的编码器”?
【发布时间】:2016-04-15 09:53:44
【问题描述】:

我已经写了 spark job:

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 中,我可以运行此作业而不会出现任何错误。有什么问题?

【问题讨论】:

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


    【解决方案1】:

    错误消息指出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.
    

    将case类的声明移到SimpleApp的范围之外。

    【讨论】:

    • 为什么范围界定在这里有什么不同?我在使用 REPL 时遇到了这个错误。
    • 我试图理解为什么案例类的范围会产生影响,如果你能指出我可以阅读和理解的任何资源,这将有很大的帮助。 scala 隐式相当新:(@jacek-laskowski
    • 我认为我无法解释为什么该解决方案会如此运作。我依稀记得它与implicits无关,implicits只是一种插入代码并认为代码本身是根本原因的机制。
    【解决方案2】:

    如果在SimpleApp 中添加sqlContext.implicits._spark.implicits._ 也会出现同样的错误(顺序无关紧要)。

    删除一个或另一个将是解决方案:

    val spark = SparkSession
      .builder()
      .getOrCreate()
    
    val sqlContext = spark.sqlContext
    import sqlContext.implicits._ //sqlContext OR spark implicits
    //import spark.implicits._ //sqlContext OR spark implicits
    
    case class Person(age: Long, city: String)
    val persons = ctx.read.json("/tmp/persons.json").as[Person]
    

    使用 Spark 2.1.0

    测试

    有趣的是,如果你将同一个对象添加两次,你不会有问题。

    【讨论】:

      【解决方案3】:

      @Milad Khajavi

      在对象 SimpleApp 之外定义 Person 案例类。 另外,在 main() 函数中添加 import sqlContext.implicits._。

      【讨论】:

        猜你喜欢
        • 2016-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多