{"id":4389,"date":"2019-05-01T21:04:03","date_gmt":"2019-05-01T13:04:03","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4389"},"modified":"2019-05-01T21:04:03","modified_gmt":"2019-05-01T13:04:03","slug":"hive%e7%aa%97%e5%8f%a3%e5%87%bd%e6%95%b0%e7%ae%80%e5%8d%95%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4389.html","title":{"rendered":"Hive\u7a97\u53e3\u51fd\u6570\u7b80\u5355\u5b66\u4e60"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u7b80\u5355\u8bb0\u5f55\u4e00\u4e0b\u524d\u6bb5\u65f6\u95f4\u5b66\u4e60\u5230\u7684Hive\u7a97\u53e3\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u65b9\u4fbf\u540e\u671f\u4f7f\u7528\u53c2\u8003\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6>\u4e00\u3001\u7a97\u53e3\u51fd\u6570\u7684\u5b9a\u4e49<\/h6>\n<p class=\"\"><span style=\"color: #ff0000;\">\u7a97\u53e3\u51fd\u6570\u4e5f\u79f0\u4e3aOLAP\uff08Online Analytical Processing\uff09\u51fd\u6570<\/span>\uff0c\u662f\u5bf9\u4e00\u7ec4\u503c\u8fdb\u884c\u64cd\u4f5c\uff0c<span style=\"color: #ff0000;\"><strong>\u4e0d\u9700\u8981\u4f7f\u7528Group by\u5b50\u53e5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u8fd8\u80fd\u5728\u540c\u4e00\u884c\u8fd4\u56de\u539f\u6765\u884c\u7684\u5217\u548c\u4f7f\u7528\u805a\u5408\u51fd\u6570\u5f97\u5230\u7684\u805a\u5408\u5217<\/strong>\u3002<\/span><\/p>\n<p class=\"\">\u90a3\u4e3a\u4ec0\u4e48\u53eb\u7a97\u53e3\u51fd\u6570\u5462\uff1f<strong>\u56e0\u4e3a\u7a97\u53e3\u51fd\u6570\u5c06\u8868\u4ee5\u7a97\u53e3\u4e3a\u5355\u4f4d\u8fdb\u884c\u5206\u5272\uff0c\u5e76\u5728\u5176\u4e2d\u8fdb\u884c\u5404\u79cd\u5206\u6790\u64cd\u4f5c\uff0c\u4e3a\u4e86\u8ba9\u5927\u5bb6\u5feb\u901f\u5f62\u6210\u76f4\u89c2\u5370\u8c61\uff0c\u624d\u8d77\u4e86\u8fd9\u6837\u4e00\u4e2a\u5bb9\u6613\u7406\u89e3\u7684\u540d\u79f0<\/strong>\u3002<\/p>\n<h6>\u4e8c\u3001\u7a97\u53e3\u51fd\u6570\u7684SQL\u8bed\u6cd5<\/h6>\n<pre class=\"lang:default decode:true\">&lt;\u7a97\u53e3\u51fd\u6570&gt;()\r\nOVER\r\n(\r\n  [PARTITION BY &lt;\u5217\u6e05\u5355&gt;]\r\n  [ORDER BY &lt;\u6392\u5e8f\u7528\u6e05\u5355\u5217&gt;] [ASC\/DESC]\r\n  (ROWS | RANGE) &lt;\u8303\u56f4\u6761\u4ef6&gt;\r\n)<\/pre>\n<p class=\"\">\u7a97\u53e3\u51fd\u6570\u7684\u8bed\u6cd5\u5206\u4e3a<strong>\u56db\u4e2a\u90e8\u5206<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u51fd\u6570\u5b50\u53e5<\/strong>\uff1a\u6307\u660e\u5177\u4f53\u64cd\u4f5c\uff0c\u5982sum-\u6c42\u548c\uff0cfirst_value-\u53d6\u7b2c\u4e00\u4e2a\u503c\uff1b<\/li>\n<li><strong>partition by\u5b50\u53e5<\/strong>\uff1a\u6307\u660e\u5206\u533a\u5b57\u6bb5\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5219\u5c06\u6240\u6709\u6570\u636e\u4f5c\u4e3a\u4e00\u4e2a\u5206\u533a\uff1b<\/li>\n<li><strong>order by\u5b50\u53e5<\/strong>\uff1a\u6307\u660e\u4e86\u6bcf\u4e2a\u5206\u533a\u6392\u5e8f\u7684\u5b57\u6bb5\u548c\u65b9\u5f0f,\u4e5f\u662f\u53ef\u9009\u7684\uff0c\u6ca1\u6709\u5c31\u662f\u6309\u7167\u8868\u4e2d\u7684\u987a\u5e8f\uff1b<\/li>\n<li><strong>\u7a97\u53e3\u5b50\u53e5<\/strong>\uff1a\u6307\u660e\u76f8\u5bf9\u5f53\u524d\u8bb0\u5f55\u7684\u8ba1\u7b97\u8303\u56f4\uff0c\u53ef\u4ee5\u5411\u4e0a\uff08preceding\uff09\uff0c\u53ef\u4ee5\u5411\u4e0b\uff08following\uff09,\u4e5f\u53ef\u4ee5\u4f7f\u7528between\u6307\u660e\uff0c\u4e0a\u4e0b\u8fb9\u754c\u7684\u503c\uff0c\u6ca1\u6709\u7684\u8bdd\u9ed8\u8ba4\u4e3a\u5f53\u524d\u5206\u533a\u3002<\/li>\n<\/ul>\n<h6>\u4e09\u3001\u7a97\u53e3\u51fd\u6570\u7684\u5206\u7c7b<\/h6>\n<p>\u7a97\u53e3\u51fd\u6570\u7684\u529f\u80fd\u5206\u4e3a\uff1a<strong>\u8ba1\u7b97\u3001\u53d6\u503c\u3001\u6392\u5e8f\u3001\u5e8f\u5217<\/strong>\u56db\u79cd\uff0c\u524d\u4e09\u79cd\u7684\u4f7f\u7528\u573a\u666f\u6bd4\u8f83\u5e38\u89c1\uff0c\u5bb9\u6613\u7406\u89e3\uff0c\u6700\u540e\u4e00\u79cd(\u5e8f\u5217)\u7684\u4f7f\u7528\u573a\u666f\u6bd4\u8f83\u5c11\u3002<\/p>\n<ul>\n<li><strong>\u8ba1\u7b97<\/strong>\n<ul>\n<li>count\u3001sum\u3001avg\u3001max\/min<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u53d6\u503c<\/strong>\n<ul>\n<li><strong>first_value\/last_value\u3001lag\/lead<\/strong><\/li>\n<\/ul>\n<\/li>\n<li><strong>\u6392\u5e8f<\/strong>\n<ul>\n<li><strong><span style=\"color: #ff0000;\">rank<\/span>\u3001dense_rank\u3001<span style=\"color: #ff0000;\">row_number<\/span>\u3001ntitle<\/strong><\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5e8f\u5217<\/strong>\n<ul>\n<li>cume_dist\u3001percent_rank<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h6>\u5e38\u89c1\u7528\u6cd51\u3001\u5206\u7ec4\u6392\u5e8f\u53d6top<\/h6>\n<pre class=\"lang:default decode:true \">\/*\r\n\t\u7edf\u8ba1Web\u8bbf\u95ee\u65e5\u5fd7\u4e2d\uff1a\r\n\t\u6bcf\u4e2a\u57df\u540d\u8bbf\u95ee\u91cf\u6700\u5927\u7684\u65e5\u671f\u662f\u54ea\u51e0\u5929\uff1f\r\n\r\n\t\u5728 table \u4e2d\u6ca1\u6709\u4e00\u4e2a\u76f4\u63a5\u7684\u5b57\u6bb5\u53ef\u7528\u4e8e\u6392\u5e8f\u65f6\uff0c\u53ef\u4ee5\u501f\u52a9 count+[group by] \u5148\u751f\u6210\u4e00\u4e2a\u5b57\u6bb5\uff0c\u518d\u501f\u52a9 row_number() \u8fdb\u884c\u5206\u533a\u6392\u5e8f\u3002\r\n*\/\r\nselect xx.*\r\nfrom\r\n(select dt, domain, cnt, row_number() over(partition by domain order by cnt desc) rank\r\nfrom\r\n(select dt, domain, count(*) as cnt from log.access_log where dt between '20190401' and '20190403' group by dt,domain\r\nhaving cnt between 1000 and 100000\r\n)x\r\n)xx\r\nwhere rank &lt;= 3\r\n;<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5e38\u89c1\u4f7f\u7528\u573a\u666f\uff1a<\/p>\n<ul>\n<li>\u53ef\u53c2\u8003\u300c<a href=\"https:\/\/mp.weixin.qq.com\/s\/JIJCtl63eGld5dhu3s-jZw\">Hive\u7a97\u53e3\u51fd\u6570\u8fdb\u9636\u6307\u5357<\/a>\u300d<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/lxw1234.com\/archives\/tag\/hive-window-functions\">\u6807\u7b7e\uff1ahive\u5206\u6790\u51fd\u6570<\/a><\/li>\n<li><a href=\"http:\/\/lxw1234.com\/archives\/2015\/04\/176.htm\">Hive\u5206\u6790\u7a97\u53e3\u51fd\u6570(\u4e00) SUM,AVG,MIN,MAX<\/a><\/li>\n<li><a href=\"http:\/\/lxw1234.com\/archives\/2015\/04\/181.htm\">Hive\u5206\u6790\u7a97\u53e3\u51fd\u6570(\u4e8c) NTILE,ROW_NUMBER,RANK,DENSE_RANK<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/qq_31573519\/article\/details\/78586205\">HIVE\u4e2d ROW_NUMBER() OVER() \u51fd\u6570<\/a><\/li>\n<li><a href=\"https:\/\/www.jianshu.com\/p\/51599bab0c00\">Hive\u4e2drow_number\u7684\u4f7f\u7528<\/a><\/li>\n<li><a href=\"https:\/\/blackproof.iteye.com\/blog\/2164260\">hive row_number\u5206\u7ec4\u6392\u5e8ftop<\/a><\/li>\n<li><a href=\"https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+WindowingAndAnalytics\">https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+WindowingAndAnalytics<\/a><\/li>\n<li><a href=\"http:\/\/blog.sina.com.cn\/s\/blog_5ceb51480102wabj.html\">Hive row_number()\u51fd\u6570\u7528\u6cd5\u8be6\u89e3\u53ca\u793a\u4f8b<\/a> #\u6307\u5bfc\u5982\u4f55\u63d2\u5165\u6d4b\u8bd5\u6570\u636e\u4ee5\u8fdb\u884c\u540e\u7eed\u529f\u80fd\u7684\u6d4b\u8bd5\u548c\u9a8c\u8bc1<\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/wujin\/p\/6051768.html\">hive \u4e2d\u7a97\u53e3\u51fd\u6570row_number,rank,dense_ran,ntile\u5206\u6790\u51fd\u6570\u7684\u7528\u6cd5<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7b80\u5355\u8bb0\u5f55\u4e00\u4e0b\u524d\u6bb5\u65f6\u95f4\u5b66\u4e60\u5230\u7684Hive\u7a97\u53e3\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u65b9\u4fbf\u540e\u671f\u4f7f\u7528\u53c2\u8003\u3002 \u6b63\u6587\uff1a \u53c2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[1261,1375,1374,1262,1373],"class_list":["post-4389","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-hive","tag-rank","tag-row_number","tag-sql","tag-1373"],"views":6709,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4389"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4389\/revisions"}],"predecessor-version":[{"id":4390,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4389\/revisions\/4390"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}