Flink事件时间间隔连接转DataStream报多rowtime字段异常如何解决
Flink 事件时间间隔连接异常排查
场景说明
需对两个数据流做事件时间维度的间隔连接操作,事件定义为如下Scala样例类,tradeDate字段类型为java.sql.Timestamp:
case class Stock(id: String, tradeDate: Timestamp, price: Double) case class StockNameChanging(id: String, name: String, tradeDate: Timestamp)
抛出的异常
运行程序时触发org.apache.flink.table.api.TableException,核心报错信息:
Found more than one rowtime field: [rt1, rt2] in the table that should be converted to a DataStream.
Please select the rowtime field that should be used as event-time timestamp for the DataStream by casting all other fields to TIMESTAMP.
业务实现代码
val env = StreamExecutionEnvironment.getExecutionEnvironment env.setParallelism(1) // 股票价格数据流 val ds1 = env.addSource(new IntervalJoinStockSource(emitInterval = 0)) .assignTimestampsAndWatermarks(new StockWatermarkGenerator(4000)) // 股票名称变更数据流 val ds2 = env.addSource(new IntervalJoinStockNameChangingSource(emitInterval = 0)) .assignTimestampsAndWatermarks(new StockNameChangingWatermarkGenerator(4000)) val tenv = StreamTableEnvironment.create(env) tenv.createTemporaryView("s1", ds1, $"id", $"price", $"tradeDate".rowtime() as "rt1") tenv.createTemporaryView("s2", ds2, $"id", $"name", $"tradeDate".rowtime() as "rt2") tenv.from("s1").printSchema() tenv.from("s2").printSchema() val sql = """ select s1.id, s2.name, s1.price, s1.rt1, s2.rt2 from s1 join s2 on s1.id = s2.id where s1.rt1 between s2.rt2 - interval '2' second and s2.rt2 + interval '2' second """.stripMargin(' ') tenv.sqlQuery(sql).toAppendStream[Row].print() env.execute()
异常根因
Flink Table API 转DataStream存在固定规则限制:转换输出的Table仅允许存在一个被标记为rowtime的事件时间字段。
当前查询结果同时保留了来自两个源表的rt1、rt2两个rowtime字段,触发规则校验失败。
注:两个源表各自单独标记rowtime字段是合法配置,不违反规则,问题出在join后的输出结果集直接携带两个rowtime字段、且被用于转DataStream的场景。
修复方式
在SQL查询阶段,将不需要作为结果流事件时间的rowtime字段通过cast转为普通TIMESTAMP类型,解除其rowtime标记。
- 若选择保留
rt1作为结果流的事件时间,修改后的SQL为:
select s1.id, s2.name, s1.price, s1.rt1, cast(s2.rt2 as timestamp) as rt2 from s1 join s2 on s1.id = s2.id where s1.rt1 between s2.rt2 - interval '2' second and s2.rt2 + interval '2' second
- 若结果流不需要绑定事件时间,可将两个字段都转为普通TIMESTAMP类型。
内容的提问来源于stack exchange,提问作者Tom
相关产品推荐
相关产品推荐

