本文翻译自官网: Time Attributes   https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/streaming/time_attributes.html

Flink Table Api & SQL 翻译目录

Flink能够根据不同的时间概念处理流数据。

  • Process time 是指正在执行相应操作的机器的系统时间(也称为“挂钟时间”)。
  • Event time 是指基于附在每行上的时间戳对流数据进行处理。时间戳可以在事件发生时进行编码。
  • Ingestion time 是事件进入Flink的时间;在内部,它的处理类似于事件时间。

有关Flink中时间处理的更多信息,请参见有关事件时间和水印的介绍。

本页说明如何在Flink的Table API和SQL中为基于时间的操作定义时间属性。

时间属性简介

Table APISQL中的基于时间的操作(例如窗口)都需要有关时间概念及其起源的信息。因此,表可以提供逻辑时间属性,以指示时间并访问表程序中的相应时间戳。

时间属性可以是每个表结构的一部分。它们是从DataStream创建表时定义的,或者是在使用TableSource时预定义的。一旦在开始定义了时间属性,就可以将其作为字段引用,并可以在基于时间的操作中使用。

只要时间属性没有被修改并且只是从查询的一部分转发到另一部分,它仍然是有效的时间属性。时间属性的行为类似于常规时间戳,可以进行访问以进行计算。常规时间戳记不能与Flink的时间和水印系统配合使用,因此不能再用于基于时间的操作。

表程序要求已为流环境指定了相应的时间特征:

  1. val env = StreamExecutionEnvironment.getExecutionEnvironment
  2.  
  3. env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime) // default
  4.  
  5. // alternatively:
  6. // env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime)
  7. // env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)

处理时间

处理时间允许表程序根据本地计算机的时间产生结果。这是最简单的时间概念,但不提供确定性。它既不需要时间戳提取也不需要水印生成。

有两种定义处理时间属性的方法。

在数据流到表的转换期间

在结构定义期间,使用.proctime属性定义了处理时间属性。时间属性只能通过其他逻辑字段扩展物理结构。因此,只能在结构定义的末尾定义它。

  1. val stream: DataStream[(String, String)] = ...
  2.  
  3. // declare an additional logical field as a processing time attribute
  4. val table = tEnv.fromDataStream(stream, 'UserActionTimestamp, 'Username, 'Data, 'UserActionTime.proctime)
  5.  
  6. val windowedTable = table.window(Tumble over 10.minutes on 'UserActionTime as 'userActionWindow)

使用TableSource

处理时间属性由实现DefinedProctimeAttribute接口的TableSource定义。逻辑时间属性附加到由TableSource的返回类型定义的物理结构。

  1. class UserActionSource extends StreamTableSource[Row] with DefinedProctimeAttribute {
  2.  
  3. override def getReturnType = {
  4. val names = Array[String]("Username" , "Data")
  5. val types = Array[TypeInformation[_]](Types.STRING, Types.STRING)
  6. Types.ROW(names, types)
  7. }
  8.  
  9. override def getDataStream(execEnv: StreamExecutionEnvironment): DataStream[Row] = {
  10. // create stream
  11. val stream = ...
  12. stream
  13. }
  14.  
  15. override def getProctimeAttribute = {
  16. // field with this name will be appended as a third field
  17. "UserActionTime"
  18. }
  19. }
  20.  
  21. // register table source
  22. tEnv.registerTableSource("UserActions", new UserActionSource)
  23.  
  24. val windowedTable = tEnv
  25. .scan("UserActions")
  26. .window(Tumble over 10.minutes on 'UserActionTime as 'userActionWindow)

事件时间

事件时间允许表程序根据每个记录中包含的时间来产生结果。即使在无序事件或迟发事件的情况下,这也可以提供一致的结果。从持久性存储中读取记录时,还可以确保表程序的可重播结果。

此外,事件时间允许批处理和流环境中的表程序使用统一语法。流环境中的时间属性可以是批处理环境中记录的常规字段。

为了处理乱序事件并区分流中的按时事件和延迟事件,Flink需要从事件中提取时间戳并及时进行某种处理(就是水印)。

可以在DataStream到Table的转换期间或使用TableSource 定义事件时间属性。

在DataStream 到 Table 的转换期间

