1

I have a Dataframe like this:

+---------------------------------------------------------------------+
|ARRAY                                                                |
+---------------------------------------------------------------------+
|[WrappedArray(1, 2, 3), WrappedArray(4, 5, 6), WrappedArray(7, 8, 9)]|
+---------------------------------------------------------------------+

I use this code to create it:

case class MySchema(arr: Array[Array[Int]])
val df = sc.parallelize(Seq(
    Array(Array(1,2,3),
          Array(4,5,6),
          Array(7,8,9))))
      .map(x => MySchema(x))
      .toDF("ARRAY")

I would like to get a result like this:

+-----------+
|ARRAY      |                                                          |
+-----------+
|[1, 2, 3]  |
|[4, 5, 6]  |
|[7, 8, 9]  |
+-----------+

Do you have any idea?

I already try to call an udf to do a flatmap(x => x) on my Array line but I get an incorrect result :

+---------------------------+
|ARRAY                      |
+---------------------------+
|[1, 2, 3, 4, 5, 6, 7, 8, 9]|
+---------------------------+

Thank you for your help

1 Answer 1

4

You can explode:

import org.apache.spark.sql.functions.{col, explode}
df.select(explode(col("array")))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.