gpt4 book ai didi

scala - 适用于Spark Case类的Scala通用编码器

转载 作者:行者123 更新时间:2023-12-04 16:38:34 26 4
gpt4 key购买 nike

我如何获得此方法进行编译。奇怪的是,隐性 Spark 已被导入。

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

这是错误:
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]

最佳答案

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

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

关于scala - 适用于Spark Case类的Scala通用编码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44247874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com