hive的分区操作(一)

hive的分区操作

一、分区的原因作用

  1. 为什么分区

    Hive的Select查询时,一般会扫描整个表内容。随着系统运行的时间越来越长,表的数据量越来越大,而hive查询做全表扫描,会消耗很多时间,降低效率。而有时候,我们需求的数据只需要扫描表中的一部分数据即可。这样,hive在建表时引入了partition概念。即在建表时,将整个表存储在不同的子目录中,每一个子目录对应一个分区。在查询时,我们就可以指定分区查询,避免了hive做全表扫描,从而提高查询效率。

  2. 如何分区

    根据业务需求而定,不过通常以年、月、日、小时、地区等进行分区。

  3. 分区的技术

    partitioned by (colName colType [comment ‘…’],…)

    • hive的分区名不区分大小写,不支持中文(分区名不区分大小写但是分区的字段区分大小写)
    • hive的分区字段是一个伪字段,但是可以用来进行操作
    • 一张表可以有一个或者多个分区,并且分区下面也可以有一个或者多个分区。- 分区是以字段的形式在表结构中存在,通过describe table命令可以查看到字段存在,但是该字段不存放实际的数据内容,仅仅是分区的表示。
  4. 分区的意义
    让用户在做数据统计的时候缩小数据扫描的范围,在进行select操作时可以指定要统计哪个分区

  5. 分区的本质
    在表的目录或者是分区的目录下在创建目录,分区的目录名为指定字段=值

分区的案例

一级分区

create table if not exists part1(
	id int,
	name string,
	age int
	)
	partitioned by (dt string)
	row format delimited 
	fields terminated by '\t'
	lines terminated by '\n';
	分区数据的导入方式:
	load data local inpath ‘./data/user.txt’ into table part1 partition(dt='2018-03-20'),
	load data local inpath './data/user.txt' into table part1 partition(dt='2018-03-21');

二级分区

create table if not exists part2(
	id int,
	name string,
	age int
	)
	partitioned by (year string,month string)
	row format delimited fields terminated by '\t';
	加载数据
	
load data local inpath '/hivedata/user.txt' into table part2 partition(year='2018',month='03'); 
	load data local inpath '/hivedata/user.txt' into table part2 partition(year='2018',month=02);

三级分区

create table if not exists part3(
	id int,
	name string,
	age int
	)
	partitioned by (year string,month string,day string)
	row format delimited 
	fields terminated by '\t';
	加载数据
	load data local inpath '/hivedata/user.txt' into table part3 partition(year='2018',month='03',day='21');​load data local inpath '/hivedata/user.txt' into table part3 partition(year='2018',month='02',day='20');  

查看分区 show partition tablename;

增加分区并且设置数据

1.新增分区(空)
alter table part1 add partition(dt='2018-03-27');
2.新增分区(带数据)
alter table part5 add partition(dt='2018-03-27') location '/user/hive/warehouse/mydb1.db/part1/dt=2018-03-20';
3.新增多分区
alter table part5 add partition(dt='2018-03-26') location '/user/hive/warehouse/mydb1.db/part1/dt=2018-03-20'
	partition(dt='2018-03-24') location '/user/hive/warehouse/qf1704.db/part1/dt=2018-03-21';

8. 删除分区

  1. 删除单个分区
    alter table part5 drop partition(dt=‘2018-03-21’);
  2. 删除多个分区
    alter table part5 drop partition(dt=‘2018-03-24’),partition(dt=‘2018-03-26’);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值