Flink SQL - OVER windows‘ ordering in stream mode must be defined on a time attribute.

OVER windows' ordering in stream mode must be defined on a time attribute.

看着网上flink sql的帖子较少,刚才这个问题查了好久,现在贴下完整源码。

这个报错,是建表的时候需要指定时间语义的字段,WATERMARK 是必须的

package com.z.demo

import org.apache.flink.table.api._

/**
 * @Author wenz.ma
 * @Date 2021/10/27 15:17
 * @Desc
 */
object SqlOverRows02 {
  def main(args: Array[String]): Unit = {
    val settings = EnvironmentSettings.newInstance().inStreamingMode().build()
    val tEnv = TableEnvironment.create(settings)

    tEnv.executeSql(
      """
        |create table projects(
        |id int,
        |name string,
        |score double,
        |load_date timestamp(3),
        |WATERMARK FOR load_date AS load_date - INTERVAL '1' MINUTE
        |)with(
        |'connector' = 'kafka',
        |'topic' = 'test-topic',
        |'properties.bootstrap.servers' = 'server120:9092',
        |'properties.group.id' = 'testGroup',
        |'scan.startup.mode' = 'latest-offset',
        |'format' = 'csv'
        |)
        |""".stripMargin)
    tEnv.executeSql(
      """
        |select
        | name,
        | max(score)
        |   over(partition by name
        |     order by load_date
        |     RANGE BETWEEN INTERVAL '10' SECOND PRECEDING AND CURRENT ROW )max_score,
        | min(score)
        |   over(partition by name
        |     order by load_date
        |     RANGE BETWEEN INTERVAL '10' SECOND PRECEDING AND CURRENT ROW )min_score,
        | current_time
        | from
        | projects
        |""".stripMargin).print()
  }
}


官网部分建表语句(在最下边):https://nightlies.apache.org/flink/flink-docs-release-1.13/docs/dev/table/sql/queries/window-agg/
官网over语法:https://nightlies.apache.org/flink/flink-docs-release-1.13/docs/dev/table/sql/queries/over-agg/

你可能感兴趣的:(Flink,flink,sql,windows)