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

Flink Table Api & SQL 翻译目录

时态表(注:Temporal Table , 我翻译为时态表,可以访问表在不同时间的内容)表示一直在修改的表上的(参数化)视图的概念,该视图返回表在特定时间点的内容。

更改表可以是跟踪表的修改历史(例如,数据库更改日志),也可以是维表的具体修改(例如,数据库表)。

对于表的历史修改,Flink可以跟踪修改,并允许在查询中访问表的特定时间点的内容。 在Flink中,这种表由Temporal Table Function表示

对于变化的维表,Flink允许在查询中的处理时访问表的内容。在Flink中,这种表由Temporal Table 表示。

设计初衷

与表的修改历史相关

假设我们有下表 RatesHistory

SELECT * FROM RatesHistory;

rowtime currency   rate
======= ======== ======
09:00 US Dollar 102
09:00 Euro 114
09:00 Yen 1
10:45 Euro 116
11:15 Euro 119
11:49 Pounds 108

RatesHistory表示一个不断增长的关于日元的货币汇率的附加表(汇率为1)。例如,汇率期间从 09:0010:45的欧元日元的汇率为 114。从 10:45 到 11:15 是 116

假设我们要在10:58的时间输出所有当前汇率,则需要以下SQL查询来计算结果表:

SELECT *
FROM RatesHistory AS r
WHERE r.rowtime = (
SELECT MAX(rowtime)
FROM RatesHistory AS r2
WHERE r2.currency = r.currency
AND r2.rowtime <= TIME '10:58');

子查询确定对应货币的最大时间小于或等于所需时间。外部查询列出具有最大时间戳的汇率。

下表显示了这种计算的结果。 在我们的示例中,考虑了10:45 时欧元的更新,但是 10:58 时表的版本中未考虑 11:15 时欧元的更新和新的英镑输入。

rowtime currency   rate
======= ======== ======
09:00 US Dollar 102
09:00 Yen 1
10:45 Euro 116

时态表的概念旨在简化此类查询,加快其执行速度,并减少Flink的状态使用率。时态表是 append-only 表上的参数化视图,该视图将 append-only 表的行解释为表的变更日志,并在特定时间点提供该表的版本。将 append-only 表解释为变更日志需要指定主键属性和时间戳属性。主键确定覆盖哪些行,时间戳确定行有效的时间。

在上面的示例中,currency RatesHistory表的主键,并且rowtime是timestamp属性。

在Flink中,这由时态表函数表示

与维表变化相关

另一方面,某些用例需要连接变化的维表,该表是外部数据库表。

假设 LatestRates 是一个以最新汇率实现的表(例如,存储在其中)。LatestRates 是物化的 RatesHistory 历史。那么时间 10:58 的 LatestRates 表的内容将是:

10:58> SELECT * FROM LatestRates;
currency rate
======== ======
US Dollar 102
Yen 1
Euro 116

12:00 时 LatestRates表的内容将是:

12:00> SELECT * FROM LatestRates;
currency rate
======== ======
US Dollar 102
Yen 1
Euro 119
Pounds 108

在Flink中,这由Temporal Table表示。

时态表函数

为了访问时态表中的数据,必须传递一个时间属性,该属性确定将要返回的表的版本。Flink使用表函数的SQL语法提供一种表达它的方法。

定义后,时态表函数将使用单个时间参数timeAttribute并返回一组行。该集合包含相对于给定时间属性的所有现有主键的行的最新版本。

假设我们Rates(timeAttribute)基于RatesHistory表定义了一个时态表函数,我们可以通过以下方式查询该函数:

SELECT * FROM Rates('10:15');

rowtime currency   rate
======= ======== ======
09:00 US Dollar 102
09:00 Euro 114
09:00 Yen 1 SELECT * FROM Rates('11:00'); rowtime currency rate
======= ======== ======
09:00 US Dollar 102
10:45 Euro 116
09:00 Yen 1

对Rates(timeAttribute)的每个查询都将返回给定timeAttribute的Rates状态。

注意:当前 Flink 不支持使用常量时间属性参数直接查询时态表函数。目前,时态表函数只能在 join 中使用。上面的示例用于提供有关函数 Rates(timeAttribute)返回内容的直观信息。

另请参阅有关用于持续查询的 join的页面,以获取有关如何与时态表 join 的更多信息。

定义时态表函数

以下代码段说明了如何从 append-only 表中创建时态表函数。

