OOvic
OOvic

Reputation: 47

Add a new Column to my DataSet in spark Java API

I'm new In Spark . My DataSet contains two columns. I want to add the third that is the sum of the two columns.

My DataSet is:

+---------+-------------------+
|C1       |       C2          |
+---------+-------------------+
|   44    |                 10|
|   55    |                 10|
+---------+-------------------+

I want to obtain a DataSet like this:

+---------+-------------------+---------+
|C1       |       C2          |   C3    |
+---------+-------------------+---------+
|   44    |                 10|   54    |
|   55    |                 10|   65    |
+---------+-------------------+---------+

Any help will be apprecieted.

Upvotes: 0

Views: 2095

Answers (1)

user9743288
user9743288

Reputation:

The correct solution is:

df.withColumn("C3", df.col1("C1").plus(df.col("C2")));

or

df.selectExpr("*", "C1 + C2");

For more arithmetic operators check Java-specific expression operators in the Column documentation.

Upvotes: 2

Related Questions