一、Sparksql隱式轉換時異常描述
Cannot
create encoder for Option of Product type, because Product type is represented as a row, and the entire row can not be null in Spark SQL like normal databases.
You can wrap your type with Tuple1 if you do want top level null Product objects, e.g. instead of creating `Dataset[Option[MyClass]]`,
you can do something like `val ds: Dataset[Tuple1[MyClass]] = Seq(Tuple1(MyClass(...)), Tuple1(null)).toDS`
二、原因和解決方法
此問題主要是由於將data[Row]轉換成對應的的dataSet類型時,找不到對應的類型轉換導致的,需要為對應的類型添加隱式轉換,一般添加代碼:
implicit val registerKryoEncoder = Encoders.kryo[MyClass]