【问题标题】:how to use spark lag and lead over group by and order by如何使用火花滞后和领先于 group by 和 order by
【发布时间】:2018-05-01 08:34:16
【问题描述】:

我使用:`

dataset.withColumn("lead",lead(dataset.col(start_date),1).over(orderBy(start_date)));

` 我只想按 trackId 添加组,以便通过任何 agg 函数来领导每个组的工作:

+----------+---------------------------------------------+
|  trackId |  start_time    |  end_time   |      lead    |
+-----+--------------------------------------------------+
|  1       | 12:00:00       |   12:04:00  |     12:05:00 |
+----------+---------------------------------------------+
|  1       | 12:05:00       |   12:08:00  |    12:20:00  |  
+----------+---------------------------------------------+
|  1       | 12:20:00       |   12:22:00  |     null     | 
+----------+---------------------------------------------+
|  2       | 13:00:00       |   13:04:00  |    13:05:00 |
+----------+---------------------------------------------+
|  2       | 13:05:00       |   13:08:00  |    13:20:00  |  
+----------+---------------------------------------------+
|  2       | 13:20:00       |   13:22:00  |     null     | 
+----------+---------------------------------------------+

有什么帮助吗?

【问题讨论】:

    标签: apache-spark apache-spark-sql apache-spark-dataset


    【解决方案1】:

    你所缺少的只是Window 关键字和partitionBy 方法调用

    import org.apache.spark.sql.expressions._
    import org.apache.spark.sql.functions._
    dataset.withColumn("lead",lead(col("start_time"),1).over(Window.partitionBy("trackId").orderBy("start_time")))
    

    【讨论】:

      【解决方案2】:

      你需要使用Window

      val df = Seq(
        (1, "12:00:00", "12:04:00"),
        (1, "12:05:00", "12:08:00"),
        (1, "12:20:00", "12:22:00"),
        (2, "13:00:00", "13:04:00"),
        (2, "13:05:00", "13:08:00"),
        (2, "13:20:00", "13:22:00")
      ).toDF( "trackId","start_time","end_time" )
      
      val window  = Window.partitionBy("trackId").orderBy("start_time")
      
      df.withColumn("lead",lead(col("start_time"),1).over(window))
      

      如果您不想要 null,那么您也可以将默认值传递为 lead($"start_time",1, defaultValue)

      结果:

      +-------+----------+--------+--------+
      |trackId|start_time|end_time|lead    |
      +-------+----------+--------+--------+
      |1      |12:00:00  |12:04:00|12:05:00|
      |1      |12:05:00  |12:08:00|12:20:00|
      |1      |12:20:00  |12:22:00|null    |
      |2      |13:00:00  |13:04:00|13:05:00|
      |2      |13:05:00  |13:08:00|13:20:00|
      |2      |13:20:00  |13:22:00|null    |
      +-------+----------+--------+--------+
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-06
        • 1970-01-01
        • 1970-01-01
        • 2011-06-28
        • 2012-04-19
        相关资源
        最近更新 更多