--查询出薪水等于5000的所有员工 select * from emp where sal =5000;
--查询工资在5000到10000的员工信息 select * from emp where sal between500and10000; --查询comm为空的所有员工信 select * from emp where comm isnull; --查询工资是1500或5000的员工信息 select * from emp where sal in(1500,5000); select * from emp where sal=1500or sal= 5000;
-- 查找以2开头薪水的员工信息 select * from emp where sal LIKE'2%'; -- 查找第二个数值为2的薪水的员工信息 select * from emp where sal LIKE'_2%'; -- 查找薪水中含有2的员工信息 select * from emp where sal RLIKE'[2]';
逻辑运算符
操作符
含义
AND
逻辑并
OR
逻辑或
NOT
逻辑否
1 2 3 4
--求每个部门的平均工资 select deptno, avg(sal) from emp groupby deptno; --求每个部门的平均薪水大于2000的部门 select deptno, avg(sal) avg_sal from emp groupby deptno having avg_sal > 2000;
Join语句
等值Join
注意:Hive支持通常的SQL JOIN语句,但 只支持等值连接,不支持非等值连接。
1 2 3 4
--根据员工表和部门表中的部门编号相等,查询员工编号、员工名称和部门名称; select e.empno, e.ename, d.deptno, d.dname from emp e join dept d on e.deptno = d.deptno; --合并员工表和部门表 select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;
表的别名:(1)使用别名可以简化查询。(2)使用表名前缀可以提高执行效率。
左外连接
左外连接:JOIN操作符左边表中符合WHERE子句的所有记录将会被返回。
1
select e.empno, e.ename, d.deptno from emp e leftjoin dept d on e.deptno = d.deptno;
右外连接
右外连接:JOIN操作符右边表中符合WHERE子句的所有记录将会被返回。
1
select e.empno, e.ename, d.deptno from emp e rightjoin dept d on e.deptno = d.deptno;
--案例实操 select empno, dname from emp, dept; --连接谓词中不支持or select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno= d.deptno or e.ename=d.ename; ## 错误的
排序
全局排序
Order By:全局排序,一个Reducer
1.使用 ORDER BY 子句排序 ASC(ascend): 升序(默认) DESC(descend): 降序
ORDER BY 子句在SELECT语句的结尾
1 2 3 4
--查询员工信息按工资升序排列 hive (default)> select * from emp order by sal; --查询员工信息按工资降序排列 select * from emp orderby sal desc;
按照别名排序
1 2 3 4
--按照员工薪水的2倍排序 select ename, sal*2 twosal from emp orderby twosal; --按照部门和工资升序排序 select ename, deptno, sal from emp orderby deptno, sal;
MR内部排序(Sort By)
Sort By:每个Reducer内部进行排序,对全局结果集来说不是排序。
1 2 3 4 5 6 7 8 9 10 11
--设置reduce个数 set mapreduce.job.reduces=3;
--查看设置reduce个数 set mapreduce.job.reduces;
--根据部门编号降序查看员工信息 select empno,ename,sal,deptno from emp sortby empno desc;
--按照部门编号降序排序 select empno,ename,sal,deptno from emp sortby deptno desc;
x表示从哪个bucket开始抽取,如果需要取多个分区,以后的分区号为当前分区号加上y。例如,table总bucket数为4,tablesample(bucket 1 out of 2),表示总共抽取(4/2=)2个bucket的数据,抽取第1(x)个和第4(x+y)个bucket的数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
--抽取第一个分区的数据 hive> select * from stu_buck tablesample(bucket 1 out of 4 on id); OK 1016 ss16 1012 ss12 1008 ss8 1004 ss4 --查询第一个分区一半的数据 hive> select * from stu_buck tablesample(bucket 1 out of 8 on id); OK 1016 ss16 1008 ss8
--x的值必须小于等于y的值,否则 hive> select * from stu_buck tablesample(bucket 6 out of 3 on id); FAILED: SemanticException [Error 10061]: Numerator should not be bigger than denominator in sample clause for table stu_buck
public String evaluate (final String s) { if (s == null) { return null; } return s.toLowerCase(); } }
上传上服务器
添加jar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
hive> add jar /home/hadoop/udf.jar; Added [/home/hadoop/udf.jar] to class path Added resources: [/home/hadoop/udf.jar] --添加关联 hive> add jar /home/hadoop/udf.jar; Added [/home/hadoop/udf.jar] to class path Added resources: [/home/hadoop/udf.jar] hive> create temporary function mylower as "com.hph.Lower"; OK Time taken: 0.033 seconds
--调用 hive> select ename, mylower(ename) lowername from emp limit 2; OK ename lowername SMITH smith ALLEN allen