// Get the stream and table environments.
val env = StreamExecutionEnvironment.getExecutionEnvironment
val tEnv = StreamTableEnvironment.create(env) // Provide a static data set of the rates history table.
val ratesHistoryData = new mutable.MutableList[(String, Long)]
ratesHistoryData.+=(("US Dollar", 102L))
ratesHistoryData.+=(("Euro", 114L))
ratesHistoryData.+=(("Yen", 1L))
ratesHistoryData.+=(("Euro", 116L))
ratesHistoryData.+=(("Euro", 119L)) // Create and register an example table using above data set.
// In the real setup, you should replace this with your own table.
val ratesHistory = env
.fromCollection(ratesHistoryData)
.toTable(tEnv, 'r_currency, 'r_rate, 'r_proctime.proctime) tEnv.registerTable("RatesHistory", ratesHistory) // Create and register TemporalTableFunction.
// Define "r_proctime" as the time attribute and "r_currency" as the primary key.
val rates = ratesHistory.createTemporalTableFunction('r_proctime, 'r_currency) // <==== (1)
tEnv.registerFunction("Rates", rates) // <==== (2)

Line (1)创建了一个 时态表函数 rates ,使我们可以在 Table API 中使用 rates 函数 。

Line (2) 在表环境中以Rates名称注册此函数,这使我们可以在SQL中使用Rates函数。

时态表

注意:仅 Blink planner 支持此功能。

为了访问时态表中的数据,当前必须使用LookupableTableSource定义一个TableSource。 Flink 使用FOR SYSTEM_TIME AS OF 的SQL语法查询时态表,这在SQL:2011中提出。

假设我们定义了一个时态表 LatestRates,我们可以通过以下方式查询此类表:

SELECT * FROM LatestRates FOR SYSTEM_TIME AS OF TIME '10:15';

currency   rate
======== ======
US Dollar 102
Euro 114
Yen 1 SELECT * FROM LatestRates FOR SYSTEM_TIME AS OF TIME '11:00'; currency rate
======== ======
US Dollar 102
Euro 116
Yen 1

注意:当前,Flink不支持以固定时间直接查询时态表。目前,时态表只能在 join 中使用。上面的示例用于提供有关时态表LatestRates返回内容的直觉。

另请参阅有关用于持续查询的 join的页面,以获取有关如何与时态表 join 的更多信息。

定义时态表

// Get the stream and table environments.
val env = StreamExecutionEnvironment.getExecutionEnvironment
val tEnv = TableEnvironment.getTableEnvironment(env) // Create an HBaseTableSource as a temporal table which implements LookableTableSource
// In the real setup, you should replace this with your own table.
val rates = new HBaseTableSource(conf, "Rates")
rates.setRowKey("currency", String.class) // currency as the primary key
rates.addColumn("fam1", "rate", Double.class) // register the temporal table into environment, then we can query it in sql
tEnv.registerTableSource("Rates", rates)

另请参阅有关如何定义LookupableTableSource的页面。

欢迎关注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 概念

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

  4. 【翻译】Flink Table Api & SQL —Streaming 概念 ——时间属性

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

  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 — Hive —— 读写 Hive 表

    本文翻译自官网:Reading & Writing Hive Tables  https://ci.apache.org/projects/flink/flink-docs-release-1 ...

  9. Flink Table Api & SQL 翻译目录

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

随机推荐

  1. MSSQL-反弹注入

    工具:香港云免费云服务器:http://www.webweb.com 注册使用匿名邮箱:https://bccto.me/ 香港云服务器搭建MSSQL数据库,并建表admin,字段数要大于等于我们想要 ...

  2. NGINX并发量优化

    NGINX并发量优化 一.压力测试 命令:ab -c 2000 -n 2000 web服务器的地址 ab:压力测试工具 -c:client缩写,客户端的数量 -n:总的访问量,所有客户端总共的访问量. ...

  3. Kotlin注解深入解析与实例剖析

    在上一次https://www.cnblogs.com/webor2006/p/11522798.html中学习了Kotlin注解相关的东东,这次继续对Kotlin的注解继续学习: 注解也可以拥有自己 ...

  4. SQLAlchemy的应用创建

    1.首先创建app文件夹 同django 创建app 一样 创建文件 在创建的views中写入两个蓝图函数为了操作数据库的增删改查 acc.py from flask import Blueprint ...

  5. Android init介绍(下)

    上一篇请参考<Android init介绍(上)> 5. AIL 在init启动过程中,系统服务等均是通过解析rc文件来启动,而rc文件则是由Android初始化语言(Android In ...

  6. 记一次用pip安装docker-compose报错及解决方法

    Docker-Compose 的安装 方法一 # 下载1.25.0 docker compose sudo curl -L "https://github.com/docker/compos ...

  7. webpack-dev-server 本地代理proxy

    proxy: [ { context: ['/user', '/rights', '/resource/getAdNotice'], target: 'https://plus.m.jd.com', ...

  8. Unity点击播放卡死问题的解决

    Unity项目使用了github for unity 插件,用来管理项目,结果安装完后Unity卡死在播放的界面,任务管理器中显示无响应. 经过排查后发现是 git for windows这个程序引起 ...

  9. 编程小白入门分享二:IntelliJ IDEA的入门操作小知识

    idea简介 IDEA 全称 IntelliJ IDEA,是java编程语言开发的集成环境.IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支 ...

  10. Go语言 - 指针 | new | make

    区别于C/C++中的指针,Go语言中的指针不能进行偏移和运算,是安全指针. 要搞明白Go语言中的指针需要先知道3个概念:指针地址.指针类型和指针取值. 概念 任何程序数据载入内存后,在内存都有他们的地 ...