Unter der Annahme, dass Ihre Eingabe dataset
erzeugt wird unter Verwendung eines case class
als
case class infos(CustomerID: Int, CustomerName: String, Sun: Int, Mon: Int, Tue: Int)
Für Testzwecke Ich erstelle ein dataset
als
import sqlContext.implicits._
val ds = Seq(
infos(1, "ABC", 0, 12, 10),
infos(2, "DEF", 10, 0, 0)
).toDS
, die Ihre Eingabe dataset
+----------+------------+---+---+---+
|CustomerID|CustomerName|Sun|Mon|Tue|
+----------+------------+---+---+---+
|1 |ABC |0 |12 |10 |
|2 |DEF |10 |0 |0 |
+----------+------------+---+---+---+
geben sollte
Wie Sie Ihre endgültige dataset
erforderlich erfordert, dass Sie eine andere case class
als
case class finalInfos(CustomerID: Int, CustomerName: String, Day: String, Value: Int)
Finale dataset
, indem Sie folgende
erreicht, die zum Erstellen werden kann
val names = ds.schema.fieldNames
ds.flatMap(row => Array(finalInfos(row.CustomerID, row.CustomerName, names(2), row.Sun),
finalInfos(row.CustomerID, row.CustomerName, names(3), row.Mon),
finalInfos(row.CustomerID, row.CustomerName, names(4), row.Tue)))
, die Sie dataset
als
+----------+------------+---+-----+
|CustomerID|CustomerName|Day|Value|
+----------+------------+---+-----+
|1 |ABC |Sun|0 |
|1 |ABC |Mon|12 |
|1 |ABC |Tue|10 |
|2 |DEF |Sun|10 |
|2 |DEF |Mon|0 |
|2 |DEF |Tue|0 |
+----------+------------+---+-----+
prüfen geben sollten diese https://stackoverflow.com/questions/37864222/transpose-column-to-row-with-spark – dumitru