spark中各窗口函数对于窗口的要求

窗口参数

class WindowSpec private[sql](
partitionSpec: Seq[Expression],
orderSpec: Seq[SortOrder],
frame: WindowFrame)
class WindowSpec private[sql](
    partitionSpec: Seq[Expression],
    orderSpec: Seq[SortOrder],
    frame: WindowFrame)
class WindowSpec private[sql]( partitionSpec: Seq[Expression], orderSpec: Seq[SortOrder], frame: WindowFrame)

1、Aggregate Functions: 聚合函数,比如:sum(…)、 max(…)、min(…)、avg(…)等.

对于窗口要求无所限制,但窗口参数具备相应的默认值:

partitionSpec 默认是 SinglePartition(单个分区)

orderSpec 默认不排序

frame

  • 当窗口缺少 orderSpec 时,默认 specifiedwindowframe(RowFrame, unboundedpreceding$(), unboundedfollowing$())
  • 当窗口具备 orderSpec 时,默认 specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())

2、Sort Functions: 排序函数 , 比如:rank()、row_number()、dense_rank() 等.

要求运行的窗口必须具备 orderSpec

并且 frame必须是 specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())

frame 可以不指定,让程序默认生成

partitionSpec 默认是 SinglePartition

3、Analytics Functions: 统计和分析函数,比如:lead(…)、lag(…) 等.

要求运行的窗口必须具备 orderSpec

frame要求使用 RowFrame

precedingfollowing 会根据 函数的offset参数 动态变化,

例如:

lead($"id", offset = 1) over spec.orderBy("id")
// specifiedwindowframe(RowFrame, 1, 1)
lead($"id", offset = 3) over spec.orderBy("id")
// specifiedwindowframe(RowFrame, 3, 3)
lag($"id", offset = 2) over spec.orderBy("id")
// specifiedwindowframe(RowFrame, -2, -2)
lag($"id", offset = 4) over spec.orderBy("id")
// specifiedwindowframe(RowFrame, -4, -4)
lead($"id", offset = 1) over spec.orderBy("id")
// specifiedwindowframe(RowFrame, 1, 1)

lead($"id", offset = 3) over spec.orderBy("id")
// specifiedwindowframe(RowFrame, 3, 3)

lag($"id", offset = 2) over spec.orderBy("id")
// specifiedwindowframe(RowFrame, -2, -2)

lag($"id", offset = 4) over spec.orderBy("id")
// specifiedwindowframe(RowFrame, -4, -4)
lead($"id", offset = 1) over spec.orderBy("id") // specifiedwindowframe(RowFrame, 1, 1) lead($"id", offset = 3) over spec.orderBy("id") // specifiedwindowframe(RowFrame, 3, 3) lag($"id", offset = 2) over spec.orderBy("id") // specifiedwindowframe(RowFrame, -2, -2) lag($"id", offset = 4) over spec.orderBy("id") // specifiedwindowframe(RowFrame, -4, -4)

frame 可以不指定,让程序默认生成

partitionSpec 默认是 SinglePartition

不难看出,lead 和 lag 函数就是通过 frame 限制窗口的大小和位置来实现的

来源链接:https://www.cnblogs.com/blog-moondream/p/18219603

© 版权声明
THE END
支持一下吧
点赞7 分享
评论 抢沙发
头像
请文明发言!
提交
头像

昵称

取消
昵称表情代码快捷回复

    暂无评论内容