-- 转小写。lower select lower("HELLO WORLD"); OK _c0 hello world
-- 转大写。upper select lower(ename), ename from emp; OK _c0 ename clerk CLERK allen ALLEN ward WARD jones JONES martin MARTIN blake BLAKE clark CLARK scott SCOTT king KING turner TURNER adams ADAMS james JAMES ford FORD miller MILLER
-- 求字符串长度。length select length(ename), ename from emp; OK _c0 ename 5 CLERK 5 ALLEN 4 WARD 5 JONES 6 MARTIN 5 BLAKE 5 CLARK 5 SCOTT 4 KING 6 TURNER 5 ADAMS 5 JAMES 4 FORD 6 MILLER
-- 字符串拼接。 concat / || select empno || " " ||ename idname from emp; OK idname NULL 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER
select concat(empno, " " ,ename) idname from emp; OK idname NULL 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER
select concat_ws(" ", ename, job) from emp; OK _c0 CLERK 7902 ALLEN SALESMAN WARD SALESMAN JONES MANAGER MARTIN SALESMAN BLAKE MANAGER CLARK MANAGER SCOTT ANALYST KING PRESIDENT TURNER SALESMAN ADAMS CLERK JAMES CLERK FORD ANALYST MILLER CLERK
-- 求子串。substr SELECT substr('www.lagou.com', 5); OK _c0 lagou.com
SELECT substr('www.lagou.com', -5); OK _c0 u.com
SELECT substr('www.lagou.com', 5, 5); OK _c0 lagou
-- 字符串切分。split,注意 '.' 要转义 select split("www.lagou.com", "\\."); OK _c0 ["www","lagou","com"]
-- 将emp表的员工工资等级分类:0-1500、1500-3000、3000以上 -- if (boolean testCondition, T valueTrue, T valueFalseOrNull) select sal, if (sal<1500, 1, if (sal < 3000, 2, 3)) from emp; OK sal _c1 NULL 3 1600 2 1250 1 2975 2 1250 1 2850 2 2450 2 3000 3 5000 3 1500 2 1100 1 950 1 3000 3 1300 1
-- CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END -- 复杂条件用 case when 更直观 select sal, case when sal<=1500 then 1 when sal<=3000 then 2 else 3 end sallevel from emp; OK sal sallevel NULL 3 1600 2 1250 1 2975 2 1250 1 2850 2 2450 2 3000 2 5000 3 1500 1 1100 1 950 1 3000 2 1300 1
-- 以下语句等价 select ename, deptno, case deptno when 10 then 'accounting' when 20 then 'research' when 30 then 'sales' else 'unknown' end deptname from emp;
select ename, deptno, case when deptno=10 then 'accounting' when deptno=20 then 'research' when deptno=30 then 'sales' else 'unknown' end deptname from emp;
-- explode,炸裂函数 -- 就是将一行中复杂的 array 或者 map 结构拆分成多行 select explode(array('A','B','C')) as col; OK col A B C
select explode(map('a', 8, 'b', 88, 'c', 888)); OK key value a 8 b 88 c 888
-- UDTF's are not supported outside the SELECT clause, nor nested in expressions -- SELECT pageid, explode(adid_list) AS myCol... is not supported -- SELECT explode(explode(adid_list)) AS myCol... is not supported
-- lateral view 的基本使用 with t1 as ( select 'OK' cola, split('www.lagou.com', '\\.') colb ) select cola, colc from t1 lateral view explode(colb) t2 as colc; OK cola colc OK www OK lagou OK com
-- 建表加载数据 create table tab1( id int, tags string ) row format delimited fields terminated by '\t'; load data local inpath '/home/hadoop/data/tab1.dat' into table tab1;
-- SQL select id, split(tags, ',') from tab1; select id, tag from tab1 lateral view explode(split(tags, ",")) t1 as tag;
-- 创建表 create table studscore( name string , score map<String,string> ) row format delimited fields terminated by '|' collection items terminated by ',' map keys terminated by ':';
-- 加载数据 load data local inpath '/home/hadoop/data/score.dat' overwrite into table studscore;
-- 需求:找到每个学员的最好成绩 -- 第一步,使用 explode 函数将map结构拆分为多行 select explode(score) as (subject, socre) from studscore;
-- 第二步:explode常与 lateral view 函数联用,这两个函数结合在一起能关 联其他字段 select name, subject, score1 as score from studscore lateral view explode(score) t1 as subject, score1;
-- 第三步:找到每个学员的最好成绩 select name, max(mark) maxscore from ( select name, subject, mark from studscore lateral view explode(score) t1 as subject, mark ) t1 group by name;
with tmp as ( select name, subject, mark from studscore lateral view explode(score) t1 as subject, mark ) select name, max(mark) maxscore from tmp group by name;