簡體   English   中英

使用案例類編碼JSON時,為什么錯誤“無法找到存儲在數據集中的類型的編碼器”?

[英]Why is the error “Unable to find encoder for type stored in a Dataset” when encoding JSON using case classes?

我寫過火花工作:

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.

將case類的聲明SimpleApp的范圍之外。

如果添加你有同樣的錯誤sqlContext.implicits._spark.implicits._SimpleApp (順序並不重要)。

刪除一個或另一個將是解決方案:

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進行測試

有趣的是,如果你添加兩個相同的對象,你就不會有問題。

@Milad Khajavi

在對象SimpleApp之外定義Person case類。 另外,在main()函數中添加import sqlContext.implicits._。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM