Можно попробовать в spark-shell
case class Employee(id: Int, name: String, department: String, salary: Option[Double])
import org.apache.spark.sql.functions._
import spark.implicits._
case class Employee(id: Int, name: String, department: String, salary: Option[Double])
val data = List(Employee(1, "XYZ", "dep1", Some(1234.0)), Employee(0, null, "unknown", None)).toDS()
data.select($"id", to_json(struct($"id",$"name", $"department", $"salary")).as("json_data")).show(false)
return =>
|id |json_data |
+---+---------------------------------------------------------+
|1 |{"id":1,"name":"XYZ","department":"dep1","salary":1234.0}|
|0 |{"id":0,"department":"unknown"} |
Ожидается =>
|id |json_data |
+---+------------------------------------------------------------+
|1 |{"id":1,"name":"XYZ","department":"dep1","salary":1234.0} |
|0 |{"id":0,"name": null, "department":"unknown","salary":null} |
пустые поля ( name & зарплата ) также должна быть заполнена в результате json. Я не хочу использовать lit ("null") для заполнения нулевых значений