hive的分区表

分区表

分区表实际上就是对应一个 HDFS 文件系统上的独立的文件夹,该文件夹下是该分区
所有的数据文件。Hive 中的分区就是分目录,把一个大的数据集根据业务需要分割成小的
数据集。在查询时通过 WHERE 子句中的表达式选择查询所需要的指定的分区,这样的查
询效率会提高很多。

分期表的基本操作

  1. 引入分区表(需要根据日期对日志进行管理)
/data/hive/warehouse/log_partition/20170702/20170702.log
/data/hive/warehouse/log_partition/20170703/20170703.log
/data/hive/warehouse/log_partition/20170704/20170704.log
  1. 创建分区表语法
hive(default)>create table sudent(
id int,name string,score bigint)
partitioned by (month string)
row format delimited
fields terminated by '\t';
  1. 加载数据到分区表中
hive (default)> load data local inpath 
'/root/data/student.txt' into table student
partition(month='20201022');
hive (default)> load data local inpath 
'/root/data/student.txt' into table student
partition(month='20201023');
hive (default)> load data local inpath 
'/root/data/student.txt' into table student
partition(month='20201024');
  1. 查询分区表中数据
hive (default)> select * from student where month='20201024';
  • 多分区联合查询
hive (default)> select * from student where 
month='20201022'
 union
 select * from student where month='20201023'
 union
 select * from student where month='20201024';

5.增加分区

  • 创建单个分区
hive (default)> alter table student add partition(month='20201025');
  • 同时创建多个分区(添加多个分区表的分割符号是空格)
hive (default)> alter table student add 
partition(month='20201026') partition(month='20201027');

6.删除分区

  • 删除单个分区
hive (default)> alter table student drop partition 
(month='20201027');
  • 删除多个分区中间用逗号隔开(删除分区表和添加的不一样,删除多个是逗号分割)
hive(default)> alter table student drop partition(month='20201026'),partition(month='20201027');

7.查看分区表有多少分区

hive(default)>show partitions student;

8.查看分区表结构

hive> desc formatted student;

分区表注意事项

1.创建二级分区表

hive(default)>create table student_partition(
id int,name string,score bigint)
partitioned by (month string,day string)
row format delimited
fields terminated by '\t';

2.正常的加载数据

  • 加载数据到二级分区
hive (default)> load data local inpath 
'/root/data/student.txt' into table
student_partition partition(month='202010', day='24');
  • 查询分区数据
hive (default)> select * from student_partition where 
month='202010' and day='24';

把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式

  • 方式一:上传数据后修复

上传数据

hive (default)> dfs -mkdir -p
/home/hive/warehouse/student_partition/month=202010/day=24;
hive (default)> dfs -put /opt/module/datas/dept.txt 
/home/hive/warehouse/student_partition/month=202010/day=24;

查询数据(查询不到刚上传的数据)

hive (default)> select * from student_partition where 
month='202010' and day='24';

执行修复命令(修复命令会修复hive表里的元素据,能够让分区的元数据信息直接指向hdfs的存储目录)

hive> msck repair table student_partition ;
  • 方式二:上传数据后添加分区
    上传数据
hive (default)> dfs -mkdir -p
/home/hive/warehouse/student_partition/month=202010/day=25;
hive (default)> dfs -put /root/data/student.txt 
/home/hive/warehouse/student_partition/month=202010/day=25;

执行添加分区(添加分区后会把表的元数据信息直接指向hdfs的分区目录)

hive (default)> alter table student_partition add 
partition(month='202010',
day='25');

查询数据

hive (default)> select * from dept_partition2 where 
month='202010' and day='25';
  • 方式三:创建文件夹后 load 数据到分区(load方法可以直接加载数据并且添加hive表里的元数据信息)

创建目录

hive (default)> dfs -mkdir -p
/home/hive/warehouse/student_partition/month=202010/day=26;

上传数据

hive (default)> load data local inpath 
'/root/data/student.txt' into table
student_partition partition(month='202010',day='26');

查询数据

hive (default)> select * from dept_partition2 where 
month='202010' and day='26';

猜你喜欢

转载自blog.csdn.net/m0_49092046/article/details/109256724