I cannot give you all the details why this is not working but I am proposing a slightly alternative solution that we frequently use in our Scala Spark projects.
The signature of Encoders.product
looks like
product[T <: scala.Product](implicit evidence$5 : scala.reflect.runtime.universe.TypeTag[T])
which means tt expects a class that extends Product
trait and an implicit TypeTag.
Instead of a trait, you could create a case class
as case classes are extending Product
(and Serializable
) automatically.
In order to get a schema you could do:
case class A (
val name: String,
val size: String
)
def createSchema[T <: Product]()(implicit tag: scala.reflect.runtime.universe.TypeTag[T]) = Encoders.product[T].schema
val schema = createSchema[A]()
schema.printTreeString()
/*
root
|-- name: string (nullable = true)
|-- size: string (nullable = true)
*/
As said in the beginning, I can't explain all the details, just provide a working solution and hoping it fit your needs.