当前位置:   article > 正文

Hive 分区表_hive 外表

hive 外表

分区表:

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

创建分区表语法

create table score(s_id string,c_id string, s_score int) partitioned by (month string) row format delimited fields terminated by '\t';
  • 1

创建一个表带多个分区

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';
  • 1

加载数据到分区表中

load data local inpath '/export/servers/hivedatas/score.csv' into table score partition (month='201806');
  • 1

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

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

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

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

查看分区

show  partitions  score;
  • 1

添加一个分区

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

同时添加多个分区

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

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

删除分区

alter table score drop partition(month = '201806');
  • 1

外部分区表综合练习:

需求描述:现在有一个文件score.csv文件,存放在集群的这个目录下/scoredatas/month=201806,这个文件每天都会生成,存放到对应的日期文件夹下面去,文件别人也需要公用,不能移动。需求,创建hive对应的表,并将数据加载到表中,进行数据统计分析,且删除表之后,数据不能删除

需求实现:
数据准备:

hdfs dfs -mkdir -p /scoredatas/month=201806
hdfs dfs -put score.csv /scoredatas/month=201806/
  • 1
  • 2

创建外部分区表,并指定文件数据存放目录

create external table score4(s_id string, c_id string,s_score int) partitioned by (month string) row format delimited fields terminated by '\t' location '/scoredatas';
  • 1

进行表的修复,就是建立表与数据文件之间的一个关系映射

msck  repair   table  score4;
  • 1

修复成功之后即可看到数据已经全部加载到表当中去了

select * from score4;
  • 1

第二种实现方式,上传数据之后手动添加分区即可

数据准备:

hdfs dfs -mkdir -p /scoredatas/month=201805
hdfs dfs -put score.csv /scoredatas/month=201805
  • 1
  • 2

修改表,进行手动添加方式

alter table score4 add partition(month='201805');
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/1005189
推荐阅读
相关标签
  

闽ICP备14008679号