【发布时间】:2020-08-18 16:26:16
【问题描述】:
我的要求是从 src-resources 文件夹中读取 csv 文件并将它们转换为 DF 。我的代码如下。 流派和月份是案例类,我正在使用它来为我的列表提供结构,以便我可以将它们转换为 DF 。在下面的代码中,我应该使用什么来代替 _ 以便我可以获得 List[Genre] 或 List[Month ] 基于将要传递的文件名值。
trait IndexReader[T] {
def read(filePath: String, sep: String,fileName:String): List[T] = {
val stream: InputStream = getClass.getResourceAsStream("/" + filePath)
val lines: Iterator[String] = scala.io.Source.fromInputStream(stream).getLines
lines.map(line => {normalize(line, sep,fileName)}).toList
}
def normalize(line: String, sep: String,fileName:String): T
}
class BrandReader extends IndexReader[_] {
override def normalize(line: String, sep: String,fileName:String):_ = {
val lineSplit: Seq[String] = line.split(sep).toList
val file = fileName match {
case "indexGenre" => Genre(lineSplit(0), lineSplit(1))
case "indexMonth" => Month(lineSplit(0), lineSplit(1))
}
file
}
}
【问题讨论】:
-
Scala 3 将具有联合类型,因此您可以使用
Genre|Month,但在此之前,请使用两者通用的特征或抽象类。