Ale*_*all 3 python join apache-flink flink-streaming flink-sql
示例代码:
from pyflink.table import EnvironmentSettings, StreamTableEnvironment
env_settings = (
EnvironmentSettings.new_instance().in_streaming_mode().use_blink_planner().build()
)
table_env = StreamTableEnvironment.create(environment_settings=env_settings)
table_env.execute_sql(
"""
CREATE TABLE table1 (
id INT,
ts TIMESTAMP(3),
WATERMARK FOR ts AS ts - INTERVAL '5' SECOND
) WITH (
'connector.type' = 'filesystem',
'format.type' = 'csv',
'connector.path' = '/home/alex/work/test-flink/data1.csv'
)
"""
)
table_env.execute_sql(
"""
CREATE TABLE table2 (
id2 INT,
ts2 TIMESTAMP(3),
WATERMARK FOR ts2 AS ts2 - INTERVAL '5' SECOND
) WITH (
'connector.type' = 'filesystem',
'format.type' = 'csv',
'connector.path' = '/home/alex/work/test-flink/data2.csv'
)
"""
)
table1 = table_env.from_path("table1")
table2 = table_env.from_path("table2")
print(table1.join(table2).where("ts = ts2 && id = id2").select("id, ts").to_pandas())
Run Code Online (Sandbox Code Playgroud)
给出错误:
py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.flink.table.runtime.arrow.ArrowUtils.collectAsPandasDataFrame.
: org.apache.flink.table.api.TableException: Cannot generate a valid execution plan for the given query:
FlinkLogicalLegacySink(name=[collect], fields=[id, ts])
+- FlinkLogicalCalc(select=[id, ts])
+- FlinkLogicalJoin(condition=[AND(=($2, $5), =($0, $3))], joinType=[inner])
:- FlinkLogicalCalc(select=[id, ts, CAST(ts) AS ts0])
: +- FlinkLogicalWatermarkAssigner(rowtime=[ts], watermark=[-($1, 5000:INTERVAL SECOND)])
: +- FlinkLogicalLegacyTableSourceScan(table=[[default_catalog, default_database, table1, source: [CsvTableSource(read fields: id, ts)]]], fields=[id, ts])
+- FlinkLogicalCalc(select=[id2, ts2, CAST(ts2) AS ts20])
+- FlinkLogicalWatermarkAssigner(rowtime=[ts2], watermark=[-($1, 5000:INTERVAL SECOND)])
+- FlinkLogicalLegacyTableSourceScan(table=[[default_catalog, default_database, table2, source: [CsvTableSource(read fields: id2, ts2)]]], fields=[id2, ts2])
Rowtime attributes must not be in the input rows of a regular join. As a workaround you can cast the time attributes of input tables to TIMESTAMP before.
Run Code Online (Sandbox Code Playgroud)
这似乎与其他类似的问题(例如这个问题)不同,因为我已遵循文档中的说明并指定了等连接和时间间隔连接(ts = ts2 && id = id2):
间隔连接至少需要一个等连接谓词和一个限制两侧时间的连接条件。这样的条件可以通过两个适当的范围谓词(<、<=、>=、>)或比较两个输入表的相同类型的时间属性(即,处理时间或事件时间)的单个相等谓词来定义。
例如,以下谓词是有效的区间连接条件:
ltime = rtime
如果问题是这些不是仅附加表,我不知道如何使它们如此。
设置时间特征没有帮助:
py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.flink.table.runtime.arrow.ArrowUtils.collectAsPandasDataFrame.
: org.apache.flink.table.api.TableException: Cannot generate a valid execution plan for the given query:
FlinkLogicalLegacySink(name=[collect], fields=[id, ts])
+- FlinkLogicalCalc(select=[id, ts])
+- FlinkLogicalJoin(condition=[AND(=($2, $5), =($0, $3))], joinType=[inner])
:- FlinkLogicalCalc(select=[id, ts, CAST(ts) AS ts0])
: +- FlinkLogicalWatermarkAssigner(rowtime=[ts], watermark=[-($1, 5000:INTERVAL SECOND)])
: +- FlinkLogicalLegacyTableSourceScan(table=[[default_catalog, default_database, table1, source: [CsvTableSource(read fields: id, ts)]]], fields=[id, ts])
+- FlinkLogicalCalc(select=[id2, ts2, CAST(ts2) AS ts20])
+- FlinkLogicalWatermarkAssigner(rowtime=[ts2], watermark=[-($1, 5000:INTERVAL SECOND)])
+- FlinkLogicalLegacyTableSourceScan(table=[[default_catalog, default_database, table2, source: [CsvTableSource(read fields: id2, ts2)]]], fields=[id2, ts2])
Rowtime attributes must not be in the input rows of a regular join. As a workaround you can cast the time attributes of input tables to TIMESTAMP before.
Run Code Online (Sandbox Code Playgroud)
如果我使用处理时间来代替,ts AS PROCTIME()那么查询就会成功。但我认为我需要使用事件时间,但我不明白为什么会出现这种差异。
FROM a, bSQL 中两个常规表之间的联接始终使用或以相同的方式表示a JOIN b。
然而,Flink 在底层为相同的语法提供了两种类型的连接运算符。一种是间隔连接,它需要时间属性来根据时间将两个表相互关联。其中之一是常规 SQL 连接,它以数据库中的通用方式实现。
间隔连接只是一种流优化,以在运行时保持较低的状态大小,并且不会在结果中产生更新。常规的 SQL 连接运算符最终可以产生与间隔相同的结果,但维护成本较高。
为了区分间隔连接和常规连接,优化器在子句中搜索作用WHERE于时间属性的谓词。对于间隔连接,输出始终可以包含两个用于外部时间操作(下游时间运算符)的行时间属性。因为两个行时间属性仍然与底层水印系统保持一致。这意味着例如外部窗口或其他间隔连接可以再次与时间属性一起使用。
然而,间隔连接的实现存在一些已知的缺点,并在FLINK-10211中进行了介绍。由于糟糕的设计,我们无法在某些位置区分间隔连接和常规连接。因此,我们需要假设常规连接可能是间隔连接,并且不能TIMESTAMP自动将时间属性转换为用户。相反,我们目前禁止常规连接的输出中包含时间属性。
在某些时候,这种限制有望消失,直到那时用户有两种可能性:
不要对包含时间属性的表使用常规联接。您也可以使用嵌套子句将其投影出来SELECT或在连接之前执行 a 操作CAST。
CAST(col AS TIMESTAMP)将时间属性转换为子句中使用的常规时间戳SELECT。它将被下推到连接操作中。
您的异常表明您正在使用常规联接。间隔连接需要一个范围来操作(即使只有 1 毫秒)。他们不支持平等。