{"id":5368,"date":"2022-12-03T19:53:55","date_gmt":"2022-12-03T11:53:55","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5368"},"modified":"2022-12-03T19:53:55","modified_gmt":"2022-12-03T11:53:55","slug":"hive-sql%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%9f%90%e4%b8%aa%e8%ae%be%e5%a4%87%e5%b1%9e%e4%ba%8e%e6%96%b0%e8%ae%be%e5%a4%87%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5368.html","title":{"rendered":"Hive SQL\u4e2d\u5982\u4f55\u5224\u65ad\u67d0\u4e2a\u8bbe\u5907\u5c5e\u4e8e\u65b0\u8bbe\u5907\uff1f"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u7b80\u5355\u8bb0\u5f55\u4e00\u4e0b\u6700\u8fd1\u5728\u5de5\u4f5c\u4e2d\u7528\u5230\u7684Hive SQL\u7684\u77e5\u8bc6\u70b9\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5c06\u8868\u4e2d\u7684\u4e00\u884c\uff08\u4f46\u5305\u542b\u591a\u4e2a\u5b57\u6bb5\u7684\uff09\u6570\u636e\u8f6c\u6362\u6210\u591a\u884c\uff08\u6bcf\u884c\u53ea\u6709\u4e00\u4e2a\u5b57\u6bb5\u7684\uff09\u6570\u636e\uff1b<br>\u4f7f\u7528 lateral view explode \u8fdb\u884c\u5207\u5206\uff0c\u5982\u679c\u5b57\u6bb5\u4e2d\u5305\u542b\u7279\u6b8a\u5b57\u7b26\uff0c\u9700\u8981\u63d0\u524d\u7528\u5b57\u7b26\u4e32\u76f8\u5173\u51fd\u6570\u8fdb\u884c\u5904\u7406\u4ee5\u6ee1\u8db3\u8981\u6c42<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>lateral view explode(split(replace(replace(replace(ip_addr, '&#91;', ''), '\\\"', ''),']',''),',')) ip_addr as ip<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>explode \u51fd\u6570\u63a5\u6536\u7684\u53c2\u6570\u7c7b\u578b\u662f array\/map \u7c7b\u578b\u7684<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>explode(ARRAY&lt;T&gt; a)\nexplode(MAP&lt;Tkey,Tvalue&gt; m)<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>collect_set \u51fd\u6570\u751f\u6210\u7684\u8fd4\u56de\u503c\u662f array \u7c7b\u578b\u7684<\/li><li>array_contains \u51fd\u6570\u7528\u4e8e\u5224\u65adarray\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u503c\uff0c\u8fd4\u56de\u7684\u662f boolean \u7c7b\u578b\u7684 true\/false<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>array_contains(Array&lt;T&gt;, value)\nReturns TRUE if the array contains value.<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\"><li>\u5728\u5df2\u6709 group by \u7684\u57fa\u7840\u4e0a\u53ef\u4ee5\u901a\u8fc7 partition by \u6765\u8fdb\u884c\u72ec\u7acb\u7684 group by \uff0c\u53ef\u4ee5\u6309\u9700\u4f7f\u7528<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>select\nusername\n,ip,device_id\n,collect_set(device_id) over (partition by username) device_list\nfrom\n...\nwhere\n...\ngroup by username,ip,device_id\n;<\/code><\/pre>\n\n\n\n<p><strong>\u5728 group by k1,k2,k3 \u91cc\u9762\u7684 collect_set(device) over (partition by k1) device_list \u662f\u72ec\u7acb\u7684\uff0c\u53ef\u4ee5\u7406\u89e3 device_list \u662f\u53ea\u57fa\u4e8e k1 \u7684 group by \uff0c\u4e0d\u53d7\u5916\u5c42\u7684 group by \u5f71\u54cd\uff1b<br>\u5728 group by k1,k2,k3 \u91cc\u9762\u7684 collect_set(device) as device_list2 \u662f\u57fa\u4e8e\u5916\u5c42 k1,k2,k3 \u7684 group by \uff0c\u5982\u679c\u5916\u5c42\u7684 group by \u6761\u4ef6\u53ea\u67091\u4e2a\u4e14\u548c partition by \u7684\u5b57\u6bb5\u76f8\u540c\uff0c\u4e24\u8005\u7684\u7ed3\u679c\u5c31\u662f\u76f8\u540c\u7684\uff0c\u5426\u5219\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u9700\u8981\u6309\u9700\u6839\u636e\u573a\u666f\u6765\u9009\u62e9\u3002<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\"><li>to_json \u51fd\u6570<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># \u901a\u8fc7 to_json \u548c map \u5c06\u591a\u4e2a\u5b57\u6bb5\u62fc\u63a5\u6210\u4e00\u4e2ajson\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u65b9\u4fbf\u4f20\u9012\u7ed9\u7a0b\u5e8f\u505a\u5904\u7406\n\nSELECT\nTO_JSON(\nmap(\n\"k1\", 0,\n\"k2\", \"a string\",\n\"k3\", \"&#91;4, 5, 6]\",\n\"k4\", \"7890\"\n)\n)\n;<\/code><\/pre>\n\n\n\n<p>==<\/p>\n\n\n\n<p><strong>\u5bf9\u4e8e\u5b89\u5168\u6765\u8bf4\uff0c\u5f88\u591a\u4e8b\u4ef6\u4e2d\u7684\u5f02\u5e38\u884c\u4e3a\u5176\u5b9e\u53ef\u4ee5\u7b80\u5355\u5b9a\u4e49\u4e3a\u2014\u2014\u548c\u4e4b\u524d\/\u5176\u5b83\u4eba\u4e0d\u4e00\u6837\uff0c\u56e0\u6b64\u5c31\u53ef\u4ee5\u901a\u8fc7\u628a\u4e4b\u524d\u7684\u6570\u636e\u505a\u4e00\u4e2a\u6c47\u603b\uff0c\u7edf\u8ba1\u51fa\u524d\u6bb5\u65f6\u95f4\u7684\u3010\u5e38\u7528\u8bbe\u5907\u5217\u8868\/\u2026\u2026\u3011\uff0c\u7136\u540e\u518d\u62ff\u5f53\u524d\u7684\u8bb0\u5f55\u548c\u7edf\u8ba1\u51fa\u7684\u5217\u8868\u505a\u4e00\u4e2a\u6bd4\u5bf9\uff0c\u5982\u679c\u5f53\u524d\u7684\u8fd9\u4e2a\u4e0d\u5728\u5e38\u7528\u7684\u8bbe\u5907\u3001\u804c\u573a\u3001\u8ba4\u8bc1\u65b9\u5f0f\u91cc\u9762\uff0c\u5219\u8ba4\u4e3a\u662f\u5f02\u5e38\uff0c\u9700\u8981\u53d1\u51fa\u544a\u8b66\uff0c\u7531\u8fd0\u8425\u4eba\u5458\u8ddf\u8fdb\u786e\u8ba4\uff0c\u5e76\u8fdb\u884c\u8bb0\u5f55\u3002<\/strong><\/p>\n\n\n\n<p>\u65b0\u8bbe\u5907\u3001\u9996\u6b21\u8bbf\u95ee\u57df\u540d\u7b49\u3010\u9996\u6b21\u3011\u7684\u5224\u65ad\u903b\u8f91\u5728\u4e8e\u2014\u2014\u4eca\u5929(dt)\u4f7f\u7528\u7684\u8bbe\u5907\u3001\u8bbf\u95ee\u7684\u57df\u540d\uff0c\u6ca1\u6709\u5728\u6628\u5929\u53ca\u4ee5\u524d\u7684n\u5929(dt-n,dt-1)\u4e2d\u51fa\u73b0\u8fc7\u3002\u53ef\u4ee5<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5c06\u4eca\u5929\u4f7f\u7528\u7684\u8bbe\u5907\u3001\u8bbf\u95ee\u7684\u57df\u540d\u6309\u81ea\u5df1\u7684\u805a\u5408\u9700\u6c42\u505a\u4e00\u4e2a group by \u53bb\u91cd\u548c\u7edf\u8ba1<\/li><li>\u7528\u524d\u6bb5\u65f6\u95f4\u7684\u6570\u636e\u7528\u57fa\u4e8e group by \u7684 collect_set \u751f\u6210\u4e00\u4e2a\u5386\u53f2\u8bbe\u5907\u5217\u8868\u3001\u8bbf\u95ee\u57df\u540d\u5217\u8868<\/li><\/ol>\n\n\n\n<p>\u65b9\u6cd5\u4e00\uff1a3-1. \u7136\u540e\u5c06\u4e0a\u97622\u6b65\u751f\u6210\u7684\u4e34\u65f6\u8868\u57fa\u4e8e username \u505a inner join \uff0c\u7136\u540e\u7528 array_contains \u5224\u65ad\u4eca\u5929\u4f7f\u7528\u7684\u8bbe\u5907\u3001\u8bbf\u95ee\u7684\u57df\u540d\u5728\u4e0d\u5728\u4e0a\u4e00\u6b65\u751f\u6210\u7684\u5217\u8868\u91cc\u9762\uff0c\u5982\u679c\u4e0d\u5728\u7684\u8bdd\uff0c\u8bf4\u660e\u8bbe\u5907\u662f\u65b0\u8bbe\u5907\u3001\u57df\u540d\u662f\u8fd1\u671f\u9996\u6b21\u8bbf\u95ee\u7684\u57df\u540d\uff0c\u53ef\u4ee5\u53d1\u51fa\u63d0\u9192\uff0c\u7136\u540e\u7531\u8fd0\u8425\u540c\u5b66\u8fdb\u884c\u8ddf\u8fdb\u786e\u8ba4\u3002<\/p>\n\n\n\n<p>\u65b9\u6cd5\u4e8c\uff1a3-2. \u4e5f\u53ef\u4ee5\u57fa\u4e8e username,device \u505a left outer join \u5f53 x2.username is not NULL and x2.device is NULL \u65f6\uff0c\u5219\u53ef\u8ba4\u4e3a\u662f\u8be5\u7528\u6237\u7684\u65b0\u8bbe\u5907\uff0c\u540e\u7eed\u5904\u7406\u6b65\u9aa4\u548c\u4e0a\u9762\u5199\u7684\u4e00\u6837\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u65b9\u6cd5\u4e00\nselect\nx1.*, x2.*\nfrom\n(select\nusername, ip, device, domain\n,count(1) as cnt\nfrom\naccesslog\nwhere\ndt = 'today'\ngroup by username, ip, device, domain\n)x1\njoin\n(select\nusername\n,collect_set(device) over (partition by username) device_list\n,collect_set(device) as device_list2\n,collect_set(domain) over (partition by username) domain_list\n,count(1) as cnt\nfrom\naccesslog\nwhere\ndt between 'today-30' and 'today-1'\ngroup by username\n)x2\non x1.username = x2.username\nwhere\n! array_contains(x2.device_list, x1.device)\n;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># \u65b9\u6cd5\u4e8c\nselect\nx1.*, x2.*\nfrom\n(select\nusername, ip, device, domain\n,count(1) as cnt\nfrom\naccesslog\nwhere\ndt = 'today'\ngroup by username, ip, device, domain\n)x1\nleft outer join\n(select\nusername, device\nfrom\naccesslog\nwhere\ndt between 'today-30' and 'today-1'\ngroup by username, device\n)x2\non x1.username = x2.username and x1.device = x2.device\nwhere\nx2.username is not NULL and x2.device is NULL\n;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>LanguageManual UDF<br><a href=\"https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+UDF#LanguageManualUDF-StringFunctions\">https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+UDF#LanguageManualUDF-StringFunctions<\/a><\/p>\n\n\n\n<p>Hive&#8211;map\/string\u2192json&amp;\u5904\u7406Json\u6570\u636e\u7684\u51fd\u6570<br><a href=\"https:\/\/blog.csdn.net\/qq_46893497\/article\/details\/109959763\">https:\/\/blog.csdn.net\/qq_46893497\/article\/details\/109959763<\/a><\/p>\n\n\n\n<p>hive\u67e5\u8be2\u7ed3\u679c\u8f6c\u4e3ajson\u683c\u5f0f<br><a href=\"https:\/\/blog.csdn.net\/weixin_40873462\/article\/details\/98175571\">https:\/\/blog.csdn.net\/weixin_40873462\/article\/details\/98175571<\/a><\/p>\n\n\n\n<p>hive \u5c06hive\u8868\u6570\u636e\u67e5\u8be2\u51fa\u6765\u8f6c\u4e3ajson\u5bf9\u8c61\u548cjson\u6570\u7ec4\u8f93\u51fa<br><a href=\"https:\/\/www.cnblogs.com\/yfb918\/p\/11325969.html\">https:\/\/www.cnblogs.com\/yfb918\/p\/11325969.html<\/a><\/p>\n\n\n\n<p>Spark SQL\u548cHive\u4e2d\u7684\u51fd\u6570\uff08\u4e8c\uff09\uff1aJSON\u51fd\u6570<br><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/343758768\">https:\/\/zhuanlan.zhihu.com\/p\/343758768<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7b80\u5355\u8bb0\u5f55\u4e00\u4e0b\u6700\u8fd1\u5728\u5de5\u4f5c\u4e2d\u7528\u5230\u7684Hive SQL\u7684\u77e5\u8bc6\u70b9\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002 \u6b63\u6587\uff1a \u53c2\u8003\u89e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,23],"tags":[1886,1888,1281,1261,1262,1887],"class_list":["post-5368","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","tag-array_contains","tag-collect_set","tag-explode","tag-hive","tag-sql","tag-to_json"],"views":2550,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5368","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=5368"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5368\/revisions"}],"predecessor-version":[{"id":5369,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5368\/revisions\/5369"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}