Created
November 11, 2016 18:18
-
-
Save pjurczenko/4bfe1ff7774feb21272ea5d91c1485cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val input = sc.parallelize(Seq( | |
(43.45, 19840, "A345", Seq("2014-12-26", "2013-12-12"), Seq("232323","45466"), Seq("123", "234")), | |
(43.45, 19840, "A345", Seq("2010-03-16", "2013-16-12"), Seq("34343","45454"), Seq("345", "456")) | |
)).toDF("amt", "id", "num", "start_date", "identifier", "another_column") | |
val zipArrays = udf { seqs: Seq[Seq[String]] => | |
for(i <- seqs.head.indices) yield seqs.fold(Seq.empty)((accu, seq) => accu :+ seq(i)) | |
} | |
val columnsToSelect = Seq($"amt", $"id", $"num") | |
val columnsToZip = Seq($"start_date", $"identifier", $"another_column") | |
val outputColumns = columnsToSelect ++ columnsToZip.zipWithIndex.map { case (column, index) => | |
$"col".getItem(index).as(column.toString()) | |
} | |
val output = input.select($"amt", $"id", $"num", explode(zipArrays(array(columnsToZip: _*)))).select(outputColumns: _*) | |
output.show() | |
/* | |
+-----+-----+----+----------+----------+--------------+ | |
| amt| id| num|start_date|identifier|another_column| | |
+-----+-----+----+----------+----------+--------------+ | |
|43.45|19840|A345|2014-12-26| 232323| 123| | |
|43.45|19840|A345|2013-12-12| 45466| 234| | |
|43.45|19840|A345|2010-03-16| 34343| 345| | |
|43.45|19840|A345|2013-16-12| 45454| 456| | |
+-----+-----+----+----------+----------+--------------+ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment