【发布时间】:2018-01-01 12:41:39
【问题描述】:
我正在使用 Spark 数据集读取 csv 文件。我想制作一个多态函数来为许多文件执行此操作。函数如下:
def loadFile[M](file: String):Dataset[M] = {
import spark.implicits._
val schema = Encoders.product[M].schema
spark.read
.option("header","false")
.schema(schema)
.csv(file)
.as[M]
}
我得到的错误是:
[error] <myfile>.scala:45: type arguments [M] do not conform to method product's type parameter bounds [T <: Product]
[error] val schema = Encoders.product[M].schema
[error] ^
[error] <myfile>.scala:50: 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] .as[M]
[error] ^
[error] two errors found
我不知道如何处理第一个错误。我尝试添加与产品定义相同的方差(M <: product typetag available for m>
如果我传入已经从编码器生成的模式,则会收到错误:
[error] Unable to find encoder for type stored in a Dataset
【问题讨论】:
标签: scala generics apache-spark