前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hive-分区分桶操作

Hive-分区分桶操作

作者头像
栗筝i
发布2022-12-01 20:30:55
4030
发布2022-12-01 20:30:55
举报
文章被收录于专栏:迁移内容迁移内容

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

一、分区表操作

企业常见的分区规则:按天进行分区(一天一个分区)

1、创建分区表语法
代码语言:javascript
复制
create table score(s_id string,c_id string, s_score int) partitioned by (month string) row format delimited fields terminated by '\t';
2、创建一个表带多个分区
代码语言:javascript
复制
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';
3、加载数据到分区表中
代码语言:javascript
复制
load data local inpath '/export/servers/hivedatas/score.csv' into table score partition (month='201806');
4、加载数据到一个多分区的表中去
代码语言:javascript
复制
load data local inpath '/export/servers/hivedatas/score.csv' into table score2 partition(year='2018',month='06',day='01');
5、多分区联合查询使用union all来实现
代码语言:javascript
复制
select * from score where month = '201806' union all select * from score where month = '201806';
1
6、查看分区
代码语言:javascript
复制
show  partitions  score;
7、添加一个分区
代码语言:javascript
复制
alter table score add partition(month='201805');
8、同时添加多个分区
代码语言:javascript
复制
alter table score add partition(month='201804') partition(month = '201803');

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

9、删除分区
代码语言:javascript
复制
alter table score drop partition(month = '201806');

特别强调: 分区字段绝对不能出现在数据库表已有的字段中! 作用: 将数据按区域划分开,查询时不用扫描无关的数据,加快查询速度。

二、分桶表操作

是在已有的表结构之上新添加了特殊的结构。

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

1、开启hive的桶表功能
代码语言:javascript
复制
set hive.enforce.bucketing=true;
2、设置reduce的个数
代码语言:javascript
复制
set mapreduce.job.reduces=3;
3、创建桶表
代码语言:javascript
复制
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';

桶表的数据加载,由于通标的数据加载通过hdfs dfs -put文件或者通过load data均不好使,只能通过insert overwrite

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

4、 创建普通表
代码语言:javascript
复制
create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by '\t';
5、 普通表中加载数据
代码语言:javascript
复制
load data local inpath '/export/servers/hivedatas/course.csv' into table course_common;
6、通过insert overwrite给桶表中加载数据
代码语言:javascript
复制
insert overwrite table course select * from course_common cluster by(c_id);

特别强调: 分桶字段必须是表中的字段。

分桶逻辑: 对分桶字段求哈希值,用哈希值与分桶的数量取余,余几,这个数据就放在哪个桶内。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、分区表操作
  • 二、分桶表操作
相关产品与服务
大数据
全栈大数据产品,面向海量数据场景,帮助您 “智理无数,心中有数”!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档