如何仅在KSQL中保留最新的窗口?

时间:2019-01-17 07:55:15

标签: ksql

我想监视最近30天发生的事件数。因此,我在KSQL中使用了HOPPING WINDOW(大小为30天,提前1天),但是它将为每个组项目生成总共30个窗口,如何编写ksql脚本以仅保留最新的窗口?谢谢!

CREATE TABLE FEATURE1_INDIVID_APPLY_REJECTED_COUNT_HOPPING_30_DAYS_TBL AS \
SELECT INDIVID_REGION, WindowStart() AS WINSTART, COUNT(*) AS APPLY_REJECTED_COUNT \
FROM FEATURE1_INDIVID_APPLY_REJECTED_HACKTIME_STM  \
     WINDOW HOPPING (SIZE 30 DAYS, ADVANCE BY 1 DAYS) \
WHERE INDIVID_REGION IS NOT NULL \
GROUP BY INDIVID_REGION;

1 个答案:

答案 0 :(得分:0)

您需要的是HAVING过滤器,以过滤掉具有历史性WINDOWEND的所有窗口,例如

CREATE TABLE FEATURE1_INDIVID_APPLY_REJECTED_COUNT_HOPPING_30_DAYS_TBL AS \
SELECT INDIVID_REGION, WindowStart() AS WINSTART, COUNT(*) AS APPLY_REJECTED_COUNT \
FROM FEATURE1_INDIVID_APPLY_REJECTED_HACKTIME_STM  \
     WINDOW HOPPING (SIZE 30 DAYS, ADVANCE BY 1 DAYS) \
WHERE INDIVID_REGION IS NOT NULL \
GROUP BY INDIVID_REGION
HAVING WINDOWEND < UNIX_TIMESTAMP();

(ksqlDB版本0.11的语法正确)

不幸的是,这样的HAVING子句要求Github issue 4397是固定的。一旦ksqlDB在WINDOWEND子句中支持HAVING,您就应该做好了!