ak17
ak17

Reputation: 202

explode column with comma separated string in Spark SQL

I have below dataset

+----+-----------+
|col1|       col2|
+----+-----------+
|   1|val1, val2 |
|   2|val3, val4 |
+----+-----------+

Consider all values as String Now i want to convert it into below dataset

+----+-----------+
|col1|       col2|
+----+-----------+
|   1|val1       |
|   1|val2       |
|   2|val3       |
|   2|val4       |
+----+-----------+

How can I achieve this?

Upvotes: 0

Views: 9205

Answers (1)

Kombajn zbożowy
Kombajn zbożowy

Reputation: 10703

Use split to parse comma-separated values as an array, then explode to rearrange array elements into separate rows.

df.withColumn("col2", explode(split($"col2", ","))).show

Upvotes: 8

Related Questions