在scala中,如何指定泛型来扩展案例类?

时间:2016-04-09 03:12:48

标签: scala apache-spark

当我写一个特质时喜欢这个:

trait Extractor[T] {

    def parse(sc: SparkContext, path: String): RDD[T]

    def extract(sc: SparkContext, path: String, output: String): Unit = {
        val rdd = parse(sc, path)
        val sqlContext = new HiveContext(sc)
        import sqlContext.implicits._
        SaveUtil.saveAsOrc(rdd.toDF(), output)  // error
    }
}

我想将通用T指定给案例类:

case class X(uid: String, app: String, tag: String)

case class Y(uid: String, dvc: String, tag: String)

我是斯卡拉的新手。隐式转换和协变似乎不起作用。怎么样?

1 个答案:

答案 0 :(得分:0)

object ExtractorX extends Extractor[X] {
    override parse(sc: SparkContext, path: String): RDD[X] = ...
}

val extractorX = new Extractor[X] {
    override parse(sc: SparkContext, path: String): RDD[X] = ...
}