1. 排序
1.1 Order By 全局排序
关注点: 只有一个reducer ,也就是只有一个分区。
1.2 Sort By Reducer内部排序,分区内排序
- 关注点: 有多个reducer,也就是有多个分区
- 注意点: 有多个reducer,单独使用sort by ,数据会被随机分到每个reducer中,在每个reducer中,sort by 将数据排序。
set mapreduce.job.reduces; select * from epm order by detp desc;
insert overwrite local directory '/opt/module/hive/datas/sort-result/'
> select * from epm sort by dept desc;
[zjl@hadoop102 sort-result]$ cat 000000_0
6小七86财务部
8刘洋120财务部
12王刚120科技部
15任里300科技部
10成家龙210人事部
9环宇140人事部
[zjl@hadoop102 sort-result]$ cat 000001_0
13何科技220科技部
14原子弹230科技部
16屈独白250科技部
1zjl100\N
3李四200\N
4王五250\N
2张三120\N
[zjl@hadoop102 sort-result]$ cat 000002_0
7阿发150财务部
11陈踢踢121人事部
5赵六90\N
[zjl@hadoop102 sort-result]$
- sort by 不会单独使用
1.3 Distribute By 分区
- 关注点: 指定按照哪个字段分区
select * from epm distribute by dept sort by salary desc;
hive (default)> insert overwrite local directory '/opt/module/hive/datas/partition-result/' select * from epm distribute by dept sort by salary desc;
[zjl@hadoop102 partition-result]$ cat 000000_0
4王五250\N
10成家龙210人事部
3李四200\N
9环宇140人事部
11陈踢踢121人事部
2张三120\N
1zjl100\N
5赵六90\N
[zjl@hadoop102 partition-result]$ cat 000001_0
15任里300科技部
16屈独白250科技部
14原子弹230科技部
13何科技220科技部
12王刚120科技部
[zjl@hadoop102 partition-result]$ cat 000002_0
7阿发150财务部
8刘洋120财务部
6小七86财务部
[zjl@hadoop102 partition-result]$
1.4 Cluster By 分区排序
- 关注点:相当于distribute by 和 sort by 同时用,并且分区和排序的字段是同一个,并且排序是升序的情况。
select * from epm distribute by dept sort by dept asc;
select * from epm cluster by dept ;
2. 分区表
2.1 问题:Hive没有索引的概念,会暴力扫描整个数据。
2.2 本质:Hive的分区表,实际就是分目录,通过多个目录维护整个数据。
2.3 创建分区表(通过dept数据模拟日志数据)
- dept_20200401.log
- dept_20200402.log
- dept_20200403.log
-- 创建分区表
create table dept_partition (
deptno int, dname string , loc string
)
partitioned by (day string) -- 指定表的分区字段是day,该字段的类型是string
row format delimited fields terminated by '\t';
-- 导入本地磁盘数据
load data local inpath '/opt/module/hive/datas/dept/dept_20200401.log' into table dept_partition partition(day='20200401');
load data local inpath '/opt/module/hive/datas/dept/dept_20200402.log' into table dept_partition partition(day='20200402');
load data local inpath '/opt/module/hive/datas/dept/dept_20200403.log' into table dept_partition partition(day='20200403');
分区表查询
-- 查看分区表详情
hive (default)> desc formatted dept_partition;
2.4 分区表的分区的操作
1) 查看分区表有多少个分区
show partitions 表名。
hive (default)> show partitions dept_partition;
OK
partition
day=20200401
day=20200402
day=20200403
Time taken: 0.03 seconds, Fetched: 3 row(s)
`
2) 增加分区
增加单个分区:alter table dept_partition add partition(day='20200404');
增加多个分区:alter table dept_partition add partition(day='20200405') partition(day='20200406');
3) 删除分区
删除单个分区:
alter table dept_partition drop partition(day='20200404');
删除多个分区:
alter table dept_partition drop partition(day='20200405'), partition(day='20200406');
ps:注意删除时,多个partition之间加,号
2.5 二级分区
create table dept_partition2 (
deptno int, dname string , loc string
)
partitioned by (day string,hour string) -- 指定表的分区字段是day,该字段的类型是string
row format delimited fields terminated by '\t';
load data local inpath '/opt/module/hive/datas/dept/dept_20200401.log' into table dept_partition2 partition(day='20200401',hour='10');
load data local inpath '/opt/module/hive/datas/dept/dept_20200402.log' into table dept_partition2 partition(day='20200401',hour='11');
load data local inpath '/opt/module/hive/datas/dept/dept_20200403.log' into