How can I return sub class objects in a Dataset of parent class?. Below code compiles but last line fails at runtime with " scala.ScalaReflectionException: is not a term". Any help is highly appreciated.
Case classes Apple and Orange extends Fruit trait. I am trying to return objects of Apple and Orange in a Fruit ref.
import org.apache.spark.sql._
object Test {
case class Item(name: String, itemType: Int, count: Long)
trait Fruit extends Product{
def name: String
def count: Long
}
case class Apple(name: String, count: Long) extends Fruit
case class Orange(name: String, count: Long) extends Fruit
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder
.master("local[2]")
.getOrCreate()
import spark.implicits._
val ds = Seq(("apple", 1, 1), ("orange", 2, 1))
.toDF("name", "itemType", "count").as[Item]
ds.map(createFruits).show
}
def createFruits(item: Item): Fruit ={
item.itemType match{
case 1 => Apple(item.name, item.count)
case 2 => Orange(item.name, item.count)
}
}
}