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;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pCFXJaMS-1610636344270)(en-resource://database/2633:1)]](https://i-blog.csdnimg.cn/blog_migrate/d126c5bcd7ea3e881ed72817a958fba6.png)
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;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZGXUGbok-1610636344273)(en-resource://database/2635:1)]](https://i-blog.csdnimg.cn/blog_migrate/b5089ed4da602d8cc0500b01e89d2669.png)
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');
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O1QzCH7K-1610636344274)(en-resource://database/2637:1)]](https://i-blog.csdnimg.cn/blog_migrate/484ff4b935f79133a49c01993e97ec99.png)
分区表查询
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y0uUWPsx-1610636344276)(en-resource://database/2639:1)]](https://i-blog.csdnimg.cn/blog_migrate/341221c2fc4b9f4a06aa7947cddd9429.png)
-- 查看分区表详情
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 table dept_partition2

本文详细介绍了Hive中的排序机制,包括Order By、Sort By、Distribute By和Cluster By的区别和使用场景。同时,深入探讨了分区表的创建、操作和优势,以及如何利用动态分区简化数据处理。此外,还讲解了分桶表的概念和创建方法,以及数据加载时的注意事项。最后,列举了Hive常用函数及其在实际问题中的应用,如行转列、列转行和窗口函数,帮助读者更好地理解和运用Hive进行大数据处理。
最低0.47元/天 解锁文章
6176





