Galay
Galay

Reputation: 17

How to combine the value of a column with same key in Scala?

Suppose I have this data

ColumnA ColumnB
row1 valueA
row1 valueB
row2 valueB

How can I join the value of Column B that has the same value in Column A? Example:

ColumnA ColumnB
row1 valueA, valueB
row2 valueB

Upvotes: 0

Views: 269

Answers (1)

Roshan N
Roshan N

Reputation: 1053

You can use collect_set and concat_ws.

df.select("ColumnA","ColumnB")
  .groupBy("ColumnA")
  .agg(concat_ws(",",collect_set("ColumnB")))

Upvotes: 0

Related Questions