{"id":245287,"date":"2022-06-28T08:23:17","date_gmt":"2022-06-28T00:23:17","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=245287"},"modified":"2022-06-19T12:24:02","modified_gmt":"2022-06-19T04:24:02","slug":"flink-sql-over","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/flink-sql-over.html","title":{"rendered":"Flink SQL\u4e4bOver \u805a\u5408\u64cd\u4f5c"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | Over \u805a\u5408\u5b9a\u4e49\uff08\u652f\u6301 Batch\\Streaming\uff09\uff1a\u53ef\u4ee5\u7406\u89e3\u4e3a\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6ed1\u52a8\u7a97\u53e3\u805a\u5408\u51fd\u6570\u3002\u90a3\u8fd9\u91cc\u6211\u4eec\u62ff Over \u805a\u5408\u200b \u4e0e \u7a97\u53e3\u805a\u5408 \u505a\u4e00\u4e2a\u5bf9\u6bd4\uff0c\u5176\u4e4b\u95f4\u7684\u6700\u5927\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff1a\u7a97\u53e3\u805a\u5408\uff1a\u4e0d\u5728 group by \u4e2d\u7684\u5b57\u6bb5\uff0c\u4e0d\u80fd\u76f4\u63a5\u5728 select \u4e2d\u62ff\u5230;Over \u805a\u5408\uff1a\u80fd\u591f\u4fdd\u7559\u539f\u59cb\u5b57\u6bb5.\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0cOver \u805a\u5408\u7684\u4f7f\u7528\u573a\u666f\u8fd8\u662f\u6bd4\u8f83\u5c11\u7684\u3002\u5728 Hive \u4e2d\u4e5f\u6709\u76f8\u540c\u7684\u805a\u5408\uff0c\u4f46\u662f\u5c0f\u4f19\u4f34\u840c\u53ef\u4ee5\u60f3\u60f3\u4f60\u5728\u79bb\u7ebf\u6570\u4ed3\u7ecf\u5e38\u4f7f\u7528\u561b\uff1f<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u5e94\u7528\u573a\u666f\uff1a\u8ba1\u7b97\u6700\u8fd1\u4e00\u6bb5\u6ed1\u52a8\u7a97\u53e3\u7684\u805a\u5408\u7ed3\u679c\u6570\u636e\u3002 SELECT order_id, order_time, amount,\r\n SUM(amount) OVER (\r\n PARTITION BY product\r\n ORDER BY order_time\r\n RANGE BETWEEN INTERVAL '1' HOUR PRECEDING AND CURRENT ROW\r\n ) AS one_hour_prod_amount_sum\r\nFROM Orders<\/pre>\n \u5982\u4e0b\u6848\u4f8b\u6240\u793a\uff1a<\/p>\n \u65f6\u95f4\u533a\u95f4\u805a\u5408<\/strong><\/div>\n \u6309\u7167\u65f6\u95f4\u533a\u95f4\u805a\u5408\u5c31\u662f\u65f6\u95f4\u533a\u95f4\u7684\u4e00\u4e2a\u6ed1\u52a8\u7a97\u53e3\uff0c\u6bd4\u5982\u4e0b\u9762\u6848\u4f8b 1 \u5c0f\u65f6\u7684\u533a\u95f4\uff0c\u6700\u65b0\u8f93\u51fa\u7684\u4e00\u6761\u6570\u636e\u7684 sum \u805a\u5408\u7ed3\u679c\u5c31\u662f\u6700\u8fd1\u4e00\u5c0f\u65f6\u6570\u636e\u7684 amount \u4e4b\u548c\u3002<\/p>\n CREATE TABLE source_table (\r\n order_id BIGINT,\r\n product BIGINT,\r\n amount BIGINT,\r\n order_time as cast(CURRENT_TIMESTAMP as TIMESTAMP(3)),\r\n WATERMARK FOR order_time AS order_time - INTERVAL '0.001' SECOND\r\n) WITH (\r\n 'connector' = 'datagen',\r\n 'rows-per-second' = '1',\r\n 'fields.order_id.min' = '1',\r\n 'fields.order_id.max' = '2',\r\n 'fields.amount.min' = '1',\r\n 'fields.amount.max' = '10',\r\n 'fields.product.min' = '1',\r\n 'fields.product.max' = '2'\r\n);\r\n\r\nCREATE TABLE sink_table (\r\n product BIGINT,\r\n order_time TIMESTAMP(3),\r\n amount BIGINT,\r\n one_hour_prod_amount_sum BIGINT\r\n) WITH (\r\n 'connector' = 'print'\r\n);\r\n\r\nINSERT INTO sink_table\r\nSELECT product, order_time, amount,\r\n SUM(amount) OVER (\r\n PARTITION BY product\r\n ORDER BY order_time\r\n -- \u6807\u8bc6\u7edf\u8ba1\u8303\u56f4\u662f\u4e00\u4e2a product \u7684\u6700\u8fd1 1 \u5c0f\u65f6\u7684\u6570\u636e\r\n RANGE BETWEEN INTERVAL '1' HOUR PRECEDING AND CURRENT ROW\r\n ) AS one_hour_prod_amount_sum\r\nFROM source_table<\/pre>\n |