I have a Spark dataframe parsed from an XML file which has data in the below format:
+---------+------------------------------------------------------------------------------------------+----------------------------+------------------------------------------------+
|id |a |b |c |
+---------+------------------------------------------------------------------------------------------+----------------------------+------------------------------------------------+
|191683250|[52396062, 55064266, 51149167, 53441347, 51309543, 51517728, 51543627, 68138995, 70180065]|[2, 2, 1, 3, 3, 2, 2, 27, 1]|[1.15, 0.8, 4.0, 2.49, 1.0, 2.8, 0.4, 0.49, 2.0]|
+---------+------------------------------------------------------------------------------------------+----------------------------+------------------------------------------------+
I need the output data in the format:
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|id |a |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|191683250|Array[(52396062,2,1.5), (55064266,2,0.8), (51149167,1,4.0), (53441347,3,2.49), (51309543,3,1.0), (51517728,2,2.8), (51543627,2,0.4), (68138995,27,0.49), (70180065,1,2.0)]|
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
ie., I need an Array of StructTypes/tuples. Im just stuck on how to proceed on this.
Could you please point me how can I achieve this in Spark using Scala. Appreciate any help.
In Spark >= 2.4 this can be solved using the arrays_zip
function:
val df = // Example dataframe in question
val df2 = df.withColumn("a", arrays_zip($"a", $"b", $"c"))
.drop("b", "c")
For older versions of Spark, use an UDF
:
val convertToArray = udf((a: Seq[Int], b: Seq[Int], c: Seq[Double]) => {
a zip b zip c map { case((a,b),c) => (a,b,c)}
})
val df = // Example dataframe in question
val df2 = df.withColumn("a", convertToArray($"a", $"b", $"c"))
.drop("b", "c")
The resulting dataframe:
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|id |a |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|191683250|[[52396062,2,1.15], [55064266,2,0.8], [51149167,1,4.0], [53441347,3,2.49], [51309543,3,1.0], [51517728,2,2.8], [51543627,2,0.4], [68138995,27,0.49], [70180065,1,2.0]]|
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
This answer is not as perfect as @Shaido's answer. This answer is just a possibility of doing in another way
df.select($"id",
array(struct($"a"(0), $"b"(0), $"c"(0)),
struct($"a"(1), $"b"(1), $"c"(1)),
struct($"a"(2), $"b"(2), $"c"(2)),
struct($"a"(3), $"b"(3), $"c"(3)),
struct($"a"(4), $"b"(4), $"c"(4)),
struct($"a"(5), $"b"(5), $"c"(5)),
struct($"a"(6), $"b"(6), $"c"(6)),
struct($"a"(7), $"b"(7), $"c"(7))).as("a"))
.show(false)
You should be getting
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------+
|id |a |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------+
|191683250|[[52396062,2,1.15], [55064266,2,0.8], [51149167,1,4.0], [53441347,3,2.49], [51309543,3,1.0], [51517728,2,2.8], [51543627,2,0.4], [68138995,27,0.49]]|
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.