scala generic encoder for spark case class

♀尐吖头ヾ 提交于 2019-12-06 03:16:04

问题


How can I get this method to compile. Strangely, sparks implicit are already imported.

def loadDsFromHive[T <: Product](tableName: String, spark: SparkSession): Dataset[T] = {
    import spark.implicits._
    spark.sql(s"SELECT * FROM $tableName").as[T]
  }

This is the error:

Unable to find encoder for type stored in a Dataset.  Primitive types (Int, String, etc) and Product types (case classes) are supported by importing spark.implicits._  Support for serializing other types will be added in future releases.
[error]     spark.sql(s"SELECT * FROM $tableName").as[T]

回答1:


According to the source code for org.apache.spark.sql.SQLImplicits, you need the type class TypeTag for your type, in order for the implicit Encoder to exist:

import scala.reflect.runtime.universe.TypeTag
def loadDsFromHive[T <: Product: TypeTag](tableName: String, spark: SparkSession): Dataset[T] = ...


来源:https://stackoverflow.com/questions/44247874/scala-generic-encoder-for-spark-case-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!