I'm new to Scala and still learning about most of the features. I'm trying to convert a Dataset[T] to Json. I'm using json play to create implicit Writes. Type parameters work fine when I load my Dataset:
def processEvent[T](spark: SparkSession, inputPath: String)(implicit encoder: Encoder[T]): Unit = {
val ds = spark.read.parquet(inputPath).as[T]
ds.collect().foreach { event =>
val serializedEvent = eventToJson[T](spark, event)
postEvent(serializedEvent)
}
}
But in the call to EventToJson
, I get the error: No Json serializer found for type T. Try to implement an implicit Writes or Format for this type.
def eventToJson[T](spark: SparkSession, event: T): String = {
Json.toJson(event).toString()
}
When I replace the parameterized type with one of my case classes, the code works fine:
def eventToJson(spark: SparkSession, event: MyCaseClass): String = {
Json.toJson(event).toString()
}
Why doesn't the parameterized type find the corresponding case class and implicit Writes?