HIVE的分区表和分桶表

时间:2021-05-04 23:23:27   收藏:0   阅读:49

分区表

hive可以转化成MR计算程序,当数据量多时,读取一整个目录下的所有文件来进行计算,因为数据量太大,所以就会变得特别慢。
在实际工作当中,我们一般有计算前一天的数据的需求,我们可以将前一天的数据放在一个文件夹下,专门来计算前一天的数据
hive的分区表大概也是通过分文件夹的形式,将每一天数据都分成一个文件夹,然后去查询数据的时候就可以查询一个文件夹下的数据,
减小数据范围,加快查询效率

create table score(s_id string,c_id string,s_score int) partitioned by (month string) row format delimited fields terminated by ‘\t‘;
create table score2(s_id string,c_id string,s_score int)
partitioned by (year string,month string,day string)
row format delimited fields terminated by ‘\t‘;
load data local inpath ‘/bigdata/logs/score.csv‘ into table score partition(month=‘201806‘);
show partitions score;
alter table score add partition(month=‘201805‘);
alter table score add partition(month=‘201804‘) partition(month=‘201803‘);
alter table score drop partition(month=‘201806‘);

分桶表

分桶是相对分区进行更细粒度的划分,hive表或分区表可进一步分桶
原理:将整个数据内容按照某列取hash值,对桶的个数取模的方式决定该条记录存放在哪个桶中;具有相同hash值的数据进入到同一个桶,形成同一个文件
eg:比如按照name属性分3个桶,就是对name属性值的hash值对3取模,按照取模结果对数据分桶。
作用:提高某些查询操作效率

set hive.enforce.bucketing=true;
set mapreduce.job.reduces=4;  ##分4个桶

##创建分桶表
create table user_buckets_demo(id int,name string)
clustered by(id) into 4 buckets
row format delimited fields terminated by ‘\t‘;

##创建普通表
create table user_demo(id int, name string)
row format delimited fields terminated by ‘\t‘;
cd /bigdata/logs/
vi user_bucket.txt


1 anzhulababy1
2 AngleBaby2
3 xiaoxuanfeng1
4 heixuanfeng1
5 jingmaoshu1
6 dongjingshu1
7 dianwanxiaozi1
8 aiguozhe1
load data local inpath ‘/bigdata/logs/user_bucket.txt‘ 
overwrite into table user_buckets_demo;

hive3.x版本之后,可以直接向分桶表中load数据,不需要通过查询普通表数据插入到分桶表中。所以hive3.x以下版本插入数据需要两步
加载数据到普通表中
load data local inpath ‘/bigdata/logs/user_bucket.txt‘ overwrite into table user_demo;
从普通表中查询数据插入到分桶表中
insert into table user_bucket_demo select * from user_demo;

分区和分桶表案例

原文:https://www.cnblogs.com/tenic/p/14730455.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!