分区表与分桶表

分区表:

在大数据中,最常用的一种思想就是分治,我们可以把大的文件切割划分成一个个的小的文件,这样每次操作一个小的文件就会很容易了,同样的道理,在hive当中也是支持这种思想的,就是我们可以把大的数据,按照每天,或者每小时进行切分成一个个的小的文件,这样去操作小的文件就会容易得多了

分区字段是一个虚拟的字段  不存放任何数据,分区表字段不能够在表中已经存在。

分区字段的数据来自于装载分区表数据的时候指定的

分区表的字段在hdfs上的效果就是在建立表的文件夹下面又创建了子文件,这样的目的把数据的划分更加细致 减少了查询时候全表扫描成本 只需要按照指定的分区扫描数据并显示结果即可

<-- 创建分区表语法 -->

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 '/export/servers/hivedatas/score.csv' into table score partition (month='201806');

<-- 加载数据到一个多分区的表中去 -->

load data local inpath '/export/servers/hivedatas/score.csv' into table score2 partition(year='2018',month='06',day='01');

<-- 多分区联合查询使用union  all来实现 -->

select * from score where month = '201806' union all select * from score where month = '201806';

<-- 查看分区 -->

show  partitions  score;

<-- 添加一个分区 -->

alter table score add partition(month='201805');

<-- 同时添加多个分区 -->

alter table score add partition(month='201804') partition(month = '201803');

<-- 注意:添加分区之后就可以在hdfs文件系统当中看到表下面多了一个文件夹 -->

<-- 删除分区 -->
alter table score drop partition(month = '201806');

 

分桶表

将数据按照指定的字段进行分成多个桶中去,说白了就是将数据按照字段进行划分,可以将数据按照字段划分到多个文件当中去

分桶表(分簇表)创建之前需要开启分桶功能,分桶表创建的时候分桶字段必须是表中已经存储的字段,也就是说你要按照表中那个字段进行分桶

针对分桶表的数据导入:load data方式不能够导成分桶表的数据,没有分桶效果,原因在于load 本质是哪个相当于 hive 去帮我们执行hadoop fs -put 

分桶表的数据采用insert+select 插入的数据来自于查询结果(查询时候执行了mr程序)对应mr当中的partitioner

默认分桶负责  按照你指定的分桶字段clustered by 哈希值与分桶的个数 set mapreduce.job.reduces 进行模运算取余

分桶表也是把表所映射的结构化数据文件分成更细致的部分 但是更多的是用在join 查询提高效率之上

只需要把join 的字段在各自表当中进行分桶操作即可

<-- 开启hive的桶表功能 -->

set hive.enforce.bucketing=true;

<-- 设置reduce的个数 -->

set mapreduce.job.reduces=3;

<-- 创建桶表 -->

create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format delimited fields terminated by '\t';

<-- 桶表的数据加载,只能通过insert  overwrite。hdfs  dfs  -put文件或者通过load  data无法加载

创建普通表,并通过insert  overwrite的方式将普通表的数据通过查询的方式加载到桶表当中去 -->

<-- 创建普通表: -->

create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by '\t';

<-- 普通表中加载数据 -->

load data local inpath '/export/servers/hivedatas/course.csv' into table course_common;

<-- 通过insert  overwrite给桶表中加载数据 -->

insert overwrite table course select * from course_common cluster by(c_id);

分区表和分桶表的优点,字段的要求

分区表:    优点是:提高查询效率      要求是:分区字段绝对不能出现在表已有的字段内。

分桶表:    优点是:提高join效率和用于数据取样。    要求是:分桶字段必须出现在表已有的字段内。

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值