1

I use Apache Spark 1.6.2 in Java.

I have a DataFrame containing:

  • a date in a creation_date field,
  • an end date in a close_date field.

If the business is not closed, then the value in close_date is null.

I would like to:

  • add an extra column to my DataFrame called last_date_business
  • fill it with the value of close_date
  • if close_date is null, then use current_date()

Can I ask Spark to do it or should I do it manually?

1 Answer 1

8

All you need here is a coalesce:

import static org.apache.spark.sql.functions.*;

df.withColumn("last_date_business", coalesce(col("close_date"), current_date()));
Sign up to request clarification or add additional context in comments.

2 Comments

What is the equivalent in java?
@zero323 , I am using spark-sql -2.4.1v , i dont find coalesce() function ... what is the alternative I can use ? please suggest.

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.