文章目录
五、查询(下)
4、Join语句
<1>、等值join
Hive支持通常的SQL JOIN语句,但是只支持等值连接,不支持非等值连接。
hive (default)> select e.empno, e.ename, d.deptno, d.dname from emp e join dept d on e.deptno = d.deptno;
<2>、表的别名
使用别名可以简化查询
使用表名前缀可以提高执行效率
hive (default)> select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;
<3>、内连接
内连接:只有进行连接的两个表中都存在与连接条件相匹配的数据才会被保留下来。
hive (default)> select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;
<4>、左外连接
左外连接:JOIN操作符左边表中符合WHERE子句的所有记录将会被返回。
hive (default)> select e.empno, e.ename, d.deptno from emp e left join dept d on e.deptno = d.deptno;
<5>、右外连接
右外连接:JOIN操作符右边表中符合WHERE子句的所有记录将会被返回。
hive (default)> select e.empno, e.ename, d.deptno from emp e right join dept d on e.deptno = d.deptno;
<6>、满外连接
满外连接:将会返回所有表中符合WHERE语句条件的所有记录。如果任一表的指定字段没有符合条件的值的话,那么就使用NULL值替代。
hive (default)> select e.empno, e.ename, d.deptno from emp e full join dept d on e.deptno = d.deptno;
<7>、多表连接
注意:连接 n个表,至少需要n-1个连接条件。例如:连接三个表,至少需要两个连接条件。
创建位置表
create table if not exists default.location(
loc int,
loc_name string
)
row format delimited fields terminated by '\t';
导入数据
hive (default)> load data local inpath '/opt/module/datas/location.txt' into table default.location;
多表连接查询
hive (default)>SELECT e.ename, d.deptno, l. loc_name
FROM emp e
JOIN dept d
ON d.deptno = e.deptno
JOIN location l
ON d.loc = l.loc;
大多数情况下,Hive会对每对JOIN连接对象启动一个MapReduce任务。本例中会首先启动一个MapReduce job对表e和表d进行连接操作,然后会再启动一个MapReduce job将第一个MapReduce job的输出和表l;进行连接操作。
注意:为什么不是表d和表l先进行连接操作呢?这是因为Hive总是按照从左到右的顺序执行的。
5、排序
<1>、全局排序(Order By)
ASC(ascend): 升序(默认)
DESC(descend): 降序
ORDER BY 子句在SELECT语句的结尾。
hive (default)> select * from emp order by sal desc;
<2>、按照别名排序
hive (default)> select ename, sal*2 twosal from emp order by twosal;
<3>、多个列排序
在order by后面加一个参数
hive (default)> select ename, deptno, sal from emp order by deptno, sal desc;
<4>、每个MapReduce内部排序(Sort By)
Sort By:每个MapReduce内部进行排序,对全局结果集来说不是排序
在设置reducer个数之前要先在hive-site.xml(也有可能叫hive-default.xml)中设置hive.exec.reducers.max也就是reducer的最大运行个数,默认最大为999,这个设置的时候挺玄学的,
设置的太小,数据量很大,会导致这个reduce异常的慢,从而导致这个任务不能结束,也有可能会宕机
设置的太大,产生的小文件太多,合并起来代价太高,NameNode的内存占用也会增大
设置reduce个数
hive (default)> set mapreduce.job.reduces=3;
查看设置reduce个数
hive (default)> set mapreduce.job.reduces;
根据部门降序查看员工信息
hive (default)> select * from emp sort by empno desc;
将查询结果导入到文件中(按照部门编号降序排序)
hive (default)> insert overwrite local directory '/opt/module/datas/sortby-result' select * from emp sort by deptno desc;
<5>、分区排序(Distribute By)
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;
<6>、Cluster By
当distribute by和sorts by字段相同时,可以使用cluster by方式。
cluster by除了具有distribute by的功能外还兼具sort by的功能。但是排序只能是倒序排序,不能指定排序规则为ASC或者DESC。
6、分桶及抽样查询
<1>、分桶表数据存储
- 分区针对的是数据的存储路径;分桶针对的是数据文件
- 分区提供一个隔离数据和优化查询的便利方式。不过,并非所有的数据集都可形成合力的分区,特别是之前所提到过的要确定合适的划分大小这个疑虑。
- 分桶是将数据集分解成更容易管理的若干部分的另一个技术。
(1)、先创建分桶表,通过直接导入数据文件的方式
1、创建分桶表
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、创建一个普通的表
create table stu(id int, name string) row format delimited fields terminated by '\t';
2、向表中导入数据
load data local inpath '/opt/module/datas/student.txt' into table stu;
3、清空stu_buck表中数据
truncate table stu_buck;
4、导入数据到分桶表,通过子查询的方式
insert into table stu_buck select id, name from stu cluster by(id);
5、发现还是只有一个分桶
6、需要设置一个属性
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);
7、查询分桶的数据
hive (default)> select * from stu_buck;
OK
stu_buck.id stu_buck.name
1001 ss1
1005 ss5
1009 ss9
1012 ss12
1016 ss16
1002 ss2
1006 ss6
1013 ss13
1003 ss3
1007 ss7
1010 ss10
1014 ss14
1004 ss4
1008 ss8
1011 ss11
1015 ss15
<2>、分桶抽样查询
对于非常大的数据集,有时用户需要使用的是一个具有代表性的查询结果而不是全部结果。Hive可以通过对表进行抽样来满足这个需求。
查询<1>中表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
<3>、数据块抽样
Hive提供了另外一种按照百分比进行抽样的方式,这种事基于行数的,按照输入路径下的数据块百分比进行的抽样。
hive (default)> select * from stu tablesample(0.1 percent);
提示:这种抽样方式不一定适用于所有的文件格式。另外,这种抽样的最小抽样单元是一个HDFS数据块。因此,如果表的数据大小小于普通的块大小128M的话,那么将会返回所有行。