在 pyspark 或 hive 中查找两个连续状态之间的持续时间

Find time duration between two consecutive states in pyspark or hive

我有一个数据框,看起来像下面有列: ID, STATE and TIMESTAMP。 数据框按照 ID and TIMESTAMP 排序。 我们需要找出 state S1 to S2.

之间的时间间隔

注意:对于特定的 ID,我们可以在 S1 到 S2 之间进行多次转换。状态总是以 S1 开始,以 S2 结束。

查看所附图片了解更多信息:

Input in Blue and expected output in Green

     select id, 
        unix_timestamp(timestamp) - 
        unix_timestamp(lag(timestamp) over(partition by id order by timestamp)) as time_diff
        from table;