2016-01-11 3 views
9

Ich habe geschrieben Funken Job:Warum ist der Fehler "Encoder für Dateityp in einem Dataset nicht gefunden" beim Kodieren von JSON mit Fallklassen?

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() 
    } 
} 

In IDE, wenn ich die Hauptfunktion ausführen, 2 Fehler auftreten:

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] 
                   ^

aber in Spark-Shell ich ohne Fehler diesen Job ausführen kann. Was ist das Problem?

Antwort

20

Die Fehlermeldung besagt, dass die Encoder die Person Fallklasse nicht übernehmen kann.

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. 

Bewegen Sie die Erklärung des Falles Klasse außerhalb des Anwendungsbereichs der SimpleApp.

+12

Warum macht Scoping hier einen Unterschied? Ich bekomme diesen Fehler bei der Verwendung der REPL. – Wahbivic

2

Sie haben den gleichen Fehler, wenn Sie sqlContext.implicits._ und spark.implicits._ in SimpleApp hinzufügen (die Reihenfolge spielt keine Rolle).

ein Entfernen oder die andere wird die Lösung sein:

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] 

Getestet mit Funken 2.1.0

Das Komische ist, wenn Sie die gleichen Objekt implicits zweimal hinzufügen, werden Sie keine Probleme haben .

Verwandte Themen