Get start date & end date from the range of timestamp

Viewed 492

I have a dataframe in Spark (Scala) from a large csv file.

Dataframe is something like this

key| col1 | timestamp            |
---------------------------------
1  | aa  | 2019-01-01 08:02:05.1 |
1  | aa  | 2019-09-02 08:02:05.2 | 
1  | cc  | 2019-12-24 08:02:05.3 |
2  | dd  | 2013-01-22 08:02:05.4 | 

I need to add two columns start_date & end_date something like this

key| col1 | timestamp            | start date              | end date              | 
---------------------------------+---------------------------------------------------
1  | aa  | 2019-01-01 08:02:05.1 | 2017-01-01 08:02:05.1   | 2018-09-02 08:02:05.2 |
1  | aa  | 2019-09-02 08:02:05.2 | 2018-09-02 08:02:05.2   | 2019-12-24 08:02:05.3 |
1  | cc  | 2019-12-24 08:02:05.3 | 2019-12-24 08:02:05.3   | NULL                  |
2  | dd  | 2013-01-22 08:02:05.4 | 2013-01-22 08:02:05.4   | NULL                  |

Here,

for each column "key", end_date is next timestamp for the same key. However, "end_date" for the latest date should be NULL.

What I tried so far:

I tried to use window function to calculate rank for each partition

something like this

 
  var df = read_csv() 

  //copy timestamp to start_date
  df = df
       .withColumn("start_date", df.col("timestamp"))

  //add null value to the end_date
  df = df.withColumn("end_date", typedLit[Option[String]](None))

  val windowSpec = Window.partitionBy("merge_key_column").orderBy("start_date")
   
  df
  .withColumn("rank", dense_rank()
  .over(windowSpec))
  .withColumn("max", max("rank").over(Window.partitionBy("merge_key_column")))

So far, I haven't got the desired output.

1 Answers

Use window lead function for this case.

Example:

val df=Seq((1,"aa","2019-01-01 08:02:05.1"),(1,"aa","2019-09-02 08:02:05.2"),(1,"cc","2019-12-24 08:02:05.3"),(2,"dd","2013-01-22 08:02:05.4")).toDF("key","col1","timestamp")
import org.apache.spark.sql.expressions._
import org.apache.spark.sql.functions._
import org.apache.spark.sql._
val df1=df.withColumn("start_date",col("timestamp"))
val windowSpec = Window.partitionBy("key").orderBy("start_date")

df1.withColumn("end_date",lead(col("start_date"),1).over(windowSpec)).show(10,false)
//+---+----+---------------------+---------------------+---------------------+
//|key|col1|timestamp            |start_date           |end_date             |
//+---+----+---------------------+---------------------+---------------------+
//|1  |aa  |2019-01-01 08:02:05.1|2019-01-01 08:02:05.1|2019-09-02 08:02:05.2|
//|1  |aa  |2019-09-02 08:02:05.2|2019-09-02 08:02:05.2|2019-12-24 08:02:05.3|
//|1  |cc  |2019-12-24 08:02:05.3|2019-12-24 08:02:05.3|null                 |
//|2  |dd  |2013-01-22 08:02:05.4|2013-01-22 08:02:05.4|null                 |
//+---+----+---------------------+---------------------+---------------------+
Related