在结构定义期间,事件时间属性是使用.rowtime属性定义的。必须在转换的DataStream中分配时间戳和水印

将 DataStream 转换为 Table 时,有两种定义时间属性的方法。根据指定的.rowtime字段名称是否存在于DataStream的结构中,timestamp字段为

  • 作为新字段附加到结构
  • 替换现有字段。

无论哪种情况,事件时间时间戳字段都将保留DataStream事件时间 时间戳的值。

  1. // Option 1:
  2.  
  3. // extract timestamp and assign watermarks based on knowledge of the stream
  4. val stream: DataStream[(String, String)] = inputStream.assignTimestampsAndWatermarks(...)
  5.  
  6. // declare an additional logical field as an event time attribute
  7. val table = tEnv.fromDataStream(stream, 'Username, 'Data, 'UserActionTime.rowtime)
  8.  
  9. // Option 2:
  10.  
  11. // extract timestamp from first field, and assign watermarks based on knowledge of the stream
  12. val stream: DataStream[(Long, String, String)] = inputStream.assignTimestampsAndWatermarks(...)
  13.  
  14. // the first field has been used for timestamp extraction, and is no longer necessary
  15. // replace first field with a logical event time attribute
  16. val table = tEnv.fromDataStream(stream, 'UserActionTime.rowtime, 'Username, 'Data)
  17.  
  18. // Usage:
  19.  
  20. val windowedTable = table.window(Tumble over 10.minutes on 'UserActionTime as 'userActionWindow)

使用TableSource

事件时间属性由实现了DefinedRowtimeAttributes接口的TableSource定义。getRowtimeAttributeDescriptors()方法返回用于描述时间属性最终名称的RowtimeAttributeDescriptor列表,用于导出属性值的时间戳提取器以及与该属性关联的水印策略。

请确保由getDataStream()方法返回的DataStream与定义的时间属性对齐。仅当定义了StreamRecordTimestamp时间戳提取器时,才考虑DataStream的时间戳(由TimestampAssigner分配的时间戳)。仅当定义了PreserveWatermarks水印策略时,才会保留DataStream的水印。 否则,仅TableSource的rowtime属性的值相关。

  1. // define a table source with a rowtime attribute
  2. class UserActionSource extends StreamTableSource[Row] with DefinedRowtimeAttributes {
  3.  
  4. override def getReturnType = {
  5. val names = Array[String]("Username" , "Data", "UserActionTime")
  6. val types = Array[TypeInformation[_]](Types.STRING, Types.STRING, Types.LONG)
  7. Types.ROW(names, types)
  8. }
  9.  
  10. override def getDataStream(execEnv: StreamExecutionEnvironment): DataStream[Row] = {
  11. // create stream
  12. // ...
  13. // assign watermarks based on the "UserActionTime" attribute
  14. val stream = inputStream.assignTimestampsAndWatermarks(...)
  15. stream
  16. }
  17.  
  18. override def getRowtimeAttributeDescriptors: util.List[RowtimeAttributeDescriptor] = {
  19. // Mark the "UserActionTime" attribute as event-time attribute.
  20. // We create one attribute descriptor of "UserActionTime".
  21. val rowtimeAttrDescr = new RowtimeAttributeDescriptor(
  22. "UserActionTime",
  23. new ExistingField("UserActionTime"),
  24. new AscendingTimestamps)
  25. val listRowtimeAttrDescr = Collections.singletonList(rowtimeAttrDescr)
  26. listRowtimeAttrDescr
  27. }
  28. }
  29.  
  30. // register the table source
  31. tEnv.registerTableSource("UserActions", new UserActionSource)
  32.  
  33. val windowedTable = tEnv
  34. .scan("UserActions")
  35. .window(Tumble over 10.minutes on 'UserActionTime as 'userActionWindow)

欢迎关注Flink菜鸟公众号,会不定期更新Flink(开发技术)相关的推文

【翻译】Flink Table Api & SQL —Streaming 概念 ——时间属性的更多相关文章

  1. 【翻译】Flink Table Api & SQL —Streaming 概念 ——动态表

    本文翻译自官网:Flink Table Api & SQL 动态表 https://ci.apache.org/projects/flink/flink-docs-release-1.9/de ...

  2. 【翻译】Flink Table Api & SQL —Streaming 概念 ——在持续查询中 Join

    本文翻译自官网 :  Joins in Continuous Queries   https://ci.apache.org/projects/flink/flink-docs-release-1.9 ...

  3. 【翻译】Flink Table Api & SQL —Streaming 概念 —— 时态表

    本文翻译自官网: Temporal Tables https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/strea ...

  4. 【翻译】Flink Table Api & SQL ——Streaming 概念

    本文翻译自官网:Streaming 概念  https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/streamin ...

  5. 【翻译】Flink Table Api & SQL —Streaming 概念 —— 表中的模式匹配 Beta版

    本文翻译自官网:Detecting Patterns in Tables Beta  https://ci.apache.org/projects/flink/flink-docs-release-1 ...

  6. 【翻译】Flink Table Api & SQL —Streaming 概念 —— 查询配置

    本文翻译自官网:Query Configuration  https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/s ...

  7. 【翻译】Flink Table Api & SQL — 流概念

    本文翻译自官网:Streaming Concepts  https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/st ...

  8. Flink Table Api & SQL 翻译目录

    Flink 官网 Table Api & SQL  相关文档的翻译终于完成,这里整理一个安装官网目录顺序一样的目录 [翻译]Flink Table Api & SQL —— Overv ...

  9. 【翻译】Flink Table Api & SQL —— 概念与通用API

    本文翻译自官网:https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/common.html Flink Tabl ...

随机推荐

  1. socket mac终端调试工具 nc netcat

    今天想学点socket ,因此搜索socket 工具,找到了netCat工具.可以打开两个终端window ,实现终端之间的socket的收发信息,为以后学习socket调试做准备用吧.两个终端分别打 ...

  2. Centos7 yum安装postgresql 9.5

    添加RPM yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos ...

  3. Appache Flume 中文介绍(转)

    Flume 是什么        Apache Flume是一个高可靠.高可用的分布式的海量日志收集.聚合.传输系统.它可以从不同的日志源采集数据并集中存储. Flume也算是Hadoop生态系统的一 ...

  4. 如何使用powerdesigner导出sql脚本

    使用power designer可以很方便的对数据库设计进行管理,并且能够更方便的查看表与表之间的关系.同时,还可以对设计好的数据库直接导出创建脚本,根据不同的数据库实例导出对应的创建脚本,然后根据脚 ...

  5. 2019牛客多校第二场BEddy Walker 2——BM递推

    题意 从数字 $0$ 除法,每次向前走 $i$ 步,$i$ 是 $1 \sim K$ 中等概率随机的一个数,也就是说概率都是 $\frac{1}{K}$.求落在过数字 $N$ 额概率,$N=-1$ 表 ...

  6. c++ socket发送数据时,sendData = char * string 导致的乱码问题

    解决方法:将string 通过copy函数复制到某个char[] 1. string res =“xxx”; char arr[100]; int len = res.copy(arr, 100); ...

  7. 《OKR工作法》–让所有人承担自己的职责

    <OKR工作法>中提到了一个创业故事,TeaBee,创业的目标是让喜欢喝茶的人喝到好茶. 创业初期作为首席执行官的汉娜和作为总裁的杰克就在将茶叶提供给餐厅还是餐厅供应商上产生了分歧,随后他 ...

  8. 15-ESP8266 SDK开发基础入门篇--上位机串口控制 Wi-Fi输出PWM的占空比,调节LED亮度,上位机程序编写

    https://www.cnblogs.com/yangfengwu/p/11104167.html 先说一下整体思路哈.. 咱滑动的时候 会进入这个,然后咱呢不直接从这个里面写发送 因为这样的话太快 ...

  9. GoCN每日新闻(2019-10-25)

    GoCN每日新闻(2019-10-25) GoCN每日新闻(2019-10-25) 1. [译]Golang应付百万级请求/分钟 https://juejin.im/post/5db1464b6fb9 ...

  10. static final与final修饰的常量有什么不同

    最近重头开始看基础的书,对一些基础的概念又有了一些新的理解,特此记录一下 static final修饰的常量: 静态常量(static修饰的全部为静态的),编译器常量,编译时就确定其值(java代码经 ...