Hive 学习笔记 排序-分区-分桶-函数

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)]


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)]

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)]

分区表查询
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y0uUWPsx-1610636344276)(en-resource://database/2639:1)]

-- 查看分区表详情
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 
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值