1

I tried the options mentioned in the following post but it didn't work in java - finding the difference of two columns in spark dataframes and appending to a new column

subtract two columns with null in spark dataframe

Please let me know how can I find difference between two column of a dataset using Spark in Java.

2
  • can you post your code and explain what does not work as you expect? Commented Jun 13, 2018 at 20:41
  • I was able to resolve it in a regular sql way instead of using when() function. posted by answer below. Commented Jun 14, 2018 at 16:12

2 Answers 2

1

You can do it next way. Lets assume, you want additional column with difference:

Dataset<Row> ds = // your dataset
ds.withColumn("diff_col", new Column("col1").$minus(new Column("col2"));
Sign up to request clarification or add additional context in comments.

Comments

0

Resolved the issue by using sql way of subtracting the two column values.

sqlContext.sql("Select col1, sum(col2)-sum(col3) as resCol from temp group by col1") 

Comments

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.