聊聊flink Table的Over Windows

本文主要研究一下flink table的over windows

聊聊flink Table的Over Windows

实例代码语言:javascript代码运行次数:0运行复制

Table table = input  .window([OverWindow w].as("w"))           // define over window with alias w  .select("a, b.sum over w, c.min over w"); // aggregate over the over window w

Over Windows类似SQL的over子句,它可以基于event-time、processing-time或者row-count;具体可以通过Over类来构造,其中必须设置orderBy、preceding及as方法;它有Unbounded及Bounded两大类Unbounded Over Windows实例代码语言:javascript代码运行次数:0运行复制

​// Unbounded Event-time over window (assuming an event-time attribute "rowtime").window(Over.partitionBy("a").orderBy("rowtime").preceding("unbounded_range").as("w"));​// Unbounded Processing-time over window (assuming a processing-time attribute "proctime").window(Over.partitionBy("a").orderBy("proctime").preceding("unbounded_range").as("w"));​// Unbounded Event-time Row-count over window (assuming an event-time attribute "rowtime").window(Over.partitionBy("a").orderBy("rowtime").preceding("unbounded_row").as("w")); // Unbounded Processing-time Row-count over window (assuming a processing-time attribute "proctime").window(Over.partitionBy("a").orderBy("proctime").preceding("unbounded_row").as("w"));

对于event-time及processing-time使用unbounded_range来表示Unbounded,对于row-count使用unbounded_row来表示UnboundedBounded Over Windows实例代码语言:javascript代码运行次数:0运行复制

// Bounded Event-time over window (assuming an event-time attribute "rowtime").window(Over.partitionBy("a").orderBy("rowtime").preceding("1.minutes").as("w"))​// Bounded Processing-time over window (assuming a processing-time attribute "proctime").window(Over.partitionBy("a").orderBy("proctime").preceding("1.minutes").as("w"))​// Bounded Event-time Row-count over window (assuming an event-time attribute "rowtime").window(Over.partitionBy("a").orderBy("rowtime").preceding("10.rows").as("w")) // Bounded Processing-time Row-count over window (assuming a processing-time attribute "proctime").window(Over.partitionBy("a").orderBy("proctime").preceding("10.rows").as("w"))

对于event-time及processing-time使用诸如1.minutes来表示Bounded,对于row-count使用诸如10.rows来表示BoundedTable.window

flink-table_2.11-1.7.0-sources.jar!/org/apache/flink/table/api/table.scala

代码语言:javascript代码运行次数:0运行复制

