{"id":4557,"date":"2023-09-03T22:26:44","date_gmt":"2023-09-03T14:26:44","guid":{"rendered":"\/?p=4557"},"modified":"2023-09-04T21:35:25","modified_gmt":"2023-09-04T13:35:25","slug":"24-window%e5%92%8cwatermark","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=4557","title":{"rendered":"24.Window\u548cWatermark"},"content":{"rendered":"<p>\u4e0a\u4e00\u8282\u6211\u4eec\u8bf4\u4e86\u5728\u6d41\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u6d89\u53ca\u7684\u5904\u7406\u6a21\u5f0f\uff0c\u6bd4\u5982Batch mode\u548cContinuous mode<\/p>\n<p>\u8fd9\u4e24\u8005\u867d\u7136\u8fd0\u884c\u7684\u6a21\u5f0f\u4e0d\u540c\uff0c\u4f46\u662f\u6700\u7ec8\u90fd\u5efa\u7acb\u5728\u4e86Spark SQL\u4e4b\u4e0a\uff0c\u5728\u7f16\u5199\u5b8c\u6210\u90fd\u662f\u4ea4\u4ed8\u7ed9Spark SQL\u548cSpark Core\u8fdb\u884c\u6267\u884c<\/p>\n<p>\u4e0d\u8fc7\u9664\u4e86\u57fa\u672c\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u4e4b\u5916\uff0c\u5728Continuous mode\u8fd8\u6709\u7740\u4e00\u4e9b\u4e13\u95e8\u9488\u5bf9\u6d41\u5904\u7406\u7684\u80fd\u529b\uff0c\u6bd4\u5982Window\u64cd\u4f5c\u548cWatermark\u914d\u5408\u5ef6\u8fdf\u6570\u636e\u5904\u7406<\/p>\n<p>\u6211\u4eec\u5c31\u8bf4\u4e0b\u5728\u5176\u4e2d\u7684\u7279\u6b8a\u64cd\u4f5c<\/p>\n<p>\u9996\u5148\u662fWindow\u64cd\u4f5c\uff0c\u5176\u662fStructured Streaming\u5f15\u64ce\u4f1a\u57fa\u4e8e\u4e00\u5b9a\u65f6\u95f4\uff0c\u5f62\u6210\u4e00\u4e2a\u7a97\u53e3\uff0c\u5305\u542b\u4e00\u6279\u6570\u636e\uff0c\u8ba9\u6570\u636e\u6d41\u5bf9\u8fd9\u4e00\u6279\u6570\u636e\u8fdb\u884c\u6d88\u8d39\u5904\u7406<\/p>\n<p>\u5728\u6b64\u4e4b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u4e24\u4e2a\u6982\u5ff5\uff0cEvent time\u548cProcessing Time\uff0c\u548cFlink\u4e00\u6837\uff0c\u6307\u7684\u662f\u4e8b\u4ef6\u65f6\u95f4\u548c\u5904\u7406\u65f6\u95f4<\/p>\n<p>\u4e8b\u4ef6\u65f6\u95f4\u5bf9\u5e94\u7684\u662f\u6d88\u606f\u751f\u6210\u7684\u65f6\u95f4\uff0c\u4e00\u822c\u4ece\u6d88\u606f\u4e2d\u76f4\u63a5\u53d6<\/p>\n<p>\u5904\u7406\u65f6\u95f4\u5219\u662f\u6d88\u606f\u5230\u8fbeStructured Streaming \u5f15\u64ce\u7684\u65f6\u95f4\uff0c\u4e5f\u88ab\u79f0\u4e3a\u5230\u8fbe\u65f6\u95f4<\/p>\n<p>Window\u540c\u65f6\u652f\u6301\u4e24\u79cd\u65f6\u95f4\uff0c\u800c\u4e14\u8fd8\u652f\u6301\u4e0d\u540c\u7684\u7a97\u53e3\u5212\u5206\u65b9\u5f0f\uff0c\u5206\u522b\u662fTumbling Window\u548cSliding Window\u3002\u5bf9\u4e8eTumbling Window\uff0c\u5212\u5206\u7684\u7a97\u53e3\u4e0d\u4f1a\u51fa\u73b0\u91cd\u53e0\uff0c\u5bf9\u4e8eSliding Window\uff0c\u5219\u53ef\u80fd\u4f1a\u51fa\u73b0\u91cd\u53e0\uff0c\u4ee5\u53ca\u51fa\u73b0\u9057\u6f0f<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"865\" height=\"408\" src=\"\/wp-content\/uploads\/2023\/07\/unnamed-file-125.png\" class=\"wp-image-4558\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2023\/07\/unnamed-file-125.png 865w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2023\/07\/unnamed-file-125-300x142.png 300w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2023\/07\/unnamed-file-125-768x362.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/p>\n<p>\u6211\u4eec\u76f4\u63a5\u62ff\u4e00\u4e2a\u5b9e\u4f8b\u8fdb\u884c\u5c55\u793a\uff0c\u5176\u4e2d\u6d89\u53ca\u7684Window\u64cd\u4f5c<\/p>\n<table>\n<tr>\n<td>\n  df = df.withColumn(&#8220;inputs&#8221;,   split($&#8221;value&#8221;, &#8220;,&#8221;))<\/p>\n<p>\/\/ \u63d0\u53d6\u4e8b\u4ef6\u65f6\u95f4<\/p>\n<p>.withColumn(&#8220;eventTime&#8221;,   element_at(col(&#8220;inputs&#8221;),1).cast(&#8220;timestamp&#8221;)) 4 \/\/ \u63d0\u53d6\u5355\u8bcd\u5e8f\u5217<\/p>\n<p>.withColumn(&#8220;words&#8221;,   split(element_at(col(&#8220;inputs&#8221;),2), &#8221; &#8220;))<\/p>\n<p>\/\/   \u62c6\u5206\u5355\u8bcd<\/p>\n<p>.withColumn(&#8220;word&#8221;,   explode($&#8221;words&#8221;))<\/p>\n<p>\/\/ \u6309\u7167Tumbling   Window\u4e0e\u5355\u8bcd\u505a\u5206\u7ec4<\/p>\n<p>.groupBy(window(col(&#8220;eventTime&#8221;),   &#8220;5 minute&#8221;), col(&#8220;word&#8221;))<\/p>\n<p>\/\/   \u7edf\u8ba1\u8ba1\u6570<\/p>\n<p>.count()<\/td>\n<\/tr>\n<\/table>\n<p>\u8fd9\u91cc\u6211\u4eec\u7684\u8f93\u5165\u6570\u636e\u5982\u4e0b<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"865\" height=\"372\" src=\"\/wp-content\/uploads\/2023\/07\/unnamed-file-126.png\" class=\"wp-image-4559\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2023\/07\/unnamed-file-126.png 865w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2023\/07\/unnamed-file-126-300x129.png 300w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2023\/07\/unnamed-file-126-768x330.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/p>\n<p>\u6211\u4eec\u9996\u5148\u8fdb\u884c\u4e86\u5206\u5272\uff0c\u7136\u540e\u53d6\u7b2c\u4e00\u4e2a\u5ea7\u4f4deventTime,\u7136\u540e\u62c6\u5206\u4e86Word,<\/p>\n<p>\u4e4b\u540e\u521b\u5efa\u4e86Tumbling Window<\/p>\n<p>\u8fd9\u91cc\u6307\u5b9a\u4e86\u7a97\u53e3\u9700\u8981\u7684\u5b57\u6bb5\uff0c\u4ee5\u53ca\u5206\u5272\u7684\u7a97\u53e3\u5927\u5c0f<\/p>\n<p>\u5982\u679c\u5e0c\u671b\u4fee\u6539\u7a97\u53e3\u5927\u5c0f\uff0c\u5219\u76f4\u63a5\u6dfb\u52a0\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u4e5f\u5c31\u662f\u7a97\u53e3\u95f4\u9694<\/p>\n<p>\u4e4b\u540e\u4fbf\u662fLate data\u548cWatermark<\/p>\n<p>\u5bf9\u4e8eLate data\uff0c\u6307\u7684\u662f\u4e8b\u4ef6\u65f6\u95f4\u548c\u5904\u7406\u65f6\u95f4\u4e0d\u4e00\u81f4\u7684\u6d88\u606f\uff0c\u5e38\u89c1\u7684\u5c31\u662f\u8fdf\u5230\u6d88\u606f<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3 Late data \u7684\u95ee\u9898\uff0cStreaming \u91c7\u7528Watermark\u7684\u673a\u5236\uff0c\u5176\u4e2d\u5305\u542b\u7740\u4e24\u4e2a\u6982\u5ff5\uff0c\u6c34\u5370\u548c\u6c34\u4f4d\u7ebf<\/p>\n<p>\u5f53\u6709\u65b0\u6d88\u606f\u5230\u8fbe\u7cfb\u7edf\u7684\u65f6\u5019\uff0cStreaming\u4f1a\u9996\u5148\u5224\u65ad\u5176\u7684\u65f6\u95f4\uff0c\u662f\u5426\u5927\u4e8e\u6c34\u5370\uff0c\u5982\u679c\u5927\u4e8e\u6c34\u5370\uff0c\u5219\u8fdb\u884c\u66f4\u65b0\u6c34\u5370\u548c\u6c34\u4f4d\u7ebf<\/p>\n<p>\u5982\u679c\u6d88\u606f\u5728\u5f53\u524d\u6c34\u5370\u4e4b\u4e0b\uff0c\u90a3\u4e48\u5c31\u5224\u65adWatermark\u65f6\u95f4\u7a97\u53e3\u4e0b\u6cbf\uff0c\u4e5f\u5c31\u662f\u672a\u8fc7\u671f\u7684\u65f6\u95f4\u7a97\u53e3\u7684\u8d77\u59cb\u65f6\u95f4\u548c\u5f53\u524d\u7684\u6d88\u606f\u643a\u5e26\u65f6\u95f4\uff0c\u5982\u679c\u5927\u4e8e\u4e86\u7a97\u53e3\u4e0b\u6cbf\uff0c\u5c31\u53c2\u4e0e\u8fd9\u4e2a\u7a97\u53e3\u7684\u8ba1\u7b97<\/p>\n<p>\u4e0d\u7136\u5c31\u8fdb\u884c\u629b\u5f03<\/p>\n<p>\u800c\u8981\u8bbe\u7f6e\u5bb9\u5fcd\u5ea6\uff0c\u53ea\u9700\u8981\u7ed9dataFrame\u8bbe\u7f6e<\/p>\n<p>WaterMark\u5373\u53ef<\/p>\n<p>withWatermark(&#8220;eventTime&#8221;, &#8220;10 minute&#8221;)<\/p>\n<p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u8bbe\u7f6e\u4e86\u5bf9\u5e94\u7684\u6c34\u4f4d\u7ebf<\/p>\n<p>\u4ece\u800c\u589e\u52a0\u4e86\u5bb9\u5fcd10\u5206\u949f\u7684\u8fdf\u5230\u6570\u636e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e00\u8282\u6211\u4eec\u8bf4\u4e86\u5728\u6d41\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/4557"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4557"}],"version-history":[{"count":0,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/4557\/revisions"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4557"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}