本文为您介绍如何使用IntervalJoin语句。
背景信息
IntervalJoin语句可以让两个流进行JOIN时,左流和右流中每条记录只关联另外一条流上满足定义的时间范围内的数据,且进行完JOIN后,仍然保留输入流上的时间列,让您继续进行基于Event Time的操作。
语法格式
SELECT column-names
FROM table1 [AS <alias1>]
[INNER | LEFT | RIGHT |FULL ] JOIN table2
ON table1.column-name1 = table2.key-name1 AND TIMEBOUND_EXPRESSION
支持INNER JOIN、LEFT JOIN、RIGHT JOIN和FULL JOIN,如果直接使用JOIN,默认为INNER JOIN。
暂不支持SEMI JOIN和ANTI JOIN。
TIMEBOUND_EXPRESSION为左右两个流时间属性列上的区间条件表达式,支持以下三种条件表达式:
ltime = rtime
ltime >= rtime AND ltime < rtime + INTERVAL '10' MINUTE
ltime BETWEEN rtime - INTERVAL '10' SECOND AND rtime + INTERVAL '5' SECOND
使用示例
统计下单后4个小时内的物流信息。
测试数据
订单表(Orders)
id
productName
orderTime
1
iphone
2024-04-01 10:00:00.0
2
mac
2024-04-01 10:02:00.0
3
huawei
2024-04-01 10:03:00.0
4
pad
2024-04-01 10:05:00.0
物流表(Shipments)
shipId
orderId
status
shiptime
0
1
shipped
2024-04-01 11:00:00.0
1
2
delivered
2024-04-01 17:00:00.0
2
3
shipped
2024-04-01 12:00:00.0
3
4
shipped
2024-04-01 11:30:00.0
测试语句
CREATE TEMPORARY TABLE Orders( id BIGINT, productName VARCHAR, ordertime TIMESTAMP(3), WATERMARK wk FOR ordertime as withOffset(ordertime, 2000) --定义基于ordertime字段的水印,并为其加上了2秒的延迟 ) WITH ( 'connector' = 'kafka', 'topic' = '<yourTopic>', 'properties.bootstrap.servers' = '<yourBrokers>', 'scan.startup.mode' = 'earliest-offset', 'format' = 'csv' ); CREATE TEMPORARY TABLE Shipments( shipId BIGINT, orderId BIGINT, status VARCHAR, shiptime TIMESTAMP(3), WATERMARK wk FOR shiptime as withOffset(shiptime, 2000) --定义基于shiptime字段的水印,并为其加上了2秒的延迟 ) WITH ( 'connector' = 'kafka', 'topic' = '<yourTopic>', 'properties.bootstrap.servers' = '<yourBrokers>', 'scan.startup.mode' = 'earliest-offset', 'format' = 'csv' ); --使用MySQL作为结果表。 CREATE TEMPORARY TABLE rds_output( id BIGINT, productName VARCHAR, status VARCHAR ) WITH ( 'connector' = 'mysql', 'hostname' = '<yourHostname>', 'port' = '3306', 'username' = '<yourUsername>', 'password' = '<yourPassword>', 'database-name' = '<yourDatabaseName>', 'table-name' = '<yourTableName>' ); INSERT INTO rds_output SELECT id, productName, status FROM Orders AS o JOIN Shipments AS s on o.id = s.orderId AND o.ordertime BETWEEN s.shiptime - INTERVAL '4' HOUR AND s.shiptime;
测试结果
id(bigint)
productName(varchar)
status(varchar)
1
iphone
shipped
3
huawei
shipped
4
pad
shipped