{"id":4243,"date":"2019-01-12T08:48:49","date_gmt":"2019-01-12T00:48:49","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4243"},"modified":"2019-01-12T08:48:49","modified_gmt":"2019-01-12T00:48:49","slug":"hive-sql%e5%ad%a6%e4%b9%a0%e6%95%b4%e7%90%86_2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4243.html","title":{"rendered":"Hive SQL\u5b66\u4e60\u6574\u7406_2"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u7ee7\u7eed\u6574\u7406\u4e00\u4e0b\u5e38\u89c1Hive SQL\u7684\u7528\u6cd5\uff0c\u65b9\u4fbf\u8981\u7528\u7684\u65f6\u5019\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>1\u3001explode\u51fd\u6570\u7684\u7b80\u5355\u4f7f\u7528<\/h6>\n<p>\u603b\u7ed3\u8d77\u6765\u4e00\u53e5\u8bdd\uff1a<strong><span style=\"color: #ff0000;\">explode\u5c31\u662f\u5c06Hive\u4e00\u884c\u4e2d\u590d\u6742\u7684array\u6216\u8005map\u7ed3\u6784\u62c6\u5206\u6210\u591a\u884c\u3002<\/span><\/strong><\/p>\n<pre class=\"lang:default decode:true\">select explode(array('A','B','C'));\r\nselect explode(array('A','B','C')) as col;\r\n\/*\r\nA\r\nB\r\nC\r\n*\/\r\n\r\nselect explode(map('A',10,'B',20,'C',30));\r\nselect explode(map('A',10,'B',20,'C',30)) as (key,value);\r\n\/*\r\nkey\tvalue\r\nA\t10\r\nB\t20\r\nC\t30\r\n*\/\r\n<\/pre>\n<h6><span style=\"color: #ff0000;\">2\u3001lateral view\u4f7f\u7528\u65f6\u7684\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879<\/span><\/h6>\n<pre class=\"lang:default decode:true \">select tf.* from (select 0) t lateral view explode(array('A','B','C')) tf;\r\n\/*\r\nsql\u8bed\u6cd5\u89e3\u6790\u9519\u8bef, SQL\u5728 '('A','B','C')) tf' \u7684\u9644\u8fd1\u6709\u8bed\u6cd5\u9519\u8bef\uff0c\u51fa\u9519\u4f4d\u7f6e: \u7b2c1\u884c, \u7b2c74\u5217\r\n*\/\r\n\r\nselect tf.* from (select 0) t lateral view explode(array('A','B','C')) tf as col; -- \u8fd9\u91cc\u7684 tf as col \u5fc5\u4e0d\u53ef\u5c11\r\n\/*\r\nA\r\nB\r\nC\r\n*\/\r\n\r\nSELECT myCol1, myCol2 FROM (select 0)\r\nLATERAL VIEW explode(array('A','B','C')) myTable1 AS myCol1\r\nLATERAL VIEW explode(array('x','y','z')) myTable2 AS myCol2\r\n\/*\r\nFAILED: ParseException line 2:8 missing EOF at 'VIEW' near 'LATERAL'\r\n*\/\r\n\r\nSELECT myCol1, myCol2 FROM (select 0) t\r\nLATERAL VIEW explode(array('A','B','C')) myTable1 AS myCol1\r\nLATERAL VIEW explode(array('x','y','z')) myTable2 AS myCol2\r\n\/*\r\nA\tx\r\nA\ty\r\nA\tz\r\nB\tx\r\nB\ty\r\nB\tz\r\nC\tx\r\nC\ty\r\nC\tz\r\n\r\nLateral View\u901a\u5e38\u548cUDTF\u4e00\u8d77\u51fa\u73b0\uff0c\u4e3a\u4e86\u89e3\u51b3UDTF\u4e0d\u5141\u8bb8\u5728select\u5b57\u6bb5\u7684\u95ee\u9898\u3002\r\nMultiple Lateral View\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7b1b\u5361\u5c14\u4e58\u79ef\u3002\r\nOuter\u5173\u952e\u5b57\u53ef\u4ee5\u628a\u4e0d\u8f93\u51fa\u7684UDTF\u7684\u7a7a\u7ed3\u679c\uff0c\u8f93\u51fa\u6210NULL\uff0c\u9632\u6b62\u4e22\u5931\u6570\u636e\u3002\r\n*\/\r\n\r\nselect tf.* from (select 0) t lateral view explode(map('A',10,'B',20,'C',30)) tf as key,value;\r\n\/*\r\ntf.key\ttf.value\r\nA\t10\r\nB\t20\r\nC\t30\r\n*\/\r\n\r\n-- lateral view\u4e0eexplode\u7b49udtf\u5c31\u662f\u5929\u751f\u597d\u642d\u6863\uff0cexplode\u5c06\u590d\u6742\u7ed3\u6784\u4e00\u884c\u62c6\u6210\u591a\u884c\uff0c\u7136\u540e\u518d\u7528lateral view\u505a\u5404\u79cd\u805a\u5408\u3002\r\n<\/pre>\n<h6>3\u3001\u5b57\u7b26\u4e32\u622a\u53d6<\/h6>\n<pre class=\"lang:default decode:true \">select substr('abcde',3,2) -- 'cd'\r\nselect substr('abcde',0,2) -- 'ab'\r\n\r\n-- \u7b2c3\u4e2a\u53c2\u6570\u6307\u7684\u662f length \u800c\u4e0d\u662f\u7ed3\u5c3e\u5b57\u7b26\u7684 index\r\nselect substr('abcde',0,-2) -- ''\r\nselect substring('abcde',0,-2) -- ''\r\n<\/pre>\n<h6>4\u3001json\u5b57\u7b26\u4e32\u89e3\u6790\u3001\u63d0\u53d6<\/h6>\n<p>\u53c2\u89c1\u4e4b\u524d\u8bb0\u5f55\u7684\u300c<a href=\"https:\/\/ixyzero.com\/blog\/archives\/4208.html\" target=\"_blank\" rel=\"noopener\">Hive\u4e2d\u5982\u4f55\u52a0\u8f7d\u548c\u89e3\u6790(\u5d4c\u5957)json\u6570\u636e<\/a>\u300d\u3002<\/p>\n<h6><span style=\"color: #ff0000;\">5\u3001\u5185\u7f6e\u7684\u805a\u5408\u51fd\u6570<\/span><\/h6>\n<table class=\"confluenceTable tablesorter tablesorter-default stickyTableHeaders\" role=\"grid\">\n<tbody aria-live=\"polite\" aria-relevant=\"all\">\n<tr role=\"row\">\n<td class=\"confluenceTd\"><strong>\u8fd4\u56de\u7c7b\u578b<\/strong><\/td>\n<td class=\"confluenceTd\"><strong>\u805a\u5408\u51fd\u6570\u540d\u79f0<\/strong><\/td>\n<td class=\"confluenceTd\"><strong>\u63cf\u8ff0\u8bf4\u660e<\/strong><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">DOUBLE<\/td>\n<td class=\"confluenceTd\">sum(col), sum(DISTINCT col)<\/td>\n<td class=\"confluenceTd\">\u6c42\u5217\u7684\u548c (returns the sum of the elements in the group or the sum of the distinct values of the column in the group)<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">DOUBLE<\/td>\n<td class=\"confluenceTd\">avg(col), avg(DISTINCT col)<\/td>\n<td class=\"confluenceTd\">\u6c42\u5217\u7684\u5e73\u5747\u6570(returns the average of the elements in the group or the average of the distinct values of the column in the group)<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">DOUBLE<\/td>\n<td class=\"confluenceTd\">min(col)<\/td>\n<td class=\"confluenceTd\">\u6c42\u5217\u7684\u6700\u5c0f\u503c(returns the minimum value of the column in the group)<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">DOUBLE<\/td>\n<td class=\"confluenceTd\">max(col)<\/td>\n<td class=\"confluenceTd\">\u6c42\u5217\u7684\u6700\u5927\u503c(returns the maximum value of the column in the group)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\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=\"https:\/\/stackoverflow.com\/questions\/28716165\/how-to-query-struct-array-with-hive-get-json-object\">Hive\u4e2d explode \u51fd\u6570\u7684\u4f7f\u7528<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/bitcarmanlee\/article\/details\/51926530\">hive lateral view \u4e0e explode\u8be6\u89e3<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/oopsoom\/article\/details\/26001307\">Lateral View\u7528\u6cd5 \u4e0e Hive UDTF explode<\/a><\/li>\n<li><a href=\"https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+UDF#LanguageManualUDF-CollectionFunctions\">https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+UDF#LanguageManualUDF-CollectionFunctions<\/a><\/li>\n<li><a href=\"https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+UDF#LanguageManualUDF-explode(array)\">https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+UDF#LanguageManualUDF-explode(array)<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/sl1992\/article\/details\/53894481\">Hive\u590d\u5408\u6570\u636e\u7c7b\u578barray,map,struct\u7684\u4f7f\u7528<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/u010670689\/article\/details\/72885944\">hive\u590d\u6742\u683c\u5f0farray,map,struct\u4f7f\u7528<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/yfkiss\/article\/details\/7842014\">hive array\u3001map\u3001struct\u4f7f\u7528<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/Gamer_gyt\/article\/details\/52169441\">\u5173\u4e8eHive\u4e2d\u7684\u590d\u6742\u6570\u636e\u7c7b\u578bArray,Map,Structs\u7684\u4e00\u4e9b\u4f7f\u7528\u6848\u4f8b<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7ee7\u7eed\u6574\u7406\u4e00\u4e0b\u5e38\u89c1Hive SQL\u7684\u7528\u6cd5\uff0c\u65b9\u4fbf\u8981\u7528\u7684\u65f6\u5019\u53c2\u8003\u3002 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a 1\u3001 [&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,12],"tags":[1281,1261,1282,1262,1283],"class_list":["post-4243","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","category-tools","tag-explode","tag-hive","tag-lateral-view","tag-sql","tag-udtf"],"views":11741,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4243","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=4243"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4243\/revisions"}],"predecessor-version":[{"id":4247,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4243\/revisions\/4247"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}