用于火花箱类的scala通用编码器

Geo*_*ler 5 generics scala apache-spark apache-spark-dataset apache-spark-encoders

我怎样才能编译这个方法.奇怪的是,隐含的火花已经被导入了.

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

这是错误:

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]
Run Code Online (Sandbox Code Playgroud)

Cyr*_*pet 15

根据源代码org.apache.spark.sql.SQLImplicits,您需要类型的类型类TypeTag,以便隐式Encoder存在:

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