val data = Seq((0,"qwe","true"),(1,"asd","false"),(2,"rty","false"),(3,"tyu","true")).toDF("id","name","has_bank_acc")
scala> data.show
+---+----+------------+
| id|name|has_bank_acc|
+---+----+------------+
| 0| qwe| true|
| 1| asd| false|
| 2| rty| false|
| 3| tyu| true|
+---+----+------------+
val data2 = Seq((0,"qwe","true"),(5,"hjk","false"),(8,"oiu","false"),(7,"nmb","true")).toDF("id","name","has_email_acc")
scala> data2.show
+---+----+-------------+
| id|name|has_email_acc|
+---+----+-------------+
| 0| qwe| true|
| 5| hjk| false|
| 8| oiu| false|
| 7| nmb| true|
+---+----+-------------+
val data_cols = data.columns
val data2_cols = data2.columns
val transformedData = data2_cols.diff(data_cols).foldLeft(data) {
case (df, (newCols)) =>
df.withColumn(newCols, lit("null"))
}
val transformedData2 = data_cols.diff(data2_cols).foldLeft(data2) {
case (df, (newCols)) =>
df.withColumn(newCols, lit("null"))
}
val finalData = transformedData2.unionByName(transformedData)
finalData.show
scala> finalData.show
+---+----+-------------+------------+
| id|name|has_email_acc|has_bank_acc|
+---+----+-------------+------------+
| 0| qwe| true| null|
| 5| hjk| false| null|
| 8| oiu| false| null|
| 7| nmb| true| null|
| 0| qwe| null| true|
| 1| asd| null| false|
| 2| rty| null| false|
| 3| tyu| null| true|
+---+----+-------------+------------+