Это должно помочь:
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.functions.{explode, udf, typedLit}
import org.apache.spark.sql.SparkSession
object test extends App {
val conf: SparkConf = new SparkConf().setAppName("test").setMaster("local[*]")
val sc: SparkContext = new SparkContext(conf)
val spark = SparkSession
.builder()
.appName("test")
.master("local[*]")
.getOrCreate()
import spark.implicits._
val df = spark.read.format("csv").option("header", true).load("file:///Users/test/Desktop/file2.csv")
val header: Seq[String] = df.columns.toSeq.map(x => x.trim)
val df1 = df.withColumn("header", typedLit(header))
val transform = udf((col0: String, col1: String, col2: String, col3: String, header: Seq[String]) => {
Array(
((header(0), col0.trim, col3.trim),1),
((header(1), col1.trim, col3.trim),1),
((header(2), col2.trim, col3.trim),1)
)
})
val df2 = df1.withColumn("transformed",transform($"A1", $" A2", $" A3", $" Class", $"header"))
.withColumn("exploded", explode($"transformed"))
.select($"exploded")
df2.take(1).foreach(println)
}
Вывод: https://imgur.com/a/Je1M3Dx