简体   繁体   中英

How to construct a column based on other columns using dataframe in Spark Java?

I need to make a column based on other columns. I have this dataframe DF1:

---------------------
  a   | b    | c    |
------|------|------|
value1|value2|value3|
------|------|------|

The expected result is that I add a column d with the values of the columns a, b and c separated with "|":

------------------------------------------
  a   | b    | c    | d
------|------|------|---------------------
value1|value2|value3|value1|value2|value3
------|------|------|---------------------

How can I do this in Spark Java using dataframe?

Use expr("concat('|',*)") , Check below code.

import org.apache.spark.sql.functions.expr

df.withColumn("d",expr("concat_ws('|',*)")).show(false)
+------+------+------+--------------------+
|a     |b     |c     |d                   |
+------+------+------+--------------------+
|value1|value2|value3|value1|value2|value3|
+------+------+------+--------------------+

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM