Hive分区入门到精通

什么是分区?

Hive 将表组织到分区中。它是一种根据分区列(如日期、城市和部门)的值将表划分为相关部分的方法。使用分区,可以轻松查询部分数据。

为什么要分区?

分区表查询时可以指定分区进行查询,这样查询范围就小,速度就快, 不需要扫描整个表格.

如何分区?

建表时要规定分区字段,还有字段类型.
load数据时要指定分区,此时这种属于静态分区

create table part1(
id int,
name string,
age int
)
partitioned by(dt string)
row format delimited
fields terminated by '\t';

load data local inpath '/root/data/user1.txt' into table part1 
partition(dt="2020-05-05");
load data local inpath '/root/data/user2.txt' into table part1 
partition(dt="2020-05-06");
1	liming	23
2	liubei	30
3	ziru	29
4	dufu	45

创建分区表后可以查看信息

hive> describe part1;
OK
id                      int
name                    string
age                     int
dt                      string

# Partition Information
# col_name              data_type               comment

dt                      string

查看数据,可以看到最后面多了一列,是表示分区的.

hive> select * from part1;
OK
1       liming  23      2020-05-05
2       liubei  30      2020-05-05
3       ziru    29      2020-05-05
4       dufu    45      2020-05-05
14      liuzong 89      2020-05-06
15      dengzong        90      2020-05-06

二级分区

也可以创建二级分区,就是分区字段有两个,方法差不多

create table part2(
    id int,
    name string,
    age int
)
partitioned by (year string,month string)
row format delimited
fields terminated by '\t';
load data local  inpath '/data/user1.txt' into table part2 partition (year="2020",month="5");
load data local  inpath '/data/user1.txt' into table part2 partition (year="2019",month="6");

验证一下

hive> describe part2;
OK
id                      int
name                    string
age                     int
year                    string
month                   string

# Partition Information
# col_name              data_type               comment

year                    string
month                   string
Time taken: 0.182 seconds, Fetched: 11 row(s)
hive> select * from part2;
OK
1       liming  23      2019    6
2       liubei  30      2019    6
3       ziru    29      2019    6
4       dufu    45      2019    6
1       liming  23      2020    5
2       liubei  30      2020    5
3       ziru    29      2020    5
4       dufu    45      2020    5

也可以这么看

hive> show partitions part2;
year=2019/month=6
year=2020/month=5

指定分区查询数据

其实和普通字段的用法没有区别!可以想象,如果数据量大的话,指定分区是可以提高查询速度的! 这也是为什么要分区的理由~

hive> select * from part2 where year=2020;
OK
1       liming  23      2020    5
2       liubei  30      2020    5
3       ziru    29      2020    5
4       dufu    45      2020    5
Time taken: 6.292 seconds, Fetched: 4 row(s)
hive> select * from part2 where month=6;
OK
1       liming  23      2019    6
2       liubei  30      2019    6
3       ziru    29      2019    6
4       dufu    45      2019    6

修改分区

比较麻烦,一般不折腾这个

增加分区

hive> alter table part2 add partition (year='2019',month='7');
Time taken: 0.772 seconds
hive> show partitions part2;
year=2019/month=6
year=2019/month=7
year=2020/month=5

删除分区

hive> alter table part2 drop partition (year='2019',month='7');
Dropped the partition year=2019/month=7
hive> show partitions part2;
year=2019/month=6
year=2020/month=5

分区表加载数据时可以不指定分区么?

不可以

hive> load data local  inpath '/data/user2.txt' into table part2;
FAILED: SemanticException [Error 10062]: Need to specify partition columns because the destination table is partitioned

可以用insert into向静态分区表插入数据么?

可以.但是注意字段的数目,分区的字段不能包括在内.比如

insert into  part2 partition(year='2019',month='7') select id,name,age from part2;

下面这种就是错误的写法

hive> insert into  part2 partition(year='2019',month='7') select *  from part2;
FAILED: SemanticException [Error 10044]: Line 1:13 Cannot insert into target table because column number/types are different ''7'': Table insclause-0 has 3 columns, but query has 5 columns.

加载数据时hive会去重么?

不会!hive会存储重复的数据!

hive可否删除某一行数据?

sorry,不支持!
要么全删,要没别删!

动态分区,下回分解

总结

  • 分区的目的是为了缩小查询范围,加快查询速度
  • 分区可以有多个字段,二级分区及以上
  • 加载数据时要指定分区
  • 分区可以增加,删除,修改等等
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值