Spark incremental loading overwrite old record

后端 未结 3 1350
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 07:39

I have a requirement to do the incremental loading to a table by using Spark (PySpark)

Here\'s the example:

Day 1

id | value
-----------
1  |         


        
3条回答
  •  既然无缘
    2020-12-16 08:34

    Workaround, add a date column in dataframe, then rank based on id and order by date in descending and take the rank == 1. It will always give you the latest record based on id.

    df.("rank", rank().over(Window.partitionBy($"id").orderBy($"date".desc)))
      .filter($"rank" === 1)
      .drop($"rank")
      .orderBy($"id")
      .show
    

提交回复
热议问题