| |
| |
|
|
| |
| CREATE TABLE orders_source ( |
| order_id BIGINT, |
| product_id INT, |
| amount DOUBLE, |
| event_time AS LOCALTIMESTAMP, |
| WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND |
| ) WITH ( |
| 'connector' = 'datagen', |
| 'rows-per-second' = '10', |
| 'fields.order_id.kind' = 'random', |
| 'fields.order_id.min' = '1', |
| 'fields.order_id.max' = '1000000', |
| 'fields.product_id.kind' = 'random', |
| 'fields.product_id.min' = '1', |
| 'fields.product_id.max' = '1000', |
| 'fields.amount.kind' = 'random', |
| 'fields.amount.min' = '1', |
| 'fields.amount.max' = '10000' |
| ); |
|
|
| |
| CREATE TABLE console_output ( |
| window_start TIMESTAMP(3), |
| product_id INT, |
| sales DOUBLE, |
| order_cnt BIGINT, |
| rn BIGINT |
| ) WITH ( |
| 'connector' = 'print' |
| ); |
|
|
| |
| INSERT INTO console_output |
| SELECT |
| window_start, |
| product_id, |
| sales, |
| order_cnt, |
| rn |
| FROM ( |
| SELECT |
| TUMBLE_START(event_time, INTERVAL '10' SECOND) AS window_start, |
| product_id, |
| SUM(amount) AS sales, |
| COUNT(*) AS order_cnt, |
| ROW_NUMBER() OVER ( |
| PARTITION BY TUMBLE_START(event_time, INTERVAL '10' SECOND) |
| ORDER BY SUM(amount) DESC |
| ) AS rn |
| FROM orders_source |
| GROUP BY TUMBLE(event_time, INTERVAL '10' SECOND), product_id |
| ) |
| WHERE rn <= 3; |
|
|