{"id":538,"date":"2021-12-10T10:27:50","date_gmt":"2021-12-10T02:27:50","guid":{"rendered":"https:\/\/blog.frost-s.tk\/?p=538"},"modified":"2021-12-16T22:03:24","modified_gmt":"2021-12-16T14:03:24","slug":"flink-sql-%e7%aa%97%e5%8f%a3%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/blog.frost-s.com\/index.php\/2021\/12\/10\/flink-sql-%e7%aa%97%e5%8f%a3%e5%87%bd%e6%95%b0\/","title":{"rendered":"Flink sql  \u7a97\u53e3\u51fd\u6570"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u6982\u8ff0<br>Flink\u7a97\u53e3\u51fd\u6570\u662fflink\u7684\u91cd\u8981\u7279\u6027\uff0c\u800cFlink SQL API\u662fFlink\u6279\u6d41\u4e00\u4f53\u7684\u5c01\u88c5\uff0c\u5b66\u4e60\u660e\u767d\u672c\u8282\u8bfe\uff0c\u662f\u5bf9Flink\u5b66\u4e60\u7684\u5f88\u5927\u6536\u76ca\uff01<\/p><\/blockquote>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7a97\u53e3\u51fd\u6570<\/h2>\n\n\n\n<p>\u7a97\u53e3\u51fd\u6570Flink SQL\u652f\u6301\u57fa\u4e8e\u65e0\u9650\u5927\u7a97\u53e3\u7684\u805a\u5408\uff08\u65e0\u9700\u5728SQL Query\u4e2d\uff0c\u663e\u5f0f\u5b9a\u4e49\u4efb\u4f55\u7a97\u53e3\uff09\u4ee5\u53ca\u5bf9\u4e00\u4e2a\u7279\u5b9a\u7684\u7a97\u53e3\u7684\u805a\u5408\u3002\u4f8b\u5982\uff0c\u9700\u8981\u7edf\u8ba1\u5728\u8fc7\u53bb\u76841\u5206\u949f\u5185\u6709\u591a\u5c11\u7528\u6237\u70b9\u51fb\u4e86\u67d0\u4e2a\u7684\u7f51\u9875\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7a97\u53e3\u6765\u6536\u96c6\u6700\u8fd11\u5206\u949f\u5185\u7684\u6570\u636e\uff0c\u5e76\u5bf9\u8fd9\u4e2a\u7a97\u53e3\u5185\u7684\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n\n\n\n<p>Flink SQL\u652f\u6301\u7684\u7a97\u53e3\u805a\u5408\u4e3b\u8981\u662f\u4e24\u79cd\uff1aWindow\u805a\u5408\u548cOver\u805a\u5408\u3002\u672c\u6587\u6863\u4e3b\u8981\u4e3a\u60a8\u4ecb\u7ecdWindow\u805a\u5408\u3002Window\u805a\u5408\u652f\u6301Event Time\u548cProcessing Time\u4e24\u79cd\u65f6\u95f4\u5c5e\u6027\u5b9a\u4e49\u7a97\u53e3\u3002\u6bcf\u79cd\u65f6\u95f4\u5c5e\u6027\u7c7b\u578b\u652f\u6301\u4e09\u79cd\u7a97\u53e3\u7c7b\u578b\uff1a\u6eda\u52a8\u7a97\u53e3\uff08TUMBLE\uff09\u3001\u6ed1\u52a8\u7a97\u53e3\uff08HOP\uff09\u548c\u4f1a\u8bdd\u7a97\u53e3\uff08SESSION\uff09\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u65f6\u95f4\u5c5e\u6027<\/h2>\n\n\n\n<p>Flink SQL\u652f\u6301\u4ee5\u4e0b\u4e24\u79cd\u65f6\u95f4\u5c5e\u6027\u3002\u5b9e\u65f6\u8ba1\u7b97\u53ef\u4ee5\u57fa\u4e8e\u8fd9\u4e24\u79cd\u65f6\u95f4\u5c5e\u6027\u5bf9\u6570\u636e\u8fdb\u884c\u7a97\u53e3\u805a\u5408\u3002<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/help.aliyun.com\/document_detail\/96878.htm#section-jf3-mhf-5fb\">Event Time<\/a>\uff1a\u60a8\u63d0\u4f9b\u7684\u4e8b\u4ef6\u65f6\u95f4\uff08\u901a\u5e38\u662f\u6570\u636e\u7684\u6700\u539f\u59cb\u7684\u521b\u5efa\u65f6\u95f4\uff09\uff0cEvent Time\u4e00\u5b9a\u662f\u60a8\u63d0\u4f9b\u5728Schema\u91cc\u7684\u6570\u636e\u3002<\/li><li><a href=\"https:\/\/help.aliyun.com\/document_detail\/96878.htm#section-lv4-5kf-5fb\">Processing Time<\/a>\uff1a\u5bf9\u4e8b\u4ef6\u8fdb\u884c\u5904\u7406\u7684\u672c\u5730\u7cfb\u7edf\u65f6\u95f4\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u8bf4\u660e<\/strong> \u5b9e\u65f6\u8ba1\u7b97\u65f6\u95f4\u5c5e\u6027\u8be6\u60c5\uff0c\u8bf7\u53c2\u89c1<a href=\"https:\/\/help.aliyun.com\/document_detail\/96878.htm#concept-swd-nmd-5fb\">\u65f6\u95f4\u5c5e\u6027<\/a>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ea7\u8054\u7a97\u53e3<\/h2>\n\n\n\n<p>Rowtime\u5217\u5728\u7ecf\u8fc7\u7a97\u53e3\u64cd\u4f5c\u540e\uff0c\u5176Event Time\u5c5e\u6027\u5c06\u4e22\u5931\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u8f85\u52a9\u51fd\u6570<code>TUMBLE_ROWTIME<\/code>\u3001<code>HOP_ROWTIME<\/code>\u6216<code>SESSION_ROWTIME<\/code>\uff0c\u83b7\u53d6\u7a97\u53e3\u4e2d\u7684Rowtime\u5217\u7684\u6700\u5927\u503c<code>max(rowtime)<\/code>\u4f5c\u4e3a\u65f6\u95f4\u7a97\u53e3\u7684Rowtime\uff0c\u5176\u7c7b\u578b\u662f\u5177\u6709Rowtime\u5c5e\u6027\u7684TIMESTAMP\uff0c\u53d6\u503c\u4e3a <code>window_end - 1<\/code> \u3002 \u4f8b\u5982<code>[00:00, 00:15)<\/code>\u7684\u7a97\u53e3\uff0c\u8fd4\u56de\u503c\u4e3a<code>00:14:59.999<\/code> \u3002<\/p>\n\n\n\n<p>\u793a\u4f8b\u903b\u8f91\u4e3a\uff1a\u57fa\u4e8e1\u5206\u949f\u7684\u6eda\u52a8\u7a97\u53e3\u805a\u5408\u7ed3\u679c\uff0c\u8fdb\u884c1\u5c0f\u65f6\u7684\u6eda\u52a8\u7a97\u53e3\u805a\u5408\uff0c\u53ef\u4ee5\u6ee1\u8db3\u60a8\u7684\u591a\u7ef4\u5ea6\u5f00\u7a97\u9700\u6c42\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE user_clicks(\n  username varchar,\n  click_url varchar,\n  ts timeStamp,\n  WATERMARK wk FOR ts as withOffset(ts, 2000)  --\u4e3aRowtime\u5b9a\u4e49Watermark\u3002\n) with (\n  type='datahub',\n  ...\n);\n\nCREATE TABLE tumble_output(\n  window_start TIMESTAMP,\n  window_end TIMESTAMP,\n  username VARCHAR,\n  clicks BIGINT\n) with (\n  type='print'\n);\n\nCREATE VIEW one_minute_window_output as\nSELECT \n  \/\/ \u4f7f\u7528TUMBLE_ROWTIME\u4f5c\u4e3a\u4e8c\u7ea7Window\u7684\u805a\u5408\u65f6\u95f4\u3002\n  TUMBLE_ROWTIME(ts, INTERVAL '1' MINUTE) as rowtime,  \n  username, \n  COUNT(click_url) as cnt\nFROM user_clicks\nGROUP BY TUMBLE(ts, INTERVAL '1' MINUTE), username;\n\nINSERT INTO tumble_output\nSELECT\n  TUMBLE_START(rowtime, INTERVAL '1' HOUR),\n  TUMBLE_END(rowtime, INTERVAL '1' HOUR),\n  username,\n  SUM(cnt)\nFROM one_minute_window_output\nGROUP BY TUMBLE(rowtime, INTERVAL '1' HOUR), username;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0Flink\u7a97\u53e3\u51fd\u6570\u662fflink\u7684\u91cd\u8981\u7279\u6027\uff0c\u800cFlink SQL API\u662fFlink\u6279\u6d41\u4e00\u4f53\u7684\u5c01\u88c5\uff0c\u5b66\u4e60\u660e\u767d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":695,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,8,6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/538"}],"collection":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/comments?post=538"}],"version-history":[{"count":4,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/538\/revisions"}],"predecessor-version":[{"id":810,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/538\/revisions\/810"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/media\/695"}],"wp:attachment":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/media?parent=538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/categories?post=538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/tags?post=538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}