求每个部门的平均工资 hive (default)> select deptno, avg(sal) avg_sal from emp group by deptno; 求每个部门的平均薪水大于2000的部门 hive (default)> select deptno, avg(sal) avg_sal from emp group by deptno having avg_sal > 2000;
hive (default)> select e.empno, e.ename, d.deptno, d.dname from emp e join dept d on e.deptno = d.deptno;
表的别名 好处:使用别名可以简化查询。使用表名前缀可以提高执行效率。 案例实操
合并员工表和部门表
hive (default)> select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;
内连接 内连接:只有进行连接的两个表中都存在与连接条件相匹配的数据才会被保留下来。 hive (default)> select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;
左外连接 JOIN操作符左边表中符合WHERE子句的所有记录将会被返回。 hive (default)> select e.empno, e.ename, d.deptno from emp e left join dept d on e.deptno = d.deptno;
右外连接 JOIN操作符右边表中符合WHERE子句的所有记录将会被返回。 hive (default)> select e.empno, e.ename, d.deptno from emp e right join dept d on e.deptno = d.deptno;
满外链接 将会返回所有表中符合WHERE语句条件的所有记录。如果任一表的指定字段没有符合条件的值的话,那么就使用NULL值替代。 hive (default)> select e.empno, e.ename, d.deptno from emp e full join dept d on e.deptno = d.deptno;
全局排序(Order By) 全局排序,一个MapReduce 1)使用 ORDER BY 子句排序 ASC(ascend): 升序(默认) DESC(descend): 降序 2)ORDER BY 子句在SELECT语句的结尾 3)案例实操
(1)查询员工信息按工资升序排列
hive (default)> select * from emp order by sal;
(2)查询员工信息按工资降序排列
hive (default)> select * from emp order by sal desc;
按照别名排序
按照员工薪水的2倍排序
hive (default)> select ename, sal*2 twosal from emp order by twosal;
多个列排序
按照部门和工资升序排序
hive (default)> select ename, deptno, sal from emp order by deptno, sal ;
每个MapReduce内部排序(Sort By) 每个MapReduce内部进行排序,对全局结果集来说不是排序 将查询结果导入到文件中(按照部门编号降序排序) hive (default)> insert overwrite local directory '/opt/module/datas/sortby-result' select * from emp sort by deptno desc;
分区排序(Distribute By) 类似MR中partition,进行分区,结合sort by使用。注意,Hive要求DISTRIBUTE BY语句要写在SORT BY语句之前。 先按照部门编号分区,再按照员工编号降序排序。 hive (default)> insert overwrite local directory '/opt/module/datas/distby-desc' select * from emp distribute by deptno sort by empno desc;
Cluster By 当distribute by和sorts by字段相同时,可以使用cluster by方式。cluster by除了具有distribute by的功能外还兼具sort by的功能。但是排序只能是倒序排序,不能指定排序规则为ASC或者DESC。 以下两种写法等价 select * from emp cluster by deptno; select * from emp distribute by deptno sort by deptno; 注意:按照部门编号分区,不一定就是固定死的数值,可以是20号和30号部门分到一个分区里面去。
create table stu_buck(id int, name string) clustered by(id) into 4 buckets row format delimited fields terminated by '\t';
(2)查看表结构
hive (default)> desc formatted stu_buck; Num Buckets: 4
(3)导入数据到分桶表中
hive (default)> load data local inpath '/opt/module/datas/student.txt' into table stu_buck;
(4)查看创建的分桶表中是否分成4个桶
发现并没有分成4个桶。是什么原因呢?(往后面看,需要设置参数)
2)创建分桶表时,数据通过子查询的方式导入
(1)先建一个普通的stu表
create table stu(id int, name string) row format delimited fields terminated by '\t';
(2)向普通的stu表中导入数据
load data local inpath '/opt/module/datas/student.txt' into table stu;
(3)清空stu_buck表中数据
truncate table stu_buck; select * from stu_buck;
(4)导入数据到分桶表,通过子查询的方式
insert into table stu_buck select id, name from stu cluster by(id);
(5)需要设置一个属性
hive (default)>set hive.enforce.bucketing=true; hive (default)> set mapreduce.job.reduces=-1; hive (default)>insert into table stu_buck select id, name from stu cluster by(id);
(6)查询分桶的数据
hive (default)> select * from stu_buck; OK stu_buck.id stu_buck.name 1001 ss1 1005 ss5 1009 ss9 . . .
分桶抽样查询 对于非常大的数据集,有时用户需要使用的是一个具有代表性的查询结果而不是全部结果。Hive可以通过对表进行抽样来满足这个需求。 查询上表stu_buck的数据。 hive (default)> select * from stu_buck TABLESAMPLE(bucket 1 out of 4 on id); 注:tablesample是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y) 。 y必须是table总bucket数的倍数或者因子。hive根据y的大小,决定抽样的比例。例如,table总共分了4份,当y=2时,抽取(4/2=)2个bucket的数据,当y=8时,抽取(4/8=)1/2个bucket的数据。 x表示从哪个bucket开始抽取。例如,table总bucket数为4,tablesample(bucket 4 out of 4),表示总共抽取(4/4=)1个bucket的数据,抽取第4个bucket的数据。 注意:x的值必须小于等于y的值,否则 FAILED: SemanticException [Error 10061]: Numerator should not be bigger than denominator in sample clause for table stu_buck
数据块抽样 Hive提供了另外一种按照百分比进行抽样的方式,这种事基于行数的,按照输入路径下的数据块百分比进行的抽样。 hive (default)> select * from stu tablesample(0.1 percent); 提示:这种抽样方式不一定适用于所有的文件格式。另外,这种抽样的最小抽样单元是一个HDFS数据块。因此,如果表的数据大小小于普通的块大小128M的话,那么将会返回所有行。