class Table(    private[flink] val tableEnv: TableEnvironment,    private[flink] val logicalPlan: LogicalNode) {​  //......  ​  @varargs  def window(overWindows: OverWindow*): OverWindowedTable = {​    if (tableEnv.isInstanceOf[BatchTableEnvironment]) {      throw new TableException("Over-windows for batch tables are currently not supported.")    }​    if (overWindows.size != 1) {      throw new TableException("Over-Windows are currently only supported single window.")    }​    new OverWindowedTable(this, overWindows.toArray)  }​  //......​}    

Table提供了OverWindow参数的window方法,用来进行Over Windows操作,它创建的是OverWindowedTableOverWindow

flink-table_2.11-1.7.0-sources.jar!/org/apache/flink/table/api/windows.scala

智谱清言 - 免费全能的AI助手 智谱清言 - 免费全能的AI助手

智谱清言 - 免费全能的AI助手

智谱清言 - 免费全能的AI助手 2 查看详情 智谱清言 - 免费全能的AI助手 代码语言:javascript代码运行次数:0运行复制

/**  * Over window is similar to the traditional OVER SQL.  */case class OverWindow(    private[flink] val alias: Expression,    private[flink] val partitionBy: Seq[Expression],    private[flink] val orderBy: Expression,    private[flink] val preceding: Expression,    private[flink] val following: Expression)

OverWindow定义了alias、partitionBy、orderBy、preceding、following属性Over

flink-table_2.11-1.7.0-sources.jar!/org/apache/flink/table/api/java/windows.scala

代码语言:javascript代码运行次数:0运行复制

object Over {​  /**    * Specifies the time attribute on which rows are grouped.    *    * For streaming tables call [[orderBy 'rowtime or orderBy 'proctime]] to specify time mode.    *    * For batch tables, refer to a timestamp or long attribute.    */  def orderBy(orderBy: String): OverWindowWithOrderBy = {    val orderByExpr = ExpressionParser.parseExpression(orderBy)    new OverWindowWithOrderBy(Array[Expression](), orderByExpr)  }​  /**    * Partitions the elements on some partition keys.    *    * @param partitionBy some partition keys.    * @return A partitionedOver instance that only contains the orderBy method.    */  def partitionBy(partitionBy: String): PartitionedOver = {    val partitionByExpr = ExpressionParser.parseExpressionList(partitionBy).toArray    new PartitionedOver(partitionByExpr)  }}​class OverWindowWithOrderBy(  private val partitionByExpr: Array[Expression],  private val orderByExpr: Expression) {​  /**    * Set the preceding offset (based on time or row-count intervals) for over window.    *    * @param preceding preceding offset relative to the current row.    * @return this over window    */  def preceding(preceding: String): OverWindowWithPreceding = {    val precedingExpr = ExpressionParser.parseExpression(preceding)    new OverWindowWithPreceding(partitionByExpr, orderByExpr, precedingExpr)  }​}​class PartitionedOver(private val partitionByExpr: Array[Expression]) {​  /**    * Specifies the time attribute on which rows are grouped.    *    * For streaming tables call [[orderBy 'rowtime or orderBy 'proctime]] to specify time mode.    *    * For batch tables, refer to a timestamp or long attribute.    */  def orderBy(orderBy: String): OverWindowWithOrderBy = {    val orderByExpr = ExpressionParser.parseExpression(orderBy)    new OverWindowWithOrderBy(partitionByExpr, orderByExpr)  }}​class OverWindowWithPreceding(    private val partitionBy: Seq[Expression],    private val orderBy: Expression,    private val preceding: Expression) {​  private[flink] var following: Expression = _​  /**    * Assigns an alias for this window that the following `select()` clause can refer to.    *    * @param alias alias for this over window    * @return over window    */  def as(alias: String): OverWindow = as(ExpressionParser.parseExpression(alias))​  /**    * Assigns an alias for this window that the following `select()` clause can refer to.    *    * @param alias alias for this over window    * @return over window    */  def as(alias: Expression): OverWindow = {​    // set following to CURRENT_ROW / CURRENT_RANGE if not defined    if (null == following) {      if (preceding.resultType.isInstanceOf[RowIntervalTypeInfo]) {        following = CURRENT_ROW      } else {        following = CURRENT_RANGE      }    }    OverWindow(alias, partitionBy, orderBy, preceding, following)  }​  /**    * Set the following offset (based on time or row-count intervals) for over window.    *    * @param following following offset that relative to the current row.    * @return this over window    */  def following(following: String): OverWindowWithPreceding = {    this.following(ExpressionParser.parseExpression(following))  }​  /**    * Set the following offset (based on time or row-count intervals) for over window.    *    * @param following following offset that relative to the current row.    * @return this over window    */  def following(following: Expression): OverWindowWithPreceding = {    this.following = following    this  }}

Over类是创建over window的帮助类,它提供了orderBy及partitionBy两个方法,分别创建的是OverWindowWithOrderBy及PartitionedOverPartitionedOver提供了orderBy方法,创建的是OverWindowWithOrderBy;OverWindowWithOrderBy提供了preceding方法,创建的是OverWindowWithPrecedingOverWindowWithPreceding则包含了partitionBy、orderBy、preceding属性,它提供了as方法创建OverWindow,另外还提供了following方法用于设置following offsetOverWindowedTable

flink-table_2.11-1.7.0-sources.jar!/org/apache/flink/table/api/table.scala

代码语言:javascript代码运行次数:0运行复制

class OverWindowedTable(    private[flink] val table: Table,    private[flink] val overWindows: Array[OverWindow]) {​  def select(fields: Expression*): Table = {    val expandedFields = expandProjectList(      fields,      table.logicalPlan,      table.tableEnv)​    if(fields.exists(_.isInstanceOf[WindowProperty])){      throw new ValidationException(        "Window start and end properties are not available for Over windows.")    }​    val expandedOverFields = resolveOverWindows(expandedFields, overWindows, table.tableEnv)​    new Table(      table.tableEnv,      Project(        expandedOverFields.map(UnresolvedAlias),        table.logicalPlan,        // required for proper projection push down        explicitAlias = true)        .validate(table.tableEnv)    )  }​  def select(fields: String): Table = {    val fieldExprs = ExpressionParser.parseExpressionList(fields)    //get the correct expression for AggFunctionCall    val withResolvedAggFunctionCall = fieldExprs.map(replaceAggFunctionCall(_, table.tableEnv))    select(withResolvedAggFunctionCall: _*)  }}

OverWindowedTable构造器需要overWindows参数;它只提供select操作,其中select可以接收String类型的参数,也可以接收Expression类型的参数;String类型的参数会被转换为Expression类型,最后调用的是Expression类型参数的select方法;select方法创建了新的Table,其Project的projectList为expandedOverFields.map(UnresolvedAlias),而expandedOverFields则通过resolveOverWindows(expandedFields, overWindows, table.tableEnv)得到小结Over Windows类似SQL的over子句,它可以基于event-time、processing-time或者row-count;具体可以通过Over类来构造,其中必须设置orderBy、preceding及as方法;它有Unbounded及Bounded两大类(

对于event-time及processing-time使用unbounded_range来表示Unbounded,对于row-count使用unbounded_row来表示Unbounded;对于event-time及processing-time使用诸如1.minutes来表示Bounded,对于row-count使用诸如10.rows来表示Bounded

)Table提供了OverWindow参数的window方法,用来进行Over Windows操作,它创建的是OverWindowedTable;OverWindow定义了alias、partitionBy、orderBy、preceding、following属性;Over类是创建over window的帮助类,它提供了orderBy及partitionBy两个方法,分别创建的是OverWindowWithOrderBy及PartitionedOver,而PartitionedOver提供了orderBy方法,创建的是OverWindowWithOrderBy;OverWindowWithOrderBy提供了preceding方法,创建的是OverWindowWithPreceding;OverWindowWithPreceding则包含了partitionBy、orderBy、preceding属性,它提供了as方法创建OverWindow,另外还提供了following方法用于设置following offsetOverWindowedTable构造器需要overWindows参数;它只提供select操作,其中select可以接收String类型的参数,也可以接收Expression类型的参数;String类型的参数会被转换为Expression类型,最后调用的是Expression类型参数的select方法;select方法创建了新的Table,其Project的projectList为expandedOverFields.map(UnresolvedAlias),而expandedOverFields则通过resolveOverWindows(expandedFields, overWindows, table.tableEnv)得到docOver Windows

以上就是聊聊flink Table的Over Windows的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/424400.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月7日 10:52:48
下一篇 2025年11月7日 10:57:20

相关推荐

  • Uniapp 中如何不拉伸不裁剪地展示图片?

    灵活展示图片:如何不拉伸不裁剪 在界面设计中,常常需要以原尺寸展示用户上传的图片。本文将介绍一种在 uniapp 框架中实现该功能的简单方法。 对于不同尺寸的图片,可以采用以下处理方式: 极端宽高比:撑满屏幕宽度或高度,再等比缩放居中。非极端宽高比:居中显示,若能撑满则撑满。 然而,如果需要不拉伸不…

    2025年12月24日
    400
  • 如何让小说网站控制台显示乱码,同时网页内容正常显示?

    如何在不影响用户界面的情况下实现控制台乱码? 当在小说网站上下载小说时,大家可能会遇到一个问题:网站上的文本在网页内正常显示,但是在控制台中却是乱码。如何实现此类操作,从而在不影响用户界面(UI)的情况下保持控制台乱码呢? 答案在于使用自定义字体。网站可以通过在服务器端配置自定义字体,并通过在客户端…

    2025年12月24日
    800
  • 如何在地图上轻松创建气泡信息框?

    地图上气泡信息框的巧妙生成 地图上气泡信息框是一种常用的交互功能,它简便易用,能够为用户提供额外信息。本文将探讨如何借助地图库的功能轻松创建这一功能。 利用地图库的原生功能 大多数地图库,如高德地图,都提供了现成的信息窗体和右键菜单功能。这些功能可以通过以下途径实现: 高德地图 JS API 参考文…

    2025年12月24日
    400
  • 如何使用 scroll-behavior 属性实现元素scrollLeft变化时的平滑动画?

    如何实现元素scrollleft变化时的平滑动画效果? 在许多网页应用中,滚动容器的水平滚动条(scrollleft)需要频繁使用。为了让滚动动作更加自然,你希望给scrollleft的变化添加动画效果。 解决方案:scroll-behavior 属性 要实现scrollleft变化时的平滑动画效果…

    2025年12月24日
    000
  • 如何为滚动元素添加平滑过渡,使滚动条滑动时更自然流畅?

    给滚动元素平滑过渡 如何在滚动条属性(scrollleft)发生改变时为元素添加平滑的过渡效果? 解决方案:scroll-behavior 属性 为滚动容器设置 scroll-behavior 属性可以实现平滑滚动。 html 代码: click the button to slide right!…

    2025年12月24日
    500
  • 如何选择元素个数不固定的指定类名子元素?

    灵活选择元素个数不固定的指定类名子元素 在网页布局中,有时需要选择特定类名的子元素,但这些元素的数量并不固定。例如,下面这段 html 代码中,activebar 和 item 元素的数量均不固定: *n *n 如果需要选择第一个 item元素,可以使用 css 选择器 :nth-child()。该…

    2025年12月24日
    200
  • 使用 SVG 如何实现自定义宽度、间距和半径的虚线边框?

    使用 svg 实现自定义虚线边框 如何实现一个具有自定义宽度、间距和半径的虚线边框是一个常见的前端开发问题。传统的解决方案通常涉及使用 border-image 引入切片图片,但是这种方法存在引入外部资源、性能低下的缺点。 为了避免上述问题,可以使用 svg(可缩放矢量图形)来创建纯代码实现。一种方…

    2025年12月24日
    100
  • 如何让“元素跟随文本高度,而不是撑高父容器?

    如何让 元素跟随文本高度,而不是撑高父容器 在页面布局中,经常遇到父容器高度被子元素撑开的问题。在图例所示的案例中,父容器被较高的图片撑开,而文本的高度没有被考虑。本问答将提供纯css解决方案,让图片跟随文本高度,确保父容器的高度不会被图片影响。 解决方法 为了解决这个问题,需要将图片从文档流中脱离…

    2025年12月24日
    000
  • CSS元素设置em和transition后,为何载入页面无放大效果?

    css元素设置em和transition后,为何载入无放大效果 很多开发者在设置了em和transition后,却发现元素载入页面时无放大效果。本文将解答这一问题。 原问题:在视频演示中,将元素设置如下,载入页面会有放大效果。然而,在个人尝试中,并未出现该效果。这是由于macos和windows系统…

    2025年12月24日
    200
  • 为什么 CSS mask 属性未请求指定图片?

    解决 css mask 属性未请求图片的问题 在使用 css mask 属性时,指定了图片地址,但网络面板显示未请求获取该图片,这可能是由于浏览器兼容性问题造成的。 问题 如下代码所示: 立即学习“前端免费学习笔记(深入)”; icon [data-icon=”cloud”] { –icon-cl…

    2025年12月24日
    200
  • 如何利用 CSS 选中激活标签并影响相邻元素的样式?

    如何利用 css 选中激活标签并影响相邻元素? 为了实现激活标签影响相邻元素的样式需求,可以通过 :has 选择器来实现。以下是如何具体操作: 对于激活标签相邻后的元素,可以在 css 中使用以下代码进行设置: li:has(+li.active) { border-radius: 0 0 10px…

    2025年12月24日
    100
  • 如何模拟Windows 10 设置界面中的鼠标悬浮放大效果?

    win10设置界面的鼠标移动显示周边的样式(探照灯效果)的实现方式 在windows设置界面的鼠标悬浮效果中,光标周围会显示一个放大区域。在前端开发中,可以通过多种方式实现类似的效果。 使用css 使用css的transform和box-shadow属性。通过将transform: scale(1.…

    2025年12月24日
    200
  • 如何用HTML/JS实现Windows 10设置界面鼠标移动探照灯效果?

    Win10设置界面中的鼠标移动探照灯效果实现指南 想要在前端开发中实现类似于Windows 10设置界面的鼠标移动探照灯效果,有两种解决方案:CSS 和 HTML/JS 组合。 CSS 实现 不幸的是,仅使用CSS无法完全实现该效果。 立即学习“前端免费学习笔记(深入)”; HTML/JS 实现 要…

    2025年12月24日
    000
  • 为什么我的 Safari 自定义样式表在百度页面上失效了?

    为什么在 Safari 中自定义样式表未能正常工作? 在 Safari 的偏好设置中设置自定义样式表后,您对其进行测试却发现效果不同。在您自己的网页中,样式有效,而在百度页面中却失效。 造成这种情况的原因是,第一个访问的项目使用了文件协议,可以访问本地目录中的图片文件。而第二个访问的百度使用了 ht…

    2025年12月24日
    000
  • 如何用前端实现 Windows 10 设置界面的鼠标移动探照灯效果?

    如何在前端实现 Windows 10 设置界面中的鼠标移动探照灯效果 想要在前端开发中实现 Windows 10 设置界面中类似的鼠标移动探照灯效果,可以通过以下途径: CSS 解决方案 DEMO 1: Windows 10 网格悬停效果:https://codepen.io/tr4553r7/pe…

    2025年12月24日
    000
  • 如何用前端技术实现Windows 10 设置界面鼠标移动时的探照灯效果?

    探索在前端中实现 Windows 10 设置界面鼠标移动时的探照灯效果 在前端开发中,鼠标悬停在元素上时需要呈现类似于 Windows 10 设置界面所展示的探照灯效果,这其中涉及到了元素外围显示光圈效果的技术实现。 CSS 实现 虽然 CSS 无法直接实现探照灯效果,但可以通过以下技巧营造出类似效…

    2025年12月24日
    000
  • 使用CSS mask属性指定图片URL时,为什么浏览器无法加载图片?

    css mask属性未能加载图片的解决方法 使用css mask属性指定图片url时,如示例中所示: mask: url(“https://api.iconify.design/mdi:apple-icloud.svg”) center / contain no-repeat; 但是,在网络面板中却…

    2025年12月24日
    000
  • 如何用CSS Paint API为网页元素添加时尚的斑马线边框?

    为元素添加时尚的斑马线边框 在网页设计中,有时我们需要添加时尚的边框来提升元素的视觉效果。其中,斑马线边框是一种既醒目又别致的设计元素。 实现斜向斑马线边框 要实现斜向斑马线间隔圆环,我们可以使用css paint api。该api提供了强大的功能,可以让我们在元素上绘制复杂的图形。 立即学习“前端…

    2025年12月24日
    000
  • 图片如何不撑高父容器?

    如何让图片不撑高父容器? 当父容器包含不同高度的子元素时,父容器的高度通常会被最高元素撑开。如果你希望父容器的高度由文本内容撑开,避免图片对其产生影响,可以通过以下 css 解决方法: 绝对定位元素: .child-image { position: absolute; top: 0; left: …

    2025年12月24日
    000
  • CSS 帮助

    我正在尝试将文本附加到棕色框的左侧。我不能。我不知道代码有什么问题。请帮助我。 css .hero { position: relative; bottom: 80px; display: flex; justify-content: left; align-items: start; color:…

    2025年12月24日 好文分享
    200

发表回复

登录后才能评论
关注微信