{"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":"\n\n\n
\u5bfc\u8bfb<\/td>\nOver \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
\n\u5b9e\u9645\u6848\u4f8b\uff1a\u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u6700\u8fd1\u4e00\u5c0f\u65f6\u8ba2\u5355\u7684\u91d1\u989d\u603b\u548c\uff1a<\/p>\n

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

Over \u805a\u5408\u7684\u8bed\u6cd5\u603b\u7ed3\u5982\u4e0b\uff1a<\/p>\n

SELECT\r\n  agg_func(agg_col) OVER (\r\n    [PARTITION BY col1[, col2, ...]]\r\n    ORDER BY time_col\r\n    range_definition),\r\n  ...\r\nFROM ...<\/pre>\n

\u5176\u4e2d\uff1a<\/p>\n

  • ORDER BY\uff1a\u5fc5\u987b\u662f\u65f6\u95f4\u6233\u5217\uff08\u4e8b\u4ef6\u65f6\u95f4\u3001\u5904\u7406\u65f6\u95f4\uff09<\/li>\n
  • PARTITION BY\uff1a\u6807\u8bc6\u4e86\u805a\u5408\u7a97\u53e3\u7684\u805a\u5408\u7c92\u5ea6\uff0c\u5982\u4e0a\u8ff0\u6848\u4f8b\u662f\u6309\u7167 product \u8fdb\u884c\u805a\u5408<\/li>\n
  • range_definition\uff1a\u8fd9\u4e2a\u6807\u8bc6\u805a\u5408\u7a97\u53e3\u7684\u805a\u5408\u6570\u636e\u8303\u56f4\uff0c\u5728 Flink \u4e2d\u6709\u4e24\u79cd\u6307\u5b9a\u6570\u636e\u8303\u56f4\u7684\u65b9\u5f0f\u3002\u7b2c\u4e00\u79cd\u4e3a\u6309\u7167\u884c\u6570\u805a\u5408\u200b\uff0c\u7b2c\u4e8c\u79cd\u4e3a\u6309\u7167\u65f6\u95f4\u533a\u95f4\u805a\u5408\u3002<\/li>\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

    \u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

    +I[2, 2021-12-24T22:08:26.583, 7, 73]\r\n+I[2, 2021-12-24T22:08:27.583, 7, 80]\r\n+I[2, 2021-12-24T22:08:28.583, 4, 84]\r\n+I[2, 2021-12-24T22:08:29.584, 7, 91]\r\n+I[2, 2021-12-24T22:08:30.583, 8, 99]\r\n+I[1, 2021-12-24T22:08:31.583, 9, 138]\r\n+I[2, 2021-12-24T22:08:32.584, 6, 105]\r\n+I[1, 2021-12-24T22:08:33.584, 7, 145]<\/pre>\n
    \u884c\u6570\u805a\u5408<\/strong><\/div>\n

    \u6309\u7167\u884c\u6570\u805a\u5408\u5c31\u662f\u6570\u636e\u884c\u6570\u7684\u4e00\u4e2a\u6ed1\u52a8\u7a97\u53e3\uff0c\u6bd4\u5982\u4e0b\u9762\u6848\u4f8b\uff0c\u6700\u65b0\u8f93\u51fa\u7684\u4e00\u6761\u6570\u636e\u7684 sum \u805a\u5408\u7ed3\u679c\u5c31\u662f\u6700\u8fd1 5 \u884c\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' = '2',\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 5 \u884c\u6570\u636e\r\n    ROWS BETWEEN 5 PRECEDING AND CURRENT ROW\r\n  ) AS one_hour_prod_amount_sum\r\nFROM source_table<\/pre>\n

    \u9884\u8dd1\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

    +I[2, 2021-12-24T22:18:19.147, 1, 9]\r\n+I[1, 2021-12-24T22:18:20.147, 2, 11]\r\n+I[1, 2021-12-24T22:18:21.147, 2, 12]\r\n+I[1, 2021-12-24T22:18:22.147, 2, 12]\r\n+I[1, 2021-12-24T22:18:23.148, 2, 12]\r\n+I[1, 2021-12-24T22:18:24.147, 1, 11]\r\n+I[1, 2021-12-24T22:18:25.146, 1, 10]\r\n+I[1, 2021-12-24T22:18:26.147, 1, 9]\r\n+I[2, 2021-12-24T22:18:27.145, 2, 11]\r\n+I[2, 2021-12-24T22:18:28.148, 1, 10]\r\n+I[2, 2021-12-24T22:18:29.145, 2, 10]<\/pre>\n

    \u5f53\u7136\uff0c\u5982\u679c\u4f60\u5728\u4e00\u4e2a SELECT \u4e2d\u6709\u591a\u4e2a\u805a\u5408\u7a97\u53e3\u7684\u805a\u5408\u65b9\u5f0f\uff0cFlink SQL \u652f\u6301\u4e86\u4e00\u79cd\u7b80\u5316\u5199\u6cd5\uff0c\u5982\u4e0b\u6848\u4f8b\uff1a<\/p>\n

    SELECT order_id, order_time, amount,\r\n  SUM(amount) OVER w AS sum_amount,\r\n  AVG(amount) OVER w AS avg_amount\r\nFROM Orders\r\n-- \u4f7f\u7528\u4e0b\u9762\u5b50\u53e5\uff0c\u5b9a\u4e49 Over Window\r\nWINDOW w AS (\r\n  PARTITION BY product\r\n  ORDER BY order_time\r\n  RANGE BETWEEN INTERVAL '1' HOUR PRECEDING AND CURRENT ROW)<\/pre>\n","protected":false},"excerpt":{"rendered":"

    \u5e94\u7528\u573a\u666f\uff1a\u8ba1\u7b97\u6700\u8fd1\u4e00\u6bb5\u6ed1\u52a8\u7a97\u53e3\u7684\u805a\u5408\u7ed3\u679c\u6570\u636e\u3002 \u5b9e\u9645\u6848\u4f8b\uff1a\u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u6700\u8fd1\u4e00\u5c0f\u65f6\u8ba2\u5355\u7684\u91d1\u989d\u603b\u548c\uff1a SELECT […]<\/p>\n","protected":false},"author":1920,"featured_media":200375,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-245287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/245287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=245287"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/245287\/revisions"}],"predecessor-version":[{"id":245289,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/245287\/revisions\/245289"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/200375"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=245287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=245287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=245287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}