0

I am trying to transpose column to rows . First to concat columns into an array Second step is to explode the array column

Explode function is not working ..

>>> filteredPaths1.select(   array ( concat( col("v1.id"),lit(","),col("v2.id"),lit(",") ,col("v2.id") )).alias("test")     ).printSchema()
root
 |-- test: array (nullable = false)
 |    |-- element: string (containsNull = true)

Values in array column -

>>> filteredPaths1.select(   array ( concat( col("v1.id"),lit(","),col("v2.id"),lit(",") ,col("v2.id") )).alias("test")     ).show(10,False)
+--------------------------------------------------------------+                ]
|test                                                          |
+--------------------------------------------------------------+
|[Sorter_SAMPLE_CUSTOMER,Join_Source_Target,Join_Source_Target]|
+--------------------------------------------------------------+

However when trying to explode the array column it's not creating new rows , just giving the same output -

>>> filteredPaths1.select(   explode (array ( concat( col("v1.id"),lit(","),col("v2.id"),lit(",") ,col("v2.id") )).alias("test") )    ).show(10,False)
+------------------------------------------------------------+                  ]
|col                                                         |
+------------------------------------------------------------+
|Sorter_SAMPLE_CUSTOMER,Join_Source_Target,Join_Source_Target|
+------------------------------------------------------------+

Any reason explode is not working ?

1
  • concat creates a single string column out of your three. Commented Jun 20, 2020 at 16:05

1 Answer 1

1

Because of you are using array(concat(..)) means array of one value & exploding this you will get only one row, i.e same value.

Use split instead of array

filteredPaths1.select(explode(split(concat_ws(",",col("v1.id"),col("v2.id"),col("v2.id")),",")).alias("test"))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing it out , I have removed concat all together and it worked as well but thanks for providing solution .

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.