Flinktableapi时间特性

处理时间指定
①DataStream 转化成 Table 时指定

val dataStream = env.readTextFile("src/main/resources/sensor.txt")
      .map(data => {
        val arr = data.split(",")
        SensorReading(arr(0),arr(1).toLong,arr(2).toDouble)
      })
val sensorTableFromStream = 
oldTabEnv.fromDataStream(dataStream,'id,'timestamp,'temperature,'pt.proctime)

oldTabEnv.fromDataStream(dataStream,'id,'timestamp,'temperature,'pt.proctime)
这个 proctime 属性只能通过附加逻辑字段,来扩展物理 schema。因此,只能在
schema 定义的末尾。
②定义 Table Schema 时指定

val sensorTableFromConnect = oldTabEnv
      .connect(new FileSystem().path("src/main/resources/sensor.txt"))
      .withFormat(new Csv())
      .withSchema(new Schema()
        .field("id",DataTypes.STRING())
        .field("ts",DataTypes.DOUBLE())
        .field("temp",DataTypes.BIGINT())
        .field("pt",DataTypes.TIMESTAMP(3))
        .proctime()
      ).createTemporaryTable("input_table")

由于CsvTableSource没有实现DefinedProctimeAttribute,因此descriptor为Csv()时无法指定时间属性,descriptor为Kafka()时实现了DefinedProctimeAttribute可以定义时间属性
在这里插入图片描述
在这里插入图片描述
③创建表的 DDL 中指定

val sourceDDL =
      """
        |create table input_ddl_table(
        |id string,
        |ts bigint,
        |temp double,
        |pt as proctime()
        |)with(
        |'connector.type' = 'filesystem',
        |'connector.path' = 'file://src//main//resources//sensor.txt',
        |'format.type' = 'csv
        |)
        |""".stripMargin
blinkTabEnv.sqlUpdate(sourceDDL)

运行这段 DDL,必须使用 Blink Planner
事件时间指定
① DataStream 转化成 Table 时指定

val dataStream = env.readTextFile("src/main/resources/sensor.txt")
      .map(data => {val arr = data.split(",")
        SensorReading(arr(0),arr(1).toLong,arr(2).toDouble)
      }).assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor[SensorReading](Time.minutes(1)) {
      override def extractTimestamp(element: SensorReading): Long = element.timestamp*1000L
    })
    val tabEnv = StreamTableEnvironment.create(env)
val tableFromStream = tabEnv.fromDataStream(dataStream,'id,'timestamp as 'ts,'temterature as 'temp,'rt.rowtime) 
val tableFromStream = 
tabEnv.fromDataStream(dataStream,'id,'timestam.rowtime as 'ts,'temperature as 'temp)

事件时间与watermark在生成datastream时已经指定,创建table时指定的事件时间只是起标识作用。
② 定义 Table Schema 时指定

tabEnv.connect(new FileSystem().path("src/main/resources/sensor.txt"))
      .withFormat(new Csv())
      .withSchema(new Schema()
        .field("id",DataTypes.STRING())
        .field("ts",DataTypes.BIGINT())
        .field("temp",DataTypes.DOUBLE())
        .rowtime(
          new Rowtime().watermarksPeriodicBounded(1000L)
        )
      ).createTemporaryTable("input_table")

③ 创建表的 DDL 中指定

   val tableDDL =
      """
        |create table dataTable (
        | id varchar(20) not null,
        | ts bigint,
        | temperature double,
        | rt AS TO_TIMESTAMP( FROM_UNIXTIME(ts) ),
        | watermark for rt as rt - interval '1' second
        |) with (
        | 'connector.type' = 'filesystem',
        | 'connector.path' = '\sensor.txt',
        | 'format.type' = 'csv'
        |)
        |""".stripMargin

tabEnv.sqlUpdate(tableDDL)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值