大数据03--Hive03查询、分区表和分桶表

hdfs命令并不会修改元数据信息

查询

查询语句语法:
SELECT [ALL | DISTINCT] select_expr, select_expr, ...
FROM table_reference
[WHERE where_condition]
[GROUP BY col_list]
[ORDER BY col_list]
[CLUSTER BY col_list
| [DISTRIBUTE BY col_list] [SORT BY col_list]
]
[LIMIT number]
数据准备
(0)原始数据
dept:
10 ACCOUNTING 1700
20 RESEARCH 1800
30 SALES 1900
40 OPERATIONS 1700
emp
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
7566 JONES MANAGER 7839 1981-4-2 2975.00 20
7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30
7782 CLARK MANAGER 7839 1981-6-9 2450.00 10
7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20
7839 KING PRESIDENT 1981-11-17 5000.00 10
7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30
7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
7900 JAMES CLERK 7698 1981-12-3 950.00 30
7902 FORD ANALYST 7566 1981-12-3 3000.00 20
7934 MILLER CLERK 7782 1982-1-23 1300.00 10
(1)创建部门表
create table if not exists dept(
deptno int,
dname string,
loc int
)
row format delimited fields terminated by ' ';
(2)创建员工表
create table if not exists emp(
empno int,
ename string,
job string,
mgr int,
hiredate string, 
sal double, 
comm double,
deptno int)
row format delimited fields terminated by ' ';
(3)导入数据
load data local inpath '/opt/module/datas/dept.txt' into table dept;
load data local inpath '/opt/module/datas/emp.txt' into table emp;
各子句一般要分行写
常用函数
求总行数( count
hive (default)> select count(*) cnt from emp;
求工资的最大值( max
hive (default)> select max(sal) max_sal from emp;
求工资的最小值( min
hive (default)> select min(sal) min_sal from emp;
求工资的总和( sum
hive (default)> select sum(sal) sum_sal from emp;
求工资的平均值( avg
hive (default)> select avg(sal) avg_sal from emp;
Limit 语句
LIMIT 子句用于限制返回的行数
hive (default)> select * from emp limit 5;

WHERE 子句紧随 FROM 子句

比较运算符(Between/In/ Is Null

查询工资在 500 1000 的员工信息

hive (default)> select * from emp where sal between 500 and 1000;
查询 comm 为空的所有员工信息
hive (default)> select * from emp where comm is null;
查询工资是 1500 5000 的员工信息
hive (default)> select * from emp where sal IN (1500, 5000);
Like   选择条件可以包含字符或数字 :
% 代表零个或多个字符 ( 任意个字符 ) 。  _ 代表一个字符。
查找名字以 A 开头的员工信息
hive (default)> select * from emp where ename LIKE 'A%';
查找名字中第二个字母为 A 的员工信息
hive (default)> select * from emp where ename LIKE '_A%';

select子句中的别名的使用,在group by及后面可使用。

from子句中的表别名的使用,在where及后面可使用。

分组
Group By 语句
GROUP BY 语句通常会和聚合函数一起使用,按照一个或者多个列队结果进行分组,然后对每个组执行聚合操作
计算 emp 表每个部门的平均工资
hive (default)> select t.deptno, avg(t.sal) avg_sal from emp t group by t.deptno;
计算 emp 每个部门中每个岗位的最高薪水
hive (default)> select t.deptno, t.job, max(t.sal) max_sal from emp t group by t.deptno, t.job;

先按照部门,再按照工资

Having 语句
having where 不同点
(1) where 后面不能写分组函数,而 having 后面可以使用分组函数。
(2) having 只用于 group by 分组统计语句。
求每个部门的平均薪水大于 2000 的部门
hive (default)> select deptno, avg(sal) avg_sal from emp group by deptno having avg_sal > 2000;
Join 语句
内连接
只有进行连接的两个表中都存在与连接条件相匹配的数据才会被保留下来。
根据员工表和部门表中的部门编号相等,查询员工编号、员工名称和部门名称;
hive (default)> select e.empno, e.ename, d.deptno, d.dname 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;
满外连接 (Hive 特有的)
将会返回所有表中符合 WHERE 语句条件的所有记录。如果任一表的指定字段没有符合条件的值的话,那么就使用 NULL 值替代。
hive (default)> select e.empno, e.ename, d.deptno from emp e full join dept d on e.deptno = d.deptno;
多表连接
连接 n 个表,至少需要 n-1 个连接条件。例如:连接三个表,至少需要两个连接条件。
创建位置表
create table if not exists location(
loc int,
loc_name string
)
row format delimited fields terminated by ' ';
导入数据
hive (default)> load data local inpath '/opt/module/datas/location.txt' into table location;
多表连接查询
hive (default)>SELECT e.ename, d.dname, 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; 进行连接操作
Hive 总是按照从左到右的顺序执行的。
优化:当对 3 个或者更多表进行 join 连接时,如果每个 on 子句都使用相同的连接键的话,那么只会产生一个 MapReduce job
排序
全局排序( Order By)  只有一个 Reducer
ASC ascend : 升序(默认)      DESC( descend : 降序
查询员工信息按工资升序排列
hive (default)> select * from emp order by sal;
查询员工信息按工资降序排列
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;

每个 Reduce 内部排序( Sort By
Sort By :对于大规模的数据集 order by 的效率非常低。在很多情况下,并不需要全局排序,此时可以使用 sort by
Sort by 为每个 reducer 产生一个排序文件。每个 Reducer 内部进行排序,对全局结果集
来说不是排序。
1 )设置 reduce 个数
hive (default)> set mapreduce.job.reduces=3;
2 )查看设置 reduce 个数
hive (default)> set mapreduce.job.reduces;
3 )根据部门编号降序查看员工信息
hive (default)> select * from emp sort by deptno desc;
4 )将查询结果导入到文件中(按照部门编号降序排序)
hive (default)> insert overwrite local directory 
'/opt/module/data/sortby-result'
select * from emp sort by deptno desc;
分区( Distribute By ) 
在有些情况下,我们需要控制某个特定行应该到哪个 reducer ,通常是为了进行后续的聚集操作。distribute by 子句可以做这件事。 distribute by 类似 MR partition (自定义分区),进行分区, 结合 sort by 使用。
对于 distribute by 进行测试,一定要分配 reduce 进行处理,否则无法看到 distribute
by 的效果。
(1)先按照部门编号分区,再按照员工编号降序排序。
hive (default)> set mapreduce.job.reduces=3;
hive (default)> insert overwrite local directory 
'/opt/module/data/distribute-result' select * from emp distribute by 
deptno sort by empno desc;
Hive 要求 DISTRIBUTE BY 语句要写在 SORT BY 语句之前。
Cluster By
distribute by sorts by 字段相同时,可以使用 cluster by 方式。
cluster by 除了具有 distribute by 的功能外还兼具 sort by 的功能。但是排序 只能是升序排序, 不能指定排序规则为 ASC 或者 DESC
(1)以下两种写法等价
hive (default)> select * from emp cluster by deptno;
hive (default)> select * from emp distribute by deptno sort by deptno;
注意: 按照部门编号分区,不一定就是固定死的数值,可以是 20 号和 30 号部门分到一
个分区里面去。

分区表和分桶表

分区表    Hive 中的分区就是分目录     把一个大的数据集根据业务需要分割成小的数据集
在查询时通过 WHERE 子句中的表达式选择查询所需要的指定的分区。
分区的目的就是避免全表扫描
分区表基本操作
创建分区表语法    这里的是 partitioned
hive (default)> create table dept_partition(
              > deptno int, dname string, loc string
              > )
              > partitioned by (day string)
              > row format delimited fields terminated by ' ';
注意: 分区字段不能是表中已经存在的数据 ,可以将分区字段看作表的伪列。
加载数据到分区表中
hive (default)> load data local inpath 
              > '/opt/module/hive/data/dept_01.txt' into table dept_partition 
              > partition(day='20220918');

hive (default)> load data local inpath 
              > '/opt/module/hive/data/dept_02.txt' into table dept_partition 
              > partition(day='20220918');

hive (default)> load data local inpath 
              > '/opt/module/hive/data/dept_03.txt' into table dept_partition 
              > partition(day='20220919');

 按照时间分了两个区,但都是再同一张表里,这里的抬头是drw..,是目录的形式

 

 查询分区表中数据

单分区查询

select * from dept_partition where day='20220918';

多分区联合查询

select * from dept_partition where day='20220918' or day='20220919';

 增加分区

alter table dept_partition add partition(day='20220920');

同时创建多个分区

hive (default)> alter table dept_partition add partition(day='20220921') partition(day='20220922');

 删除分区

删除单个分区

alter table dept_partition drop partition(day='20220920');
同时删除多个分区
alter table dept_partition drop partition(day='20220921'), partition(day='20220922');

 查看分区表有多少分区

hive (default)> show partitions dept_partition;
查看分区表结构
desc formatted dept_partition;

 二级分区   也就是分成二级目录

创建二级分区表

hive (default)>  create table dept_partition2(
              >  deptno int, dname string, loc string
              >  )
              >  partitioned by (day string, hour string)
              >  row format delimited fields terminated by ' ';
加载数据到二级分区表中
hive (default)> load data local inpath 
              > '/opt/module/hive/data/dept_01.txt' into table
              > dept_partition2 partition(day='20220918', hour='12');

hive (default)> load data local inpath 
              > '/opt/module/hive/data/dept_02.txt' into table
              > dept_partition2 partition(day='20220918', hour='13');


load data local inpath 
'/opt/module/hive/data/dept_03.txt' into table
dept_partition2 partition(day='20220919', hour='12');

 

查询分区数据
hive (default)> select * from dept_partition2 where day='20220919' and hour='12';

 

把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式
(1)方式一:上传数据后修复
上传数据
dfs -mkdir -p /user/hive/warehouse/dept_partition2/day=20220920/hour=12;
dfs -put /opt/module/hive/data/dept_03.txt /user/hive/warehouse/dept_partition2/day=20220920/hour=12;

查不到数据 。Reason:创建的是分区表,再hdfs里面再创建dir然后put文件并不会显示,都没带分区信息查分区表怎么会显示呢。并且hdfs命令并不会修改元数据信息

执行修复命令

hive (default)> msck repair table dept_partition2;
OK
Partitions not in metastore:	dept_partition2:day=20220920/hour=12
Repair: Added partition to metastore dept_partition2:day=20220920/hour=12
Time taken: 0.222 seconds, Fetched: 2 row(s)

(2)方式二:上传数据后添加分区
上传数据
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/day=20220921/hour=12;
hive (default)> dfs -put /opt/module/hive/data/dept_02.txt /user/hive/warehouse/dept_partition2/day=20220921/hour=12;
执行添加分区
alter table dept_partition2 add partition(day='20220921',hour='12');

 

(3)方式三:创建文件夹后 load 数据到分区     load命令会修改元数据
创建目录
dfs -mkdir -p /user/hive/warehouse/dept_partition2/day=20220922/hour=13;
上传数据
load data local inpath '/opt/module/hive/data/dept_01.txt' into table dept_partition2 partition(day='20220922',hour='13');

 

分桶表     分区针对的是数据的存储路径;分桶针对的是数据文件。
创建分桶表    这里的clusterd by
create table stu_buck(id int, name string)
clustered by(id) 
into 4 buckets
row format delimited fields terminated by ' ';
查看表结构
desc formatted stu_buck;
Num Buckets: 4
导入数据到分桶表中, load 的方式
load data local inpath '/opt/module/hive/data/bucket_01.txt' into table stu_buck;

 

hive (default)>  select * from stu_buck;
OK
stu_buck.id	stu_buck.name
1016	ss16
1012	ss12
1008	ss8
1004	ss4
1009	ss9
1005	ss5
1001	ss1
1013	ss13
1010	ss10
1002	ss2
1006	ss6
1014	ss14
1003	ss3
1011	ss11
1007	ss7
1015	ss15
根据结果可知: Hive 的分桶采用对分桶字段的值进行哈希,然后除以桶的个数求余的方式决定该条记录存放在哪个桶当中
分桶表操作需要注意的事项 :
(1) reduce 的个数设置为 -1, Job 自行决定需要用多少个 reduce 或者 将 reduce 的个
设置为 大于等于分桶表的桶数
(2)从 hdfs load 数据到分桶表中,避免本地文件找不到问题
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值