{"id":1329,"date":"2022-03-06T11:45:59","date_gmt":"2022-03-06T03:45:59","guid":{"rendered":"http:\/\/www.eait.co\/?p=1329"},"modified":"2022-06-13T10:17:24","modified_gmt":"2022-06-13T02:17:24","slug":"hive%e5%af%bc%e5%85%a5%e5%af%bc%e5%87%ba-%e5%a2%9e%e5%88%a0%e6%94%b9%e6%9f%a5-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/notes.coremix.net\/?p=1329","title":{"rendered":"Hive\u5bfc\u5165\u5bfc\u51fa \u589e\u5220\u6539\u67e5 + \u51fd\u6570"},"content":{"rendered":"<h2>5.1 \u6570\u636e\u5bfc\u5165<\/h2>\n<h3>5.1.1 \u5411\u8868\u4e2d\u88c5\u8f7d\u6570\u636e\uff08Load\uff09<\/h3>\n<p>1\uff09\u8bed\u6cd5<\/p>\n<p>hive&gt;load data [local] inpath &#8216;\/opt\/module\/datas\/student.txt&#8217; [overwrite] into table student [partition (partcol1=val1,\u2026)];<\/p>\n<p>\uff081\uff09load data:\u8868\u793a\u52a0\u8f7d\u6570\u636e<\/p>\n<p>\uff082\uff09local:\u8868\u793a\u4ece\u672c\u5730\u52a0\u8f7d\u6570\u636e\u5230hive\u8868\uff1b\u5426\u5219\u4eceHDFS\u52a0\u8f7d\u6570\u636e\u5230hive\u8868<\/p>\n<p>\uff083\uff09inpath:\u8868\u793a\u52a0\u8f7d\u6570\u636e\u7684\u8def\u5f84<\/p>\n<p>\uff084\uff09overwrite:\u8868\u793a\u8986\u76d6\u8868\u4e2d\u5df2\u6709\u6570\u636e\uff0c\u5426\u5219\u8868\u793a\u8ffd\u52a0<\/p>\n<p>\uff085\uff09into table:\u8868\u793a\u52a0\u8f7d\u5230\u54ea\u5f20\u8868<\/p>\n<p>\uff086\uff09student:\u8868\u793a\u5177\u4f53\u7684\u8868\u540d<\/p>\n<p>\uff087\uff09partition:\u8868\u793a\u4e0a\u4f20\u5230\u6307\u5b9a\u5206\u533a<\/p>\n<p>2\uff09\u5b9e\u64cd\u6848\u4f8b<\/p>\n<p>\uff080\uff09\u521b\u5efa\u4e00\u5f20\u8868<\/p>\n<p>hive (default)&gt; create table student(id string, name string) row format delimited fields terminated by &#8216;\\t&#8217;;<\/p>\n<p>\uff081\uff09\u52a0\u8f7d\u672c\u5730\u6587\u4ef6\u5230hive<\/p>\n<p>hive (default)&gt; load data local inpath &#8216;\/opt\/module\/datas\/student.txt&#8217; into table default.student;<\/p>\n<p>\uff082\uff09\u52a0\u8f7dHDFS\u6587\u4ef6\u5230hive\u4e2d<\/p>\n<p>\u4e0a\u4f20\u6587\u4ef6\u5230HDFS<\/p>\n<p>hive (default)&gt; dfs -put \/opt\/module\/datas\/student.txt \/user\/data\/hive;<\/p>\n<p>\u52a0\u8f7dHDFS\u4e0a\u6570\u636e<\/p>\n<p>hive (default)&gt;<span style=\"color: #0000ff;\">load data inpath<\/span> &#8216;\/user\/data\/hive\/student.txt&#8217; into table default.student;<\/p>\n<p>\uff083\uff09\u52a0\u8f7d\u6570\u636e\u8986\u76d6\u8868\u4e2d\u5df2\u6709\u7684\u6570\u636e<\/p>\n<p>\u4e0a\u4f20\u6587\u4ef6\u5230HDFS<\/p>\n<p>hive (default)&gt; dfs -put \/opt\/module\/datas\/student.txt \/user\/data\/hive;<\/p>\n<p>\u52a0\u8f7d\u6570\u636e\u8986\u76d6\u8868\u4e2d\u5df2\u6709\u7684\u6570\u636e<\/p>\n<p>hive (default)&gt;load data inpath &#8216;\/user\/data\/hive\/student.txt&#8217; <span style=\"color: #ff0000;\">overwrite <\/span>into table default.student;<\/p>\n<p>\u6ce8\uff1aload hdfs\u7684\u6570\u636e\u76f8\u5f53\u4e8emv\u6587\u4ef6\u5230\u53e6\u4e00\u4e2a\u76ee\u5f55\u4e2d\uff0c\u539f\u76ee\u5f55\u6587\u4ef6\u6d88\u5931<\/p>\n<h3>5.1.2 \u901a\u8fc7\u67e5\u8be2\u8bed\u53e5\u5411\u8868\u4e2d\u63d2\u5165\u6570\u636e\uff08Insert\uff09<\/h3>\n<p>1\uff09\u521b\u5efa\u4e00\u5f20\u5206\u533a\u8868<\/p>\n<p>hive (default)&gt; create table student(id int, name string) <span style=\"color: #ff0000;\">partitioned by (month string)<\/span> row format delimited fields terminated by &#8216;\\t&#8217;;<\/p>\n<p>2\uff09\u57fa\u672c\u63d2\u5165\u6570\u636e<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"519\">hive (default)&gt; insert into table\u00a0 student partition(month=&#8217;201809&#8242;) values(1,&#8217;wangwu&#8217;);<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>3\uff09\u57fa\u672c\u6a21\u5f0f\u63d2\u5165\uff08\u6839\u636e\u5355\u5f20\u8868\u67e5\u8be2\u7ed3\u679c\uff09<\/p>\n<p>hive (default)&gt; insert <span style=\"color: #ff0000;\">overwrite<\/span> table student partition(month=&#8217;201808&#8242;)<\/p>\n<p>select id, name from student where month=&#8217;201809&#8242;;<\/p>\n<p>4\uff09\u591a\u63d2\u5165\u6a21\u5f0f\uff08\u6839\u636e\u591a\u5f20\u8868\u67e5\u8be2\u7ed3\u679c\uff09<\/p>\n<p>hive (default)&gt; from student<\/p>\n<p><span style=\"color: #ff0000;\">insert<\/span> overwrite table student partition(month=&#8217;201807&#8242;)<\/p>\n<p>select id, name where month=&#8217;201809&#8242;<\/p>\n<p><span style=\"color: #ff0000;\">insert<\/span> overwrite table student partition(month=&#8217;201806&#8242;)<\/p>\n<p>select id, name where month=&#8217;201809&#8242;;<\/p>\n<h3>5.1.3 \u67e5\u8be2\u8bed\u53e5\u4e2d\u521b\u5efa\u8868\u5e76\u52a0\u8f7d\u6570\u636e\uff08As Select\uff09<\/h3>\n<p>\u6839\u636e\u67e5\u8be2\u7ed3\u679c\u521b\u5efa\u8868\uff08\u67e5\u8be2\u7684\u7ed3\u679c\u4f1a\u6dfb\u52a0\u5230\u65b0\u521b\u5efa\u7684\u8868\u4e2d\uff09<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"519\">create table if not exists student3<\/p>\n<p><span style=\"color: #ff0000;\">as select<\/span> id, name from student;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>5.1.4 \u521b\u5efa\u8868\u65f6\u901a\u8fc7Location\u6307\u5b9a\u52a0\u8f7d\u6570\u636e\u8def\u5f84<\/h3>\n<p>1\uff09\u521b\u5efa\u8868\uff0c\u5e76\u6307\u5b9a\u5728hdfs\u4e0a\u7684\u4f4d\u7f6e<\/p>\n<p>hive (default)&gt; create table if not exists student5(<\/p>\n<p>id int, name string<\/p>\n<p>)<\/p>\n<p>row format delimited fields terminated by &#8216;\\t&#8217;<\/p>\n<p><span style=\"color: #ff0000;\">location<\/span> &#8216;\/user\/hive\/warehouse\/student5&#8217;;<\/p>\n<p>2\uff09\u4e0a\u4f20\u6570\u636e\u5230hdfs\u4e0a<\/p>\n<p>hive (default)&gt; dfs -put \/opt\/module\/datas\/student.txt\u00a0 \/user\/hive\/warehouse\/student5;<\/p>\n<p>3\uff09\u67e5\u8be2\u6570\u636e<\/p>\n<p>hive (default)&gt; select * from student5;<\/p>\n<h3>5.1.5 Import\u6570\u636e\u5230\u6307\u5b9aHive\u8868\u4e2d<\/h3>\n<p>\u6ce8\u610f\uff1a\u5148\u7528export\u5bfc\u51fa\u540e\uff0c\u518d\u5c06\u6570\u636e\u5bfc\u5165\u3002\u540c\u5728HDFS\u4e0a\u662fCopy\u7ea7\u64cd\u4f5c<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"553\">hive (default)&gt; <span style=\"color: #ff0000;\">export<\/span> table default.student to &#8216;\/user\/hive\/warehouse\/export\/student&#8217;;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<tbody>\n<tr>\n<td width=\"554\">hive (default)&gt; <span style=\"color: #ff0000;\">import<\/span> table student2 partition(month=&#8217;201809&#8242;) from &#8216;\/user\/hive\/warehouse\/export\/student&#8217;;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>5.2 \u6570\u636e\u5bfc\u51fa<\/h2>\n<h3>5.2.1 Insert\u5bfc\u51fa<\/h3>\n<p>1\uff09\u5c06\u67e5\u8be2\u7684\u7ed3\u679c\u5bfc\u51fa\u5230\u672c\u5730,\u6570\u636e\u4e4b\u95f4\u65e0\u95f4\u9694<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"548\">hive (default)&gt;<span style=\"color: #ff0000;\"> insert overwrite local directory<\/span> &#8216;\/opt\/module\/datas\/export\/student&#8217;<\/p>\n<p>select * from student;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>2\uff09\u5c06\u67e5\u8be2\u7684\u7ed3\u679c\u683c\u5f0f\u5316\u5bfc\u51fa\u5230\u672c\u5730,\u6570\u636e\u4e4b\u95f4&#8221;\\t&#8221;\u95f4\u9694<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"548\">hive (default)&gt; insert overwrite local directory &#8216;\/opt\/module\/datas\/export\/student1&#8217;<\/p>\n<p>ROW FORMAT DELIMITED FIELDS TERMINATED BY &#8216;\\t&#8217;\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0select * from student;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>3\uff09\u5c06\u67e5\u8be2\u7684\u7ed3\u679c\u5bfc\u51fa\u5230HDFS\u4e0a(\u6ca1\u6709local)<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"548\">hive (default)&gt;<span style=\"color: #ff0000;\"> insert overwrite directory &#8216;\/user\/data\/student2&#8217;<\/span><\/p>\n<p>ROW FORMAT DELIMITED FIELDS TERMINATED BY &#8216;\\t&#8217;<\/p>\n<p>select * from student;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6ce8\uff1a\u867d\u7136\u540c\u662fHDFS\uff0c\u4f46\u4e0d\u662fcopy\u64cd\u4f5c<\/p>\n<h3>5.2.2 Hadoop\u547d\u4ee4\u5bfc\u51fa\u5230\u672c\u5730<\/h3>\n<table>\n<tbody>\n<tr>\n<td width=\"548\">hive (default)&gt; <span style=\"color: #ff0000;\">dfs -get<\/span> \/user\/hive\/warehouse\/student\/month=201809\/000000_0\u00a0 \/opt\/module\/datas\/export\/student3.txt;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>5.2.3 Hive Shell \u547d\u4ee4\u5bfc\u51fa<\/h3>\n<p>\u57fa\u672c\u8bed\u6cd5\uff1a\uff08hive -f\/-e \u6267\u884c\u8bed\u53e5\u6216\u8005\u811a\u672c &gt; file\uff09<\/p>\n<table style=\"height: 57px;\">\n<tbody>\n<tr style=\"height: 57px;\">\n<td style=\"height: 57px; width: 548px;\"><span style=\"color: #ff0000;\">\u00a0bin\/hive<\/span> -e &#8216;select * from default.student;&#8217;<span style=\"color: #ff0000;\"> &gt;<\/span> \/opt\/module\/datas\/export\/student4.txt;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>5.2.4 Export\u5bfc\u51fa\u5230HDFS\u4e0a<\/h3>\n<table>\n<tbody>\n<tr>\n<td width=\"548\">hive (default)&gt; export table default.student to &#8216;\/user\/hive\/warehouse\/export\/student&#8217;;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>5.3 \u6e05\u9664\u8868\u4e2d\u6570\u636e\uff08Truncate\uff09<\/h2>\n<p>\u6ce8\u610f\uff1aTruncate\u53ea\u80fd\u5220\u9664\u7ba1\u7406\u8868\uff0c\u4e0d\u80fd\u5220\u9664\u5916\u90e8\u8868\u4e2d\u6570\u636e<\/p>\n<p>hive (default)&gt; truncate <span style=\"color: #ff0000;\">table<\/span> student;<\/p>\n<h1>\u516d \u67e5\u8be2<\/h1>\n<p><a href=\"https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+Select\">https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+Select<\/a><\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"548\">[WITH CommonTableExpression (, CommonTableExpression)*]\u00a0\u00a0\u00a0 (Note: Only available starting with Hive\u00a00.13.0)<\/p>\n<p>SELECT [ALL | DISTINCT] select_expr, elect_expr, &#8230;<\/p>\n<p>FROM table_reference<\/p>\n<p>[WHERE where_condition]<\/p>\n<p>[GROUP BY col_list]<\/p>\n<p>[ORDER BY col_list]<\/p>\n<p><span style=\"color: #ff0000;\">\u00a0\u00a0[CLUSTER BY col_list<\/span><\/p>\n<p><span style=\"color: #ff0000;\">\u00a0\u00a0\u00a0\u00a0| [DISTRIBUTE BY col_list] [SORT BY col_list]<\/span><\/p>\n<p>]<\/p>\n<p>[LIMIT number]<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>6.1 \u57fa\u672c\u67e5\u8be2\uff08Select\u2026From\uff09<\/h2>\n<h3>6.1.1 \u5168\u8868\u548c\u7279\u5b9a\u5217\u67e5\u8be2<\/h3>\n<p>1\uff09\u5168\u8868\u67e5\u8be2<\/p>\n<p>hive (default)&gt; select * from emp;<\/p>\n<p>2\uff09\u9009\u62e9\u7279\u5b9a\u5217\u67e5\u8be2<\/p>\n<p>hive (default)&gt; select empno, ename from emp;<\/p>\n<p>\u6ce8\u610f\uff08\u7c7b\u4f3cSQL\uff09\uff1a<\/p>\n<p>\uff081\uff09SQL \u8bed\u8a00\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u3002<\/p>\n<p>\uff082\uff09SQL \u53ef\u4ee5\u5199\u5728\u4e00\u884c\u6216\u8005\u591a\u884c<\/p>\n<p>\uff083\uff09\u5173\u952e\u5b57\u4e0d\u80fd\u88ab\u7f29\u5199\u4e5f\u4e0d\u80fd\u5206\u884c<\/p>\n<p>\uff084\uff09\u5404\u5b50\u53e5\u4e00\u822c\u8981\u5206\u884c\u5199\u3002<\/p>\n<p>\uff085\uff09\u4f7f\u7528\u7f29\u8fdb\u63d0\u9ad8\u8bed\u53e5\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<h3>6.1.2 \u5217\u522b\u540d<\/h3>\n<p>1\uff09\u91cd\u547d\u540d\u4e00\u4e2a\u5217\u3002<\/p>\n<p>2\uff09\u4fbf\u4e8e\u8ba1\u7b97\u3002<\/p>\n<p>3\uff09\u7d27\u8ddf\u5217\u540d\uff0c\u4e5f\u53ef\u4ee5\u5728\u5217\u540d\u548c\u522b\u540d\u4e4b\u95f4\u52a0\u5165\u5173\u952e\u5b57\u2018AS\u2019<\/p>\n<p>4\uff09\u6848\u4f8b\u5b9e\u64cd<\/p>\n<p>\uff081\uff09\u67e5\u8be2\u540d\u79f0\u548c\u90e8\u95e8<\/p>\n<p>hive (default)&gt; select ename AS name, deptno dn from emp;<\/p>\n<h3>6.1.3 \u7b97\u672f\u8fd0\u7b97\u7b26<\/h3>\n<table>\n<tbody>\n<tr>\n<td width=\"179\">\u8fd0\u7b97\u7b26<\/td>\n<td width=\"368\">\u63cf\u8ff0<\/td>\n<\/tr>\n<tr>\n<td width=\"179\">A+B<\/td>\n<td width=\"368\">A\u548cB \u76f8\u52a0<\/td>\n<\/tr>\n<tr>\n<td width=\"179\">A-B<\/td>\n<td width=\"368\">A\u51cf\u53bbB<\/td>\n<\/tr>\n<tr>\n<td width=\"179\">A*B<\/td>\n<td width=\"368\">A\u548cB\u76f8\u4e58<\/td>\n<\/tr>\n<tr>\n<td width=\"179\">A\/B<\/td>\n<td width=\"368\">A\u9664\u4ee5B<\/td>\n<\/tr>\n<tr>\n<td width=\"179\">A%B<\/td>\n<td width=\"368\">A\u5bf9B\u53d6\u4f59\/\u6a21<\/td>\n<\/tr>\n<tr>\n<td width=\"179\">A&amp;B<\/td>\n<td width=\"368\">A\u548cB\u6309\u4f4d\u53d6\u4e0e<\/td>\n<\/tr>\n<tr>\n<td width=\"179\">A|B<\/td>\n<td width=\"368\">A\u548cB\u6309\u4f4d\u53d6\u6216<\/td>\n<\/tr>\n<tr>\n<td width=\"179\">A^B<\/td>\n<td width=\"368\">A\u548cB\u6309\u4f4d\u53d6\u5f02\u6216<\/td>\n<\/tr>\n<tr>\n<td width=\"179\">~A<\/td>\n<td width=\"368\">A\u6309\u4f4d\u53d6\u53cd<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6848\u4f8b\u5b9e\u64cd<\/p>\n<p>\u67e5\u8be2\u51fa\u6240\u6709\u5458\u5de5\u7684\u85aa\u6c34\u540e\u52a01\u663e\u793a\u3002<\/p>\n<p>hive (default)&gt; select sal +1 from emp;<\/p>\n<h3>6.1.4 \u5e38\u7528\u51fd\u6570<\/h3>\n<p>1\uff09\u6c42\u603b\u884c\u6570\uff08count\uff09<\/p>\n<p>hive (default)&gt; select count(1) cnt from emp;<\/p>\n<p>2\uff09\u6c42\u5de5\u8d44\u7684\u6700\u5927\u503c\uff08max\uff09<\/p>\n<p>hive (default)&gt; select max(sal) max_sal from emp;<\/p>\n<p>3\uff09\u6c42\u5de5\u8d44\u7684\u6700\u5c0f\u503c\uff08min\uff09<\/p>\n<p>hive (default)&gt; select min(sal) min_sal from emp;<\/p>\n<p>4\uff09\u6c42\u5de5\u8d44\u7684\u603b\u548c\uff08sum\uff09<\/p>\n<p>hive (default)&gt; select sum(sal) sum_sal from emp;<\/p>\n<p>5\uff09\u6c42\u5de5\u8d44\u7684\u5e73\u5747\u503c\uff08avg\uff09<\/p>\n<p>hive (default)&gt; select avg(sal) avg_sal from emp;<\/p>\n<h3>6.1.5 Limit\u8bed\u53e5<\/h3>\n<p>\u5178\u578b\u7684\u67e5\u8be2\u4f1a\u8fd4\u56de\u591a\u884c\u6570\u636e\u3002LIMIT\u5b50\u53e5\u7528\u4e8e\u9650\u5236\u8fd4\u56de\u7684\u884c\u6570\u3002<\/p>\n<p>hive (default)&gt; select * from emp limit 5;<\/p>\n<p><span style=\"color: #ff0000;\">\u4e25\u683c\u6a21\u5f0f\u4e0b\u4e00\u5b9a\u8981\u52a0limit\u548cwhere\uff0c\u5927\u6570\u636e\u540c\u65f6\u4e5f\u8981\u52a0limit<\/span><\/p>\n<h2>6.2 Where\u8bed\u53e5<\/h2>\n<p>1\uff09\u4f7f\u7528WHERE\u5b50\u53e5\uff0c\u5c06\u4e0d\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u8fc7\u6ee4\u6389\u3002<\/p>\n<p>2\uff09WHERE\u5b50\u53e5\u7d27\u968fFROM\u5b50\u53e5\u3002<\/p>\n<p>3\uff09\u6848\u4f8b\u5b9e\u64cd<\/p>\n<p>\u67e5\u8be2\u51fa\u85aa\u6c34\u5927\u4e8e1000\u7684\u6240\u6709\u5458\u5de5<\/p>\n<p>hive (default)&gt; select ename , sal from emp where sal &gt; 1000;<\/p>\n<h3>6.2.1 \u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08Between\/In\/ Is Null\uff09<\/h3>\n<p>1\uff09\u4e0b\u9762\u8868\u4e2d\u63cf\u8ff0\u4e86\u8c13\u8bcd\u64cd\u4f5c\u7b26\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u540c\u6837\u53ef\u4ee5\u7528\u4e8eJOIN\u2026ON\u548cHAVING\u8bed\u53e5\u4e2d\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"141\">\u64cd\u4f5c\u7b26<\/td>\n<td width=\"113\">\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b<\/td>\n<td width=\"292\">\u63cf\u8ff0<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">A=B<\/td>\n<td width=\"113\">\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/td>\n<td width=\"292\">\u5982\u679cA\u7b49\u4e8eB\u5219\u8fd4\u56deTRUE\uff0c\u53cd\u4e4b\u8fd4\u56deFALSE<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">A&lt;=&gt;B<\/td>\n<td width=\"113\">\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/td>\n<td width=\"292\">\u5982\u679cA\u548cB\u90fd\u4e3aNULL\uff0c\u5219\u8fd4\u56deTRUE\uff0c\u5176\u4ed6\u7684\u548c\u7b49\u53f7\uff08=\uff09\u64cd\u4f5c\u7b26\u7684\u7ed3\u679c\u4e00\u81f4\uff0c\u5982\u679c\u4efb\u4e00\u4e3aNULL\u5219\u7ed3\u679c\u4e3aNULL<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">A&lt;&gt;B, A!=B<\/td>\n<td width=\"113\">\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/td>\n<td width=\"292\">A\u6216\u8005B\u4e3aNULL\u5219\u8fd4\u56deNULL\uff1b\u5982\u679cA\u4e0d\u7b49\u4e8eB\uff0c\u5219\u8fd4\u56deTRUE\uff0c\u53cd\u4e4b\u8fd4\u56deFALSE<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">A&lt;B<\/td>\n<td width=\"113\">\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/td>\n<td width=\"292\">A\u6216\u8005B\u4e3aNULL\uff0c\u5219\u8fd4\u56deNULL\uff1b\u5982\u679cA\u5c0f\u4e8eB\uff0c\u5219\u8fd4\u56deTRUE\uff0c\u53cd\u4e4b\u8fd4\u56deFALSE<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">A&lt;=B<\/td>\n<td width=\"113\">\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/td>\n<td width=\"292\">A\u6216\u8005B\u4e3aNULL\uff0c\u5219\u8fd4\u56deNULL\uff1b\u5982\u679cA\u5c0f\u4e8e\u7b49\u4e8eB\uff0c\u5219\u8fd4\u56deTRUE\uff0c\u53cd\u4e4b\u8fd4\u56deFALSE<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">A&gt;B<\/td>\n<td width=\"113\">\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/td>\n<td width=\"292\">A\u6216\u8005B\u4e3aNULL\uff0c\u5219\u8fd4\u56deNULL\uff1b\u5982\u679cA\u5927\u4e8eB\uff0c\u5219\u8fd4\u56deTRUE\uff0c\u53cd\u4e4b\u8fd4\u56deFALSE<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">A&gt;=B<\/td>\n<td width=\"113\">\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/td>\n<td width=\"292\">A\u6216\u8005B\u4e3aNULL\uff0c\u5219\u8fd4\u56deNULL\uff1b\u5982\u679cA\u5927\u4e8e\u7b49\u4e8eB\uff0c\u5219\u8fd4\u56deTRUE\uff0c\u53cd\u4e4b\u8fd4\u56deFALSE<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">A [NOT] BETWEEN B AND C<\/td>\n<td width=\"113\">\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/td>\n<td width=\"292\">\u5982\u679cA\uff0cB\u6216\u8005C\u4efb\u4e00\u4e3aNULL\uff0c\u5219\u7ed3\u679c\u4e3aNULL\u3002\u5982\u679cA\u7684\u503c\u5927\u4e8e\u7b49\u4e8eB\u800c\u4e14\u5c0f\u4e8e\u6216\u7b49\u4e8eC\uff0c\u5219\u7ed3\u679c\u4e3aTRUE\uff0c\u53cd\u4e4b\u4e3aFALSE\u3002\u5982\u679c\u4f7f\u7528NOT\u5173\u952e\u5b57\u5219\u53ef\u8fbe\u5230\u76f8\u53cd\u7684\u6548\u679c\u3002<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">A IS NULL<\/td>\n<td width=\"113\">\u6240\u6709\u6570\u636e\u7c7b\u578b<\/td>\n<td width=\"292\">\u5982\u679cA\u7b49\u4e8eNULL\uff0c\u5219\u8fd4\u56deTRUE\uff0c\u53cd\u4e4b\u8fd4\u56deFALSE<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">A IS NOT NULL<\/td>\n<td width=\"113\">\u6240\u6709\u6570\u636e\u7c7b\u578b<\/td>\n<td width=\"292\">\u5982\u679cA\u4e0d\u7b49\u4e8eNULL\uff0c\u5219\u8fd4\u56deTRUE\uff0c\u53cd\u4e4b\u8fd4\u56deFALSE<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">IN(\u6570\u503c1, \u6570\u503c2)<\/td>\n<td width=\"113\">\u6240\u6709\u6570\u636e\u7c7b\u578b<\/td>\n<td width=\"292\">\u4f7f\u7528 IN\u8fd0\u7b97\u663e\u793a\u5217\u8868\u4e2d\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">A [NOT] LIKE B<\/td>\n<td width=\"113\">STRING \u7c7b\u578b<\/td>\n<td width=\"292\">B\u662f\u4e00\u4e2aSQL\u4e0b\u7684\u7b80\u5355\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5982\u679cA\u4e0e\u5176\u5339\u914d\u7684\u8bdd\uff0c\u5219\u8fd4\u56deTRUE\uff1b\u53cd\u4e4b\u8fd4\u56deFALSE\u3002B\u7684\u8868\u8fbe\u5f0f\u8bf4\u660e\u5982\u4e0b\uff1a\u2018x%\u2019\u8868\u793aA\u5fc5\u987b\u4ee5\u5b57\u6bcd\u2018x\u2019\u5f00\u5934\uff0c\u2018%x\u2019\u8868\u793aA\u5fc5\u987b\u4ee5\u5b57\u6bcd\u2019x\u2019\u7ed3\u5c3e\uff0c\u800c\u2018%x%\u2019\u8868\u793aA\u5305\u542b\u6709\u5b57\u6bcd\u2019x\u2019,\u53ef\u4ee5\u4f4d\u4e8e\u5f00\u5934\uff0c\u7ed3\u5c3e\u6216\u8005\u5b57\u7b26\u4e32\u4e2d\u95f4\u3002\u5982\u679c\u4f7f\u7528NOT\u5173\u952e\u5b57\u5219\u53ef\u8fbe\u5230\u76f8\u53cd\u7684\u6548\u679c\u3002<\/td>\n<\/tr>\n<tr>\n<td width=\"141\">A RLIKE B, A REGEXP B<\/td>\n<td width=\"113\">STRING \u7c7b\u578b<\/td>\n<td width=\"292\">B\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5982\u679cA\u4e0e\u5176\u5339\u914d\uff0c\u5219\u8fd4\u56deTRUE\uff1b\u53cd\u4e4b\u8fd4\u56deFALSE\u3002\u5339\u914d\u4f7f\u7528\u7684\u662fJDK\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u63a5\u53e3\u5b9e\u73b0\u7684\uff0c\u56e0\u4e3a\u6b63\u5219\u4e5f\u4f9d\u636e\u5176\u4e2d\u7684\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5fc5\u987b\u548c\u6574\u4e2a\u5b57\u7b26\u4e32A\u76f8\u5339\u914d\uff0c\u800c\u4e0d\u662f\u53ea\u9700\u4e0e\u5176\u5b57\u7b26\u4e32\u5339\u914d\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>2\uff09\u6848\u4f8b\u5b9e\u64cd<\/p>\n<p>\uff081\uff09\u67e5\u8be2\u51fa\u85aa\u6c34\u7b49\u4e8e5000\u7684\u6240\u6709\u5458\u5de5<\/p>\n<p>hive (default)&gt; select * from emp where sal =5000;<\/p>\n<p>\uff082\uff09\u67e5\u8be2\u5de5\u8d44\u5728500\u52301000\u7684\u5458\u5de5\u4fe1\u606f\uff0c\u6ce8\u610fbetween &#8230; and \u662f\u95ed\u533a\u95f4<\/p>\n<p>hive (default)&gt; select * from emp where sal between 500 and 1000;<\/p>\n<p>\uff083\uff09\u67e5\u8be2comm(\u5956\u91d1)\u4e3a\u7a7a\u7684\u6240\u6709\u5458\u5de5\u4fe1\u606f<\/p>\n<p>hive (default)&gt; select * from emp where comm is null;<\/p>\n<p>\uff084\uff09\u67e5\u8be2\u5de5\u8d44\u662f1500\u548c5000\u7684\u5458\u5de5\u4fe1\u606f<\/p>\n<p>hive (default)&gt; select * from emp where sal IN (1500, 5000);<\/p>\n<h3>6.2.2 Like\u548cRLike<\/h3>\n<p>1\uff09\u4f7f\u7528LIKE\u8fd0\u7b97\u9009\u62e9\u7c7b\u4f3c\u7684\u503c<\/p>\n<p>2\uff09\u9009\u62e9\u6761\u4ef6\u53ef\u4ee5\u5305\u542b\u5b57\u7b26\u6216\u6570\u5b57:<\/p>\n<p>% \u4ee3\u8868\u96f6\u4e2a\u6216\u591a\u4e2a\u5b57\u7b26(\u4efb\u610f\u4e2a\u5b57\u7b26)\u3002<\/p>\n<p>_ \u4ee3\u8868\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<p>3\uff09<span style=\"color: #ff0000;\">RLIKE\u5b50\u53e5\u662fHive\u4e2d\u8fd9\u4e2a\u529f\u80fd\u7684\u4e00\u4e2a\u6269\u5c55\uff0c\u5176\u53ef\u4ee5\u901a\u8fc7Java\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd9\u4e2a\u66f4\u5f3a\u5927\u7684\u8bed\u8a00\u6765\u6307\u5b9a\u5339\u914d\u6761\u4ef6\u3002<\/span><\/p>\n<p>4\uff09\u6848\u4f8b\u5b9e\u64cd<\/p>\n<p>\uff081\uff09\u67e5\u627e\u4ee52\u5f00\u5934\u85aa\u6c34\u7684\u5458\u5de5\u4fe1\u606f<\/p>\n<p>hive (default)&gt; select * from emp where sal LIKE &#8216;2%&#8217;;<\/p>\n<p>\uff082\uff09\u67e5\u627e\u7b2c\u4e8c\u4e2a\u6570\u503c\u4e3a2\u7684\u85aa\u6c34\u7684\u5458\u5de5\u4fe1\u606f<\/p>\n<p>hive (default)&gt; select * from emp where sal LIKE &#8216;_2%&#8217;;<\/p>\n<p>\uff083\uff09\u67e5\u627e\u85aa\u6c34\u4e2d\u542b\u67092\u7684\u5458\u5de5\u4fe1\u606f<\/p>\n<p>hive (default)&gt; select * from emp where sal <span style=\"color: #ff0000;\">RLIKE &#8216;[2]&#8217;;<\/span><\/p>\n<h3>6.2.3 \u903b\u8f91\u8fd0\u7b97\u7b26\uff08And\/Or\/Not\uff09<\/h3>\n<table>\n<tbody>\n<tr>\n<td width=\"276\">\u64cd\u4f5c\u7b26<\/td>\n<td width=\"271\">\u542b\u4e49<\/td>\n<\/tr>\n<tr>\n<td width=\"276\">AND<\/td>\n<td width=\"271\">\u903b\u8f91\u5e76<\/td>\n<\/tr>\n<tr>\n<td width=\"276\">OR<\/td>\n<td width=\"271\">\u903b\u8f91\u6216<\/td>\n<\/tr>\n<tr>\n<td width=\"276\">NOT<\/td>\n<td width=\"271\">\u903b\u8f91\u5426<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6848\u4f8b\u5b9e\u64cd<\/p>\n<p>\uff081\uff09\u67e5\u8be2\u85aa\u6c34\u5927\u4e8e1000\uff0c\u90e8\u95e8\u662f30<\/p>\n<p>hive (default)&gt; select * from emp where sal&gt;1000 and deptno=30;<\/p>\n<p>\uff082\uff09\u67e5\u8be2\u85aa\u6c34\u5927\u4e8e1000\uff0c\u6216\u8005\u90e8\u95e8\u662f30<\/p>\n<p>hive (default)&gt; select * from emp where sal&gt;1000 or deptno=30;<\/p>\n<p>\uff083\uff09\u67e5\u8be2\u9664\u4e8620\u90e8\u95e8\u548c30\u90e8\u95e8\u4ee5\u5916\u7684\u5458\u5de5\u4fe1\u606f<\/p>\n<p>hive (default)&gt; select * from emp where deptno not IN(30, 20);<\/p>\n<h2>6.3 \u5206\u7ec4<\/h2>\n<h3>6.3.1 Group By\u8bed\u53e5<\/h3>\n<p>GROUP BY\u8bed\u53e5\u901a\u5e38\u4f1a\u548c\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u6309\u7167\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u5217\u961f\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u5bf9\u6bcf\u4e2a\u7ec4\u6267\u884c\u805a\u5408\u64cd\u4f5c\u3002<\/p>\n<p>\u6848\u4f8b\u5b9e\u64cd\uff1a<\/p>\n<p>\uff081\uff09\u8ba1\u7b97emp\u8868\u6bcf\u4e2a\u90e8\u95e8\u7684\u5e73\u5747\u5de5\u8d44<\/p>\n<p>hive (default)&gt; select t.deptno, avg(t.sal) avg_sal from emp t group by t.deptno;<\/p>\n<p>\uff082\uff09\u8ba1\u7b97emp\u6bcf\u4e2a\u90e8\u95e8\u4e2d\u6bcf\u4e2a\u5c97\u4f4d\u7684\u6700\u9ad8\u85aa\u6c34<\/p>\n<p>hive (default)&gt; select t.deptno, t.job, max(t.sal) max_sal from emp t group by t.deptno, t.job;<\/p>\n<h3>6.3.2 Having\u8bed\u53e5<\/h3>\n<p>1\uff09having\u4e0ewhere\u4e0d\u540c\u70b9<\/p>\n<p>\uff081\uff09where\u9488\u5bf9\u8868\u4e2d\u7684\u5217\u53d1\u6325\u4f5c\u7528\uff0c\u67e5\u8be2\u6570\u636e\uff1bhaving\u9488\u5bf9\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u5217\u53d1\u6325\u4f5c\u7528\uff0c\u7b5b\u9009\u6570\u636e\u3002<\/p>\n<p>\uff082\uff09where\u540e\u9762\u4e0d\u80fd\u5199\u5206\u7ec4\u51fd\u6570\uff0c\u800chaving\u540e\u9762\u53ef\u4ee5\u4f7f\u7528\u5206\u7ec4\u51fd\u6570\u3002<\/p>\n<p>\uff083\uff09having\u53ea\u7528\u4e8egroup by\u5206\u7ec4\u7edf\u8ba1\u8bed\u53e5\u3002<\/p>\n<p>2\uff09\u6848\u4f8b\u5b9e\u64cd\uff1a<\/p>\n<p>\uff081\uff09\u6c42\u6bcf\u4e2a\u90e8\u95e8\u7684\u5e73\u5747\u85aa\u6c34\u5927\u4e8e2000\u7684\u90e8\u95e8<\/p>\n<p>\u6c42\u6bcf\u4e2a\u90e8\u95e8\u7684\u5e73\u5747\u5de5\u8d44<\/p>\n<p>hive (default)&gt; select deptno, avg(sal) from emp group by deptno;<\/p>\n<p>\u6c42\u6bcf\u4e2a\u90e8\u95e8\u7684\u5e73\u5747\u85aa\u6c34\u5927\u4e8e2000\u7684\u90e8\u95e8<\/p>\n<p>hive (default)&gt; select deptno, avg(sal) avg_sal from emp group by deptno having avg_sal &gt; 2000;<\/p>\n<h2>6.4 Join\u8bed\u53e5<\/h2>\n<h3>6.4.1 \u7b49\u503cJoin<\/h3>\n<p>Hive\u652f\u6301\u901a\u5e38\u7684SQL JOIN\u8bed\u53e5\uff0c\u4f46\u662f\u53ea\u652f\u6301\u7b49\u503c\u8fde\u63a5\uff0c\u4e0d\u652f\u6301\u975e\u7b49\u503c\u8fde\u63a5\u3002<\/p>\n<p>\u6848\u4f8b\u5b9e\u64cd<\/p>\n<p>\uff081\uff09\u6839\u636e\u5458\u5de5\u8868\u548c\u90e8\u95e8\u8868\u4e2d\u7684\u90e8\u95e8\u7f16\u53f7\u76f8\u7b49\uff0c\u67e5\u8be2\u5458\u5de5\u7f16\u53f7\u3001\u5458\u5de5\u540d\u79f0\u548c\u90e8\u95e8\u7f16\u53f7\uff1b<\/p>\n<p>hive (default)&gt; select e.empno, e.ename, d.deptno, d.dname from emp e join dept d on e.deptno = d.deptno;<\/p>\n<p>\u540c\u6837\u4e0e<\/p>\n<p>select <a href=\"http:\/\/e.empn\">e.empn<\/a>o,<a href=\"http:\/\/e.enam\">e.enam<\/a>e,<a href=\"http:\/\/d.dept\">d.dept<\/a>no,<a href=\"http:\/\/d.dnam\">d.dnam<\/a>e from emp e,dept d where <a href=\"http:\/\/e.dept\">e.dept<\/a>no=<a href=\"http:\/\/d.dept\">d.dept<\/a>no<\/p>\n<h3>6.4.2 \u8868\u7684\u522b\u540d<\/h3>\n<p>1\uff09\u597d\u5904<\/p>\n<p>\uff081\uff09\u4f7f\u7528\u522b\u540d\u53ef\u4ee5\u7b80\u5316\u67e5\u8be2\u3002<\/p>\n<p>\uff082\uff09\u4f7f\u7528\u8868\u540d\u524d\u7f00\u53ef\u4ee5\u63d0\u9ad8\u6267\u884c\u6548\u7387\u3002<\/p>\n<p>2\uff09\u6848\u4f8b\u5b9e\u64cd<\/p>\n<p>\u5408\u5e76\u5458\u5de5\u8868\u548c\u90e8\u95e8\u8868<\/p>\n<p>hive (default)&gt; select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;<\/p>\n<h3>6.4.3 \u5185\u8fde\u63a5<\/h3>\n<p>\u5185\u8fde\u63a5\uff1a\u53ea\u6709\u8fdb\u884c\u8fde\u63a5\u7684\u4e24\u4e2a\u8868\u4e2d\u90fd\u5b58\u5728\u4e0e\u8fde\u63a5\u6761\u4ef6\u76f8\u5339\u914d\u7684\u6570\u636e\u624d\u4f1a\u88ab\u4fdd\u7559\u4e0b\u6765\u3002<\/p>\n<p>hive (default)&gt; select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;<\/p>\n<h3>6.4.4 \u5de6\u5916\u8fde\u63a5<\/h3>\n<p>\u5de6\u5916\u8fde\u63a5\uff1aJOIN\u64cd\u4f5c\u7b26\u5de6\u8fb9\u8868\u4e2d\u7b26\u5408WHERE\u5b50\u53e5\u7684\u6240\u6709\u8bb0\u5f55\u5c06\u4f1a\u88ab\u8fd4\u56de\u3002<\/p>\n<p>hive (default)&gt; select e.empno, e.ename, d.deptno from emp e left join dept d on e.deptno = d.deptno;<\/p>\n<h3>6.4.5 \u53f3\u5916\u8fde\u63a5<\/h3>\n<p>\u53f3\u5916\u8fde\u63a5\uff1aJOIN\u64cd\u4f5c\u7b26\u53f3\u8fb9\u8868\u4e2d\u7b26\u5408WHERE\u5b50\u53e5\u7684\u6240\u6709\u8bb0\u5f55\u5c06\u4f1a\u88ab\u8fd4\u56de\u3002<\/p>\n<p>hive (default)&gt; select e.empno, e.ename, d.deptno from emp e right join dept d on e.deptno = d.deptno;<\/p>\n<h3>6.4.6 \u6ee1\u5916\u8fde\u63a5<\/h3>\n<p>\u6ee1\u5916\u8fde\u63a5\uff1a\u5c06\u4f1a\u8fd4\u56de\u6240\u6709\u8868\u4e2d\u7b26\u5408WHERE\u8bed\u53e5\u6761\u4ef6\u7684\u6240\u6709\u8bb0\u5f55\u3002\u5982\u679c\u4efb\u4e00\u8868\u7684\u6307\u5b9a\u5b57\u6bb5\u6ca1\u6709\u7b26\u5408\u6761\u4ef6\u7684\u503c\u7684\u8bdd\uff0c\u90a3\u4e48\u5c31\u4f7f\u7528NULL\u503c\u66ff\u4ee3\u3002<\/p>\n<p>hive (default)&gt; select e.empno, e.ename, d.deptno from emp e full join dept d on e.deptno = d.deptno;<\/p>\n<h3>6.4.7 \u591a\u8868\u8fde\u63a5<\/h3>\n<p>\u6ce8\u610f\uff1a\u8fde\u63a5 n\u4e2a\u8868\uff0c\u81f3\u5c11\u9700\u8981n-1\u4e2a\u8fde\u63a5\u6761\u4ef6\u3002\u4f8b\u5982\uff1a\u8fde\u63a5\u4e09\u4e2a\u8868\uff0c\u81f3\u5c11\u9700\u8981\u4e24\u4e2a\u8fde\u63a5\u6761\u4ef6\u3002<\/p>\n<p>0\uff09\u6570\u636e\u51c6\u5907<\/p>\n<p>&nbsp;<\/p>\n<p>1\uff09\u521b\u5efa\u4f4d\u7f6e\u8868<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"491\">create table if not exists default.location(<\/p>\n<p>loc int,<\/p>\n<p>loc_name string<\/p>\n<p>)<\/p>\n<p>row format delimited fields terminated by &#8216;\\t&#8217;;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>2\uff09\u5bfc\u5165\u6570\u636e<\/p>\n<p>hive (default)&gt; load data local inpath &#8216;\/opt\/module\/datas\/location.txt&#8217; into table default.location;<\/p>\n<p>3\uff09\u591a\u8868\u8fde\u63a5\u67e5\u8be2distinct<\/p>\n<p>hive (default)&gt;SELECT e.ename, d.deptno, l. loc_name<\/p>\n<p>FROM\u00a0\u00a0 emp e<\/p>\n<p>JOIN\u00a0\u00a0 dept d<\/p>\n<p>ON\u00a0\u00a0\u00a0\u00a0 d.deptno = e.deptno<\/p>\n<p>JOIN\u00a0\u00a0 location l<\/p>\n<p>ON\u00a0\u00a0\u00a0\u00a0 d.loc = l.loc;<\/p>\n<p>\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cHive\u4f1a\u5bf9\u6bcf\u5bf9JOIN\u8fde\u63a5\u5bf9\u8c61\u542f\u52a8\u4e00\u4e2aMapReduce\u4efb\u52a1\u3002\u672c\u4f8b\u4e2d\u4f1a\u9996\u5148\u542f\u52a8\u4e00\u4e2aMapReduce job\u5bf9\u8868e\u548c\u8868d\u8fdb\u884c\u8fde\u63a5\u64cd\u4f5c\uff0c\u7136\u540e\u4f1a\u518d\u542f\u52a8\u4e00\u4e2aMapReduce job\u5c06\u7b2c\u4e00\u4e2aMapReduce job\u7684\u8f93\u51fa\u548c\u8868l;\u8fdb\u884c\u8fde\u63a5\u64cd\u4f5c\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u4e3a\u4ec0\u4e48\u4e0d\u662f\u8868d\u548c\u8868l\u5148\u8fdb\u884c\u8fde\u63a5\u64cd\u4f5c\u5462\uff1f\u8fd9\u662f\u56e0\u4e3aHive\u603b\u662f\u6309\u7167\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u6267\u884c\u7684\u3002<\/p>\n<h3>6.4.8 \u7b1b\u5361\u5c14\u79ef<\/h3>\n<p>1\uff09\u7b1b\u5361\u5c14\u96c6\u4f1a\u5728\u4e0b\u9762\u6761\u4ef6\u4e0b\u4ea7\u751f:<\/p>\n<p>\uff081\uff09\u7701\u7565\u8fde\u63a5\u6761\u4ef6<\/p>\n<p>\uff082\uff09\u8fde\u63a5\u6761\u4ef6\u65e0\u6548<\/p>\n<p>\uff083\uff09\u6240\u6709\u8868\u4e2d\u7684\u6240\u6709\u884c\u4e92\u76f8\u8fde\u63a5<\/p>\n<p>2\uff09\u6848\u4f8b\u5b9e\u64cd<\/p>\n<p>hive (default)&gt; <span style=\"color: #ff0000;\">select e.empno, d.deptno from emp e, dept d;<\/span><\/p>\n<h3>6.4.9 \u8fde\u63a5\u8c13\u8bcd\u4e2d\u4e0d\u652f\u6301or<\/h3>\n<p>hive (default)&gt; select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno or e.ename=d.ename;\u00a0\u00a0 \u9519\u8bef\u7684<\/p>\n<h2>6.5 \u6392\u5e8f<\/h2>\n<h3>6.5.1 \u5168\u5c40\u6392\u5e8f\uff08Order By\uff09<\/h3>\n<p>Order By\uff1a\u5168\u5c40\u6392\u5e8f\uff0c\u4e00\u4e2aMapReduce<\/p>\n<p>1\uff09\u4f7f\u7528 ORDER BY \u5b50\u53e5\u6392\u5e8f<\/p>\n<p>ASC\uff08ascend\uff09: \u5347\u5e8f\uff08\u9ed8\u8ba4\uff09<\/p>\n<p>DESC\uff08descend\uff09: \u964d\u5e8f<\/p>\n<p>2\uff09ORDER BY \u5b50\u53e5\u5728SELECT\u8bed\u53e5\u7684\u7ed3\u5c3e\u3002<\/p>\n<p>3\uff09\u6848\u4f8b\u5b9e\u64cd<\/p>\n<p>\uff081\uff09\u67e5\u8be2\u5458\u5de5\u4fe1\u606f\u6309\u5de5\u8d44\u5347\u5e8f\u6392\u5217<\/p>\n<p>hive (default)&gt; select * from emp order by sal;<\/p>\n<p>\uff082\uff09\u67e5\u8be2\u5458\u5de5\u4fe1\u606f\u6309\u5de5\u8d44\u964d\u5e8f\u6392\u5217<\/p>\n<p>hive (default)&gt; select * from emp order by sal desc;<\/p>\n<h3>6.5.2 \u6309\u7167\u522b\u540d\u6392\u5e8f<\/h3>\n<p>\u6309\u7167\u5458\u5de5\u85aa\u6c34\u76842\u500d\u6392\u5e8f<\/p>\n<p>hive (default)&gt; select ename, sal*2 twosal from emp order by twosal;<\/p>\n<h3>6.5.3 \u591a\u4e2a\u5217\u6392\u5e8f<\/h3>\n<p>\u6309\u7167\u90e8\u95e8\u548c\u5de5\u8d44\u5347\u5e8f\u6392\u5e8f<\/p>\n<p>hive (default)&gt; select ename, deptno, sal from emp order by deptno, sal ;<\/p>\n<p>\u6ce8\uff1a\u8fd9\u4e2a\u662f\u5148\u6309\u90e8\u95e8\u53f7\u6392\u5e8f\uff0c\u90e8\u95e8\u53f7\u76f8\u540c\uff0c\u6309\u85aa\u6c34\u5347\u5e8f\u6392\u5e8f<\/p>\n<h3>6.5.4 <span style=\"color: #ff0000;\">\u6bcf\u4e2aMapReduce\u5185\u90e8\u6392\u5e8f\uff08Sort By\uff09<\/span><\/h3>\n<p>Sort By\uff1a\u6bcf\u4e2aMapReduce\u5185\u90e8\u8fdb\u884c\u6392\u5e8f\uff0c\u5206\u533a\u89c4\u5219\u6309\u7167key\u7684hash\u6765\u8fd0\u7b97\uff0c\uff08\u533a\u5185\u6392\u5e8f\uff09\u5bf9\u5168\u5c40\u7ed3\u679c\u96c6\u6765\u8bf4\u4e0d\u662f\u6392\u5e8f\u3002<\/p>\n<p>1\uff09<span style=\"color: #ff0000;\">\u8bbe\u7f6ereduce\u4e2a\u6570<\/span><\/p>\n<p>hive (default)&gt; set mapreduce.job.reduces=3;<\/p>\n<p>2\uff09\u67e5\u770b\u8bbe\u7f6ereduce\u4e2a\u6570<\/p>\n<p>hive (default)&gt; set mapreduce.job.reduces;<\/p>\n<p>3\uff09\u6839\u636e\u90e8\u95e8\u7f16\u53f7\u964d\u5e8f\u67e5\u770b\u5458\u5de5\u4fe1\u606f<\/p>\n<p>hive (default)&gt; select * from emp sort by empno desc;<\/p>\n<p>4\uff09\u5c06\u67e5\u8be2\u7ed3\u679c\u5bfc\u5165\u5230\u6587\u4ef6\u4e2d\uff08\u6309\u7167\u90e8\u95e8\u7f16\u53f7\u964d\u5e8f\u6392\u5e8f\uff09<\/p>\n<p>hive (default)&gt; insert overwrite local directory &#8216;\/opt\/module\/datas\/emp1.txt&#8217; row format delimited fields terminated by &#8216;\\t&#8217; select * from emp sort by deptno desc;<\/p>\n<h3>6.5.5 \u5206\u533a\u6392\u5e8f\uff08<span style=\"color: #ff0000;\">Distribute By<\/span>\uff09<\/h3>\n<p>Distribute By\uff1a\u7c7b\u4f3cMR\u4e2dpartition\uff0c\u8fdb\u884c\u5206\u533a\uff0c\u7ed3\u5408sort by\u4f7f\u7528\u3002<\/p>\n<p>\u6ce8\u610f\uff0cHive\u8981\u6c42DISTRIBUTE BY\u8bed\u53e5\u8981\u5199\u5728SORT BY\u8bed\u53e5\u4e4b\u524d\u3002<\/p>\n<p>\u5bf9\u4e8edistribute by\u8fdb\u884c\u6d4b\u8bd5\uff0c\u4e00\u5b9a\u8981\u5206\u914d\u591areduce\u8fdb\u884c\u5904\u7406\uff0c\u5426\u5219\u65e0\u6cd5\u770b\u5230distribute by\u7684\u6548\u679c\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u6848\u4f8b\u5b9e\u64cd\uff1a<\/p>\n<p>\uff081\uff09\u5148\u6309\u7167\u90e8\u95e8\u7f16\u53f7\u5206\u533a\uff0c\u518d\u6309\u7167\u5458\u5de5\u7f16\u53f7\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<p>hive (default)&gt; set mapreduce.job.reduces=3;<\/p>\n<p>hive (default)&gt;<span style=\"color: #ff0000;\"> insert overwrite local directory &#8216;\/opt\/module\/datas\/distribute-result&#8217; row format delimited fields terminated by &#8216;\\t&#8217; select * from emp distribute by deptno sort by empno desc;<\/span><\/p>\n<h3>6.5.6 Cluster By<\/h3>\n<p>\u5f53distribute by\u548csorts by\u5b57\u6bb5\u76f8\u540c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528cluster by\u65b9\u5f0f\u3002<\/p>\n<p>cluster by\u9664\u4e86\u5177\u6709distribute by\u7684\u529f\u80fd\u5916\u8fd8\u517c\u5177sort by\u7684\u529f\u80fd\u3002\u4f46\u662f\u6392\u5e8f\u53ea\u80fd\u662f\u5012\u5e8f\u6392\u5e8f\uff0c\u4e0d\u80fd\u6307\u5b9a\u6392\u5e8f\u89c4\u5219\u4e3aASC\u6216\u8005DESC\u3002<\/p>\n<p>1\uff09\u4ee5\u4e0b\u4e24\u79cd\u5199\u6cd5\u7b49\u4ef7<\/p>\n<p>hive (default)&gt; insert overwrite local directory &#8216;\/opt\/module\/datas\/cluster-result&#8217; row format delimited fields terminated by &#8216;\\t&#8217; select * from emp cluster by deptno;<\/p>\n<p>hive (default)&gt; select * from emp distribute by deptno sort by deptno;<\/p>\n<p>\u6ce8\u610f\uff1a\u6309\u7167\u90e8\u95e8\u7f16\u53f7\u5206\u533a\uff0c\u4e0d\u4e00\u5b9a\u5c31\u662f\u56fa\u5b9a\u6b7b\u7684\u6570\u503c\uff0c\u53ef\u4ee5\u662f20\u53f7\u548c30\u53f7\u90e8\u95e8\u5206\u5230\u4e00\u4e2a\u5206\u533a\u91cc\u9762\u53bb\u3002<\/p>\n<p><span style=\"color: #ff0000;\">\u6ce8:\u5efa\u8bae\u5206\u5f00\u4f7f\u7528<\/span>,cluster by \u4f1a\u541e\u5e76\u6389sort by\u7684\u5e76\u884c\u5ea6<\/p>\n<h2>6.6 \u5206\u6876\u53ca\u62bd\u6837\u67e5\u8be2(\u719f\u6089)<\/h2>\n<h3>6.6.1 \u5206\u6876\u8868\u6570\u636e\u5b58\u50a8<\/h3>\n<p>\u5206\u533a\u9488\u5bf9\u7684\u662f\u6570\u636e\u7684\u5b58\u50a8\u8def\u5f84\uff1b\u5206\u6876\u9488\u5bf9\u7684\u662f\u6570\u636e\u6587\u4ef6\u3002<\/p>\n<p>\u5206\u533a\u63d0\u4f9b\u4e00\u4e2a\u9694\u79bb\u6570\u636e\u548c\u4f18\u5316\u67e5\u8be2\u7684\u4fbf\u5229\u65b9\u5f0f\u3002\u4e0d\u8fc7\uff0c\u5e76\u975e\u6240\u6709\u7684\u6570\u636e\u96c6\u90fd\u53ef\u5f62\u6210\u5408\u7406\u7684\u5206\u533a\uff0c\u7279\u522b\u662f\u4e4b\u524d\u6240\u63d0\u5230\u8fc7\u7684\u8981\u786e\u5b9a\u5408\u9002\u7684\u5212\u5206\u5927\u5c0f\u8fd9\u4e2a\u7591\u8651\u3002<\/p>\n<p>\u5206\u6876\u662f\u5c06\u6570\u636e\u96c6\u5206\u89e3\u6210\u66f4\u5bb9\u6613\u7ba1\u7406\u7684\u82e5\u5e72\u90e8\u5206\u7684\u53e6\u4e00\u4e2a\u6280\u672f\u3002<\/p>\n<p>1\uff09\u5148\u521b\u5efa\u5206\u6876\u8868\uff0c\u901a\u8fc7\u76f4\u63a5\u5bfc\u5165\u6570\u636e\u6587\u4ef6\u7684\u65b9\u5f0f<\/p>\n<p>\uff080\uff09\u6570\u636e\u51c6\u5907<\/p>\n<p>&nbsp;<\/p>\n<p>\uff081\uff09\u521b\u5efa\u5206\u6876\u8868<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"519\">create table stu_buck(id int, name string)<\/p>\n<p><span style=\"color: #ff0000;\">clustered by(id)<\/span><\/p>\n<p><span style=\"color: #ff0000;\">into 4 buckets<\/span><\/p>\n<p>row format delimited fields terminated by &#8216;\\t&#8217;;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\uff082\uff09\u67e5\u770b\u8868\u7ed3\u6784<\/p>\n<p>hive (default)&gt; desc formatted stu_buck;<\/p>\n<p>Num Buckets:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4<\/p>\n<p>\uff083\uff09\u5bfc\u5165\u6570\u636e\u5230\u5206\u6876\u8868\u4e2d<\/p>\n<p>hive (default)&gt; load data local inpath &#8216;\/opt\/module\/datas\/stu_buck.txt&#8217; into table stu_buck;<\/p>\n<p>\uff084\uff09\u67e5\u770b\u521b\u5efa\u7684\u5206\u6876\u8868\u4e2d\u662f\u5426\u5206\u62104\u4e2a\u6876<\/p>\n<p>\u53d1\u73b0\u5e76\u6ca1\u6709\u5206\u62104\u4e2a\u6876\u3002\u662f\u4ec0\u4e48\u539f\u56e0\u5462\uff1f<\/p>\n<p>2\uff09\u521b\u5efa\u5206\u6876\u8868\u65f6\uff0c\u6570\u636e\u901a\u8fc7\u5b50\u67e5\u8be2\u7684\u65b9\u5f0f\u5bfc\u5165<\/p>\n<p>\uff081\uff09\u5148\u5efa\u4e00\u4e2a\u666e\u901a\u7684stu\u8868<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"510\">create table stu(id int, name string)<\/p>\n<p>row format delimited fields terminated by &#8216;\\t&#8217;;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\uff082\uff09\u5411\u666e\u901a\u7684stu\u8868\u4e2d\u5bfc\u5165\u6570\u636e<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"510\">load data local inpath &#8216;\/opt\/module\/datas\/stu_buck.txt&#8217; into table stu;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\uff083\uff09\u6e05\u7a7astu_buck\u8868\u4e2d\u6570\u636e<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"510\">truncate table stu_buck;<\/p>\n<p>select * from stu_buck;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\uff084\uff09\u5bfc\u5165\u6570\u636e\u5230\u5206\u6876\u8868\uff0c\u901a\u8fc7\u5b50\u67e5\u8be2\u7684\u65b9\u5f0f<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"510\">insert into table stu_buck<\/p>\n<p>select id, name from stu;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\uff085\uff09\u53d1\u73b0\u8fd8\u662f\u53ea\u6709\u4e00\u4e2a\u5206\u6876<\/p>\n<p>\uff086\uff09\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2a\u5c5e\u6027<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"510\">hive (default)&gt; <span style=\"color: #ff0000;\">set hive.enforce.bucketing=true;<\/span><\/p>\n<p>hive (default)&gt; <span style=\"color: #ff0000;\">set mapreduce.job.reduces=-1;<\/span><\/p>\n<p>hive (default)&gt; insert into table stu_buck<\/p>\n<p>select id, name from stu;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\uff087\uff09\u67e5\u8be2\u5206\u6876\u7684\u6570\u636e<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"510\">hive (default)&gt; select * from stu_buck;<\/p>\n<p>OK<\/p>\n<p>stu_buck.id\u00a0\u00a0\u00a0\u00a0 stu_buck.name<\/p>\n<p>1001\u00a0\u00a0\u00a0 ss1<\/p>\n<p>1005\u00a0\u00a0\u00a0 ss5<\/p>\n<p>1009\u00a0\u00a0\u00a0 ss9<\/p>\n<p>1012\u00a0\u00a0\u00a0 ss12<\/p>\n<p>1016\u00a0\u00a0\u00a0 ss16<\/p>\n<p>1002\u00a0\u00a0\u00a0 ss2<\/p>\n<p>1006\u00a0\u00a0\u00a0 ss6<\/p>\n<p>1013\u00a0\u00a0\u00a0 ss13<\/p>\n<p>1003\u00a0\u00a0 \u00a0ss3<\/p>\n<p>1007\u00a0\u00a0\u00a0 ss7<\/p>\n<p>1010\u00a0\u00a0\u00a0 ss10<\/p>\n<p>1014\u00a0\u00a0\u00a0 ss14<\/p>\n<p>1004\u00a0\u00a0\u00a0 ss4<\/p>\n<p>1008\u00a0\u00a0\u00a0 ss8<\/p>\n<p>1011\u00a0\u00a0\u00a0 ss11<\/p>\n<p>1015\u00a0\u00a0\u00a0 ss15<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>6.6.2 \u5206\u6876\u62bd\u6837\u67e5\u8be2<\/h3>\n<p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u6709\u65f6\u7528\u6237\u9700\u8981\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u5177\u6709\u4ee3\u8868\u6027\u7684\u67e5\u8be2\u7ed3\u679c\u800c\u4e0d\u662f\u5168\u90e8\u7ed3\u679c\u3002Hive\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8868\u8fdb\u884c\u62bd\u6837\u6765\u6ee1\u8db3\u8fd9\u4e2a\u9700\u6c42\u3002<\/p>\n<p>\u67e5\u8be2\u8868stu_buck\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"519\">hive (default)&gt; select * from stu_buck tablesample(bucket 1 out of 4 on id);<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6ce8\uff1atablesample\u662f\u62bd\u6837\u8bed\u53e5\uff0c\u8bed\u6cd5\uff1aTABLESAMPLE(BUCKET x OUT OF y)\u00a0\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"519\">hive (default)&gt; select * from stu_buck tablesample(bucket 1 out of 3 on id);<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0d\u662f\u6876\u6570\u7684\u500d\u6570\u6216\u8005\u56e0\u5b50\u4e5f\u53ef\u4ee5\uff0c\u4f46\u662f\u4e0d\u63a8\u8350\u3002<\/p>\n<p>y\u5fc5\u987b\u662ftable\u603bbucket\u6570\u7684\u500d\u6570\u6216\u8005\u56e0\u5b50\u3002hive\u6839\u636ey\u7684\u5927\u5c0f\uff0c\u51b3\u5b9a\u62bd\u6837\u7684\u6bd4\u4f8b\u3002\u4f8b\u5982\uff0ctable\u603b\u5171\u5206\u4e864\u4efd\uff0c\u5f53y=2\u65f6\uff0c\u62bd\u53d6(4\/2=)2\u4e2abucket\u7684\u6570\u636e\uff0c\u5f53y=8\u65f6\uff0c\u62bd\u53d6(4\/8=)1\/2\u4e2abucket\u7684\u6570\u636e\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"519\">hive (default)&gt; select * from stu_buck tablesample(bucket 1 out of 2 on id);<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>x\u8868\u793a\u4ece\u54ea\u4e2abucket\u5f00\u59cb\u62bd\u53d6\u3002\u4f8b\u5982\uff0ctable\u603bbucket\u6570\u4e3a4\uff0ctablesample(bucket 4 out of 4)\uff0c\u8868\u793a\u603b\u5171\u62bd\u53d6\uff084\/4=\uff091\u4e2abucket\u7684\u6570\u636e\uff0c\u62bd\u53d6\u7b2c4\u4e2abucket\u7684\u6570\u636e\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"519\">hive (default)&gt; select * from stu_buck tablesample(bucket 1 out of 8 on id);<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6ce8\u610f\uff1ax\u7684\u503c\u5fc5\u987b\u5c0f\u4e8e\u7b49\u4e8ey\u7684\u503c\uff0c\u5426\u5219<\/p>\n<p>FAILED: SemanticException [Error 10061]: Numerator should not be bigger than denominator in sample clause for table stu_buck<\/p>\n<h3>6.6.3 \u6570\u636e\u5757\u62bd\u6837<\/h3>\n<p>Hive\u63d0\u4f9b\u4e86\u53e6\u5916\u4e00\u79cd\u6309\u7167\u767e\u5206\u6bd4\u8fdb\u884c\u62bd\u6837\u7684\u65b9\u5f0f\uff0c\u8fd9\u79cd\u662f\u57fa\u4e8e\u884c\u6570\u7684\uff0c\u6309\u7167\u8f93\u5165\u8def\u5f84\u4e0b\u7684\u6570\u636e\u5757\u767e\u5206\u6bd4\u8fdb\u884c\u7684\u62bd\u6837\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"519\">hive (default)&gt; select * from stu tablesample(0.1 percent) ;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u63d0\u793a\uff1a\u8fd9\u79cd\u62bd\u6837\u65b9\u5f0f\u4e0d\u4e00\u5b9a\u9002\u7528\u4e8e\u6240\u6709\u7684\u6587\u4ef6\u683c\u5f0f\u3002\u53e6\u5916\uff0c\u8fd9\u79cd\u62bd\u6837\u7684\u6700\u5c0f\u62bd\u6837\u5355\u5143\u662f\u4e00\u4e2aHDFS\u6570\u636e\u5757\u3002\u56e0\u6b64\uff0c\u5982\u679c\u8868\u7684\u6570\u636e\u5927\u5c0f\u5c0f\u4e8e\u666e\u901a\u7684\u5757\u5927\u5c0f128M\u7684\u8bdd\uff0c\u90a3\u4e48\u5c06\u4f1a\u8fd4\u56de\u6240\u6709\u884c\u3002<\/p>\n<h1>\u4e03 \u51fd\u6570<\/h1>\n<h2>7.1 \u7cfb\u7edf\u81ea\u5e26\u7684\u51fd\u6570<\/h2>\n<p>1\uff09\u67e5\u770b\u7cfb\u7edf\u81ea\u5e26\u7684\u51fd\u6570<\/p>\n<p>hive&gt; show functions;<\/p>\n<p>2\uff09\u663e\u793a\u81ea\u5e26\u7684\u51fd\u6570\u7684\u7528\u6cd5<\/p>\n<p>hive&gt; desc function upper;<\/p>\n<p>3\uff09\u8be6\u7ec6\u663e\u793a\u81ea\u5e26\u7684\u51fd\u6570\u7684\u7528\u6cd5<\/p>\n<p>hive&gt; desc function extended upper;<\/p>\n<h2>7.2 \u81ea\u5b9a\u4e49\u51fd\u6570<\/h2>\n<p>1\uff09Hive \u81ea\u5e26\u4e86\u4e00\u4e9b\u51fd\u6570\uff0c\u6bd4\u5982\uff1amax\/min\u7b49\uff0c\u4f46\u662f\u6570\u91cf\u6709\u9650\uff0c\u81ea\u5df1\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49UDF\u6765\u65b9\u4fbf\u7684\u6269\u5c55\u3002<\/p>\n<p>2\uff09\u5f53Hive\u63d0\u4f9b\u7684\u5185\u7f6e\u51fd\u6570\u65e0\u6cd5\u6ee1\u8db3\u4f60\u7684\u4e1a\u52a1\u5904\u7406\u9700\u8981\u65f6\uff0c\u6b64\u65f6\u5c31\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\uff08UDF\uff1auser-defined function\uff09\u3002<\/p>\n<p>3\uff09\u6839\u636e\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u7c7b\u522b\u5206\u4e3a\u4ee5\u4e0b\u4e09\u79cd\uff1a<\/p>\n<p>\uff081\uff09UDF\uff08User-Defined-Function\uff09<\/p>\n<p>\u4e00\u8fdb\u4e00\u51fa<\/p>\n<p>\uff082\uff09UDAF\uff08User-Defined Aggregation Function\uff09<\/p>\n<p>\u805a\u96c6\u51fd\u6570\uff0c\u591a\u8fdb\u4e00\u51fa<\/p>\n<p>\u7c7b\u4f3c\u4e8e\uff1acount\/max\/min<\/p>\n<p>\uff083\uff09UDTF\uff08User-Defined Table-Generating Functions\uff09<\/p>\n<p>\u4e00\u8fdb\u591a\u51fa<\/p>\n<p>\u5982lateral view explore()<\/p>\n<p>4\uff09\u5b98\u65b9\u6587\u6863\u5730\u5740<\/p>\n<p>https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/HivePlugins<\/p>\n<p>5\uff09\u7f16\u7a0b\u6b65\u9aa4\uff1a<\/p>\n<p>\uff081\uff09\u7ee7\u627forg.apache.hadoop.hive.ql.UDF<\/p>\n<p>\uff082\uff09\u9700\u8981\u5b9e\u73b0evaluate\u51fd\u6570\uff1bevaluate\u51fd\u6570\u652f\u6301\u91cd\u8f7d\uff1b<\/p>\n<p>\uff083\uff09\u5728hive\u7684\u547d\u4ee4\u884c\u7a97\u53e3\u521b\u5efa\u51fd\u6570<\/p>\n<p>a\uff09\u6dfb\u52a0jar<\/p>\n<p>add jar linux_jar_path<\/p>\n<p>b\uff09\u521b\u5efafunction\uff0c<\/p>\n<p>create [temporary] function [dbname.]function_name AS class_name;<\/p>\n<p>\uff084\uff09\u5728hive\u7684\u547d\u4ee4\u884c\u7a97\u53e3\u5220\u9664\u51fd\u6570<\/p>\n<p>Drop [temporary] function [if exists] [dbname.]function_name;<\/p>\n<p>6\uff09\u6ce8\u610f\u4e8b\u9879<\/p>\n<p>\uff081\uff09UDF\u5fc5\u987b\u8981\u6709\u8fd4\u56de\u7c7b\u578b\uff0c\u53ef\u4ee5\u8fd4\u56denull\uff0c\u4f46\u662f\u8fd4\u56de\u7c7b\u578b\u4e0d\u80fd\u4e3avoid\uff1b<\/p>\n<h2>7.3 \u81ea\u5b9a\u4e49UDF\u51fd\u6570\u5f00\u53d1\u6848\u4f8b<\/h2>\n<p>1\uff09\u521b\u5efa\u4e00\u4e2ajava\u5de5\u7a0b\uff0c\u5e76\u521b\u5efa\u4e00\u4e2alib\u6587\u4ef6\u5939<\/p>\n<p>2\uff09\u5c06hive\u7684jar\u5305\u89e3\u538b\u540e\uff0c\u5c06apache-hive-1.2.1-bin\\lib\u6587\u4ef6\u4e0b\u7684jar\u5305\u90fd\u62f7\u8d1d\u5230java\u5de5\u7a0b\u4e2d\u3002<\/p>\n<p>3\uff09\u521b\u5efa\u4e00\u4e2a\u7c7b<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"548\">package vip.isok.hive;<\/p>\n<p>import org.apache.hadoop.hive.ql.exec.UDF;<\/p>\n<p>&nbsp;<\/p>\n<p>public class Lower extends <span style=\"color: #ff0000;\">UDF<\/span> {<\/p>\n<p>&nbsp;<\/p>\n<p>public String <span style=\"color: #ff0000;\">evaluate<\/span> (final String s) {<\/p>\n<p>&nbsp;<\/p>\n<p>if (s == null) {<\/p>\n<p>return null;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>return s.toString().toLowerCase();<\/p>\n<p>}<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>4\uff09\u6253\u6210jar\u5305\u4e0a\u4f20\u5230\u670d\u52a1\u5668\/opt\/module\/jars\/udf.jar<\/p>\n<p>5\uff09\u5c06jar\u5305\u6dfb\u52a0\u5230hive\u7684classpath<\/p>\n<p>hive (default)&gt; add jar \/opt\/module\/datas\/udf.jar;<\/p>\n<p>6\uff09\u521b\u5efa\u4e34\u65f6\u51fd\u6570\u4e0e\u5f00\u53d1\u597d\u7684java class\u5173\u8054(\u5168\u7c7b\u540d)<\/p>\n<p>hive (default)&gt; create temporary function udf_lower as &#8220;Lower&#8221;;<\/p>\n<p>7\uff09\u5373\u53ef\u5728hql\u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u51fd\u6570strip<\/p>\n<p>hive (default)&gt; select ename, udf_lower(ename) lowername from emp;<\/p>\n<p>&nbsp;<\/p>\n<h2>7.4 \u884c\u8f6c\u5217\u3001\u5217\u8f6c\u884c\uff08UDAF\u4e0eUDTF\uff09<\/h2>\n<h3>7.4.1\u3001\u5217\u8f6c\u884c<\/h3>\n<p><strong>\u8868\u7ed3\u6784\uff1a<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"189\">name<\/td>\n<td width=\"189\">constellation<\/td>\n<td width=\"189\">blood_type<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">\u5b59\u609f\u7a7a<\/td>\n<td width=\"189\">\u767d\u7f8a\u5ea7<\/td>\n<td width=\"189\">A<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">\u767d\u9aa8\u7cbe<\/td>\n<td width=\"189\">\u5c04\u624b\u5ea7<\/td>\n<td width=\"189\">A<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">\u6c99\u548c\u5c1a<\/td>\n<td width=\"189\">\u767d\u7f8a\u5ea7<\/td>\n<td width=\"189\">B<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">\u732a\u516b\u6212<\/td>\n<td width=\"189\">\u767d\u7f8a\u5ea7<\/td>\n<td width=\"189\">A<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">\u86e4\u87c6\u7cbe<\/td>\n<td width=\"189\">\u5c04\u624b\u5ea7<\/td>\n<td width=\"189\">A<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u521b\u5efa\u8868\u53ca\u6570\u636e\u5bfc\u5165\uff1a<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"568\">create table person_info(<\/p>\n<p>name string,<\/p>\n<p>constellation string,<\/p>\n<p>blood_type string)<\/p>\n<p>row format delimited fields terminated by &#8220;\\t&#8221;;<\/p>\n<p>load data local inpath &#8216;\/opt\/module\/datas\/person_info.tsv&#8217; into table person_info;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u6ce8\uff1a<\/strong><\/p>\n<ul>\n<li><strong>Concat()<\/strong><strong>\u51fd\u6570<\/strong> \u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u7528\u7279\u5b9a\u7b26\u53f7\u94fe\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\n<ol>\n<li>concat(constellation, &#8220;,&#8221;, blood_type,&#8221;,&#8221;,\u5b57\u6bb51\uff0c&#8221;,&#8221;,\u5b57\u6bb52\uff0c&#8221;,&#8221;,\u5b57\u6bb53)<\/li>\n<li>select concat(constellation, &#8220;,&#8221;, blood_type, &#8220;:&#8221;, name) from person_info;<\/li>\n<\/ol>\n<\/li>\n<li><strong>concat_ws()<\/strong><strong>\u51fd\u6570 <\/strong>\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u7528\u7279\u5b9a\u7b26\u53f7\u94fe\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\n<ol>\n<li>select concat_ws(&#8220;,&#8221;, blood_type, constellation, name) from person_info;<\/li>\n<\/ol>\n<\/li>\n<li><strong>collect_set()<\/strong><strong>\u51fd\u6570 <\/strong>\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u7528\u7279\u5b9a\u7b26\u53f7\u94fe\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e14\u53bb\u9664\u91cd\u590d\u5143\u7d20\u3002\n<ol>\n<li><strong>select collect_set(constellation) from person_info;<\/strong><\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p><strong>\u4f8b\u5982\uff1a\u628a\u661f\u5ea7\u548c\u8840\u578b\u4e00\u6837\u7684\u4eba\u5f52\u7c7b\u5230\u4e00\u8d77<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"568\">select<\/p>\n<p>t1.base,<\/p>\n<p>concat_ws(&#8216;|&#8217;, collect_set(t1.name)) name<\/p>\n<p>from<\/p>\n<p>(select<\/p>\n<p>name,<\/p>\n<p>concat(constellation, &#8220;,&#8221;, blood_type) base<\/p>\n<p>from<\/p>\n<p>person_info) t1<\/p>\n<p>group by<\/p>\n<p>t1.base;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h3>7.4.2\u3001\u884c\u8f6c\u5217<\/h3>\n<p><strong>\u8868\u7ed3\u6784\uff1a<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"284\">movie<\/td>\n<td width=\"284\">category<\/td>\n<\/tr>\n<tr>\n<td width=\"284\">\u300a\u7591\u72af\u8ffd\u8e2a\u300b<\/td>\n<td width=\"284\">\u60ac\u7591,\u52a8\u4f5c,\u79d1\u5e7b,\u5267\u60c5<\/td>\n<\/tr>\n<tr>\n<td width=\"284\">\u300aLie to me\u300b<\/td>\n<td width=\"284\">\u60ac\u7591,\u8b66\u532a,\u52a8\u4f5c,\u5fc3\u7406,\u5267\u60c5<\/td>\n<\/tr>\n<tr>\n<td width=\"284\">\u300a\u6218\u72fc2\u300b<\/td>\n<td width=\"284\">\u6218\u4e89,\u52a8\u4f5c,\u707e\u96be<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u521b\u5efa\u8868\u53ca\u5bfc\u5165\u6570\u636e\uff1a<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"568\">create table movie_info(<\/p>\n<p>movie string,<\/p>\n<p>category array&lt;string&gt;)<\/p>\n<p>row format delimited fields terminated by &#8220;\\t&#8221;<\/p>\n<p>collection items terminated by &#8220;,&#8221;;<\/p>\n<p>load data local inpath &#8220;\/opt\/module\/datas\/movie_info.tsv&#8221; into table movie_info;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u4f8b\u5982\uff1a\u5c06\u7535\u5f71\u5206\u7c7b\u4e2d\u7684\u6570\u7ec4\u6570\u636e\u5c55\u5f00<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"568\">select<\/p>\n<p>movie,<\/p>\n<p>category_name<\/p>\n<p>from<\/p>\n<p>movie_info<\/p>\n<p>lateral view explode(category) table_tmp as category_name;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>lateral view \u8bed\u6cd5\u5f0f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5.1 \u6570\u636e\u5bfc\u5165 5.1.1 \u5411\u8868\u4e2d\u88c5\u8f7d\u6570\u636e\uff08Load\uff09 1\uff09\u8bed\u6cd5 hive&gt;load data [lo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1329","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1329"}],"version-history":[{"count":3,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1329\/revisions"}],"predecessor-version":[{"id":1421,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1329\/revisions\/1421"}],"wp:attachment":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}