Hive 分区的使用

📅 发布时间:2026/8/30 4:44:52
Hive 分区的使用 分区的作用为了提高sql的查询效率比如select * from orders where create_date20230826;假如数据量比较大这个sql就是全表扫描速度肯定慢。可以将数据按照天进行分区一个分区就是一个文件夹当你查询20230826的时候只需要去20230826这个文件夹中取数据即可不需要全表扫描提高了查询效率。总结几点1分区表实际上就是对应一个HDFS文件系统上的独立的文件夹。2该文件夹下是该分区所有的数据文件。3Hive中的分区就是分目录把一个大的数据集根据业务需要分割成小的数据集。4在查询时通过WHERE子句中的表达式选择查询所需要的指定的分区这样的查询效率会提高很多如何创建分区根据业务需求而定不过通常以年、月、日、小时、地区等进行分区启动以日为单位的存储最为常见create table tableName( ....... ....... ) partitioned by (colName colType [comment ...],...)一级分区【分区字段只有1个】create table if not exists part1( id int, name string, age int ) partitioned by (dt string) row format delimited fields terminated by , lines terminated by \n;注意 dt字段不在普通字段里面是一个伪列但是可以当做普通字段使用。搞两份数据user1.txt 和 user2.txt1,zhangsan,21 2,lisi,25 3,wangwu,334,zhaoliu,38 5,laoyan,36 6,xiaoqian,12加载数据建表的时候有ed,不建表的时候的sql不加ed.load data local inpath /home/hivedata/user1.txt into table part1 partition (dt2026-03-18); load data local inpath /home/hivedata/user2.txt into table part1 partition (dt2026-03-19);查看数据发现分区字段列也查询出来了。对应hdfs的目录文件以上创建的是一级分区只有一个分区字段但是有两个分区 dt20230825 和 dt20230826二级分区【分区字段有2个】create table if not exists part2( id int, name string, age int ) partitioned by (year string,month string) row format delimited fields terminated by ,;load data local inpath /home/hivedata/user1.txt into table part2 partition(year2023,month03); load data local inpath /home/hivedata/user2.txt into table part2 partition(year2023,month04); load data local inpath /home/hivedata/user2.txt into table part2 partition(year2023,month05);这里需要注意分区值加引号和不加引号的区别三级分区【分区字段有3个】create table if not exists part3( id int, name string, age int ) partitioned by (year string,month string,day string) row format delimited fields terminated by ,;加载数据load data local inpath /home/hivedata/user1.txt into table part3 partition(year2023,month08,day01); load data local inpath /home/hivedata/user3.txt into table part3 partition(year2023,month08,day31);hdfs对应的文件夹注意创建了某个分区之后除了在 hdfs 上创建了与之对应的文件夹mysql 中的元数据其实也做了新增操作如图所示注意分区字段不区分大小写分区值会区分如下图所示load data local inpath /home/hivedata/user1.txt into table part4 partition(year2018,month03,DAy21); load data local inpath /home/hivedata/user2.txt into table part4 partition(year2018,month03,dayAA);分区数据的查询单个分区查询select * from part1 where dt2026-03-18;查询多个分区select * from part1 where dt2026-03-18 union select * from part1 where dt2026-03-19; 使用union 整个SQL语句会转换成MR任务导致速度很慢 而以下两个sql没有进行MR任务。所以推荐下面的写法 select * from part1 where dt2026-03-18 or dt2026-03-19; select * from part1 where dt in(2026-03-18,2026-03-19);查看分区语法 show partitions tableName 例如: show partitions part3;分区和分区字段的区别分区比如year2018/month03/day21 这是一个分区上面目前是2个分区分区字段创建表的时候有多少个分区字段就是多少级分区。上面是3个分区字段添加分区添加分区不带数据-- 单个分区 alter table part3 add partition(year2025,month03,day08); -- 多个分区 alter table part3 add partition(year2025,month03,day09) partition(year2025,month03,day10); 一下子添加多个分区partition 之间没有符号添加分区并且带数据单分区带数据alter table part3 add partition(year2023,month05,day05) location /user/hive/warehouse/db03.db/part1/dt2026-03-18;多分区带数据alter table part3 add partition(year2020,month05,day06) location /user/hive/warehouse/db03/part1/dt2026-03-18 partition(year2020,month05,day07) location /user/hive/warehouse/db03/part1/dt2026-03-19;删除分区删除一个分区 alter table part3 drop partition(year2020,month05,day06); 删除多个分区中间有逗号 alter table part3 drop partition(year2020,month05,day06),partition(year2020,month05,day07);让分区关联数据的三种方式【重点】方式一上传数据后修复创建表和分区create table if not exists db03.part5( id int, name string, age int ) partitioned by (year string,month string,day string) row format delimited fields terminated by ,;在hdfs上构建分区的文件夹下面命令在sql控制台运行dfs -mkdir -p /user/hive/warehouse/db03.db/part5/year2026/month03/day18;上传数据到指定的分区下dfs -put /home/hivedata/user1.txt /user/hive/warehouse/db03.db/part5/year2026/month03/day18;查询数据发现没有数据原因是partition的元数据没有在mysql中修复一下修复操作其实是在part5这个表的元数据中添加了分区的数据。msck repair table part5;再次测试,发现就有数据了select * from part5;方式二上传数据后添加分区先创建表create table if not exists db03.part5( id int, name string, age int ) partitioned by (year string,month string,day string) row format delimited fields terminated by ,;在hdfs上创建文件夹dfs -mkdir -p /user/hive/warehouse/db03.db/part5/year2026/month03/day19;上传数据dfs -put /home/hivedata/user1.txt /user/hive/warehouse/db03.db/part5/year2026/month03/day19;添加一个分区alter table db03.part5 add partition(year2026,month03,day19);这时查询有数据了先创建一个分区会不会产生文件夹呢会创建一个分区表会不会产生文件夹呢不会你也可以先创建分区在分区的文件夹里面上传数据alter table part5 add partition(year2026,month03,day20);添加分区之后就有了文件夹/user/hive/warehouse/part5/year2026/month03/day20在这个文件夹里面上传数据dfs -put /home/hivedata/user1.txt /user/hive/warehouse/part5/year2026/month03/day20;方式三load数据到分区先创建表create table if not exists db03.part5( id int, name string, age int ) partitioned by (year string,month string,day string) row format delimited fields terminated by ,;load数据到表对应的分区这种方式不用创建文件夹load数据到分区表也会自动创建的。load data local inpath /home/hivedata/user1.txt into table db03.part5 partition(year2026,month03,day21);分区的种类静态分区先创建分区再加载数据动态分区直接加载数据根据数据动态创建分区混合分区分区字段有静态的也有动态的。动态分区的玩法1和2必须执行3和4可以按需执行1、开启动态分区功能默认true开启set hive.exec.dynamic.partitiontrue;2、设置为非严格模式动态分区的模式默认strict表示必须指定至少一个分区为静态分区nonstrict模式表示允许所有的分区字段都可以使用动态分区。set hive.exec.dynamic.partition.modenonstrict;3、在所有执行MR的节点上最大一共可以创建多少个动态分区。默认1000set hive.exec.max.dynamic.partitions1000;4、在每个执行MR的节点上最大可以创建多少个动态分区。该参数需要根据实际的数据来设定。比如源数据中包含了一年的数据即day字段有365个值那么该参数就需要设置成大于365如果使用默认值100则会报错。set hive.exec.max.dynamic.partitions.pernode100;创建一个dongtai.txt添加如下数据10703007267488 usa 2014-05-01 10101043505096 usa 2014-05-01 10103043509747 china 2014-05-02 10103043501575 china 2014-05-02 10104043514061 china 2014-05-01接着按照需求创建动态分区表create table dy_part1 ( order_no string ) partitioned by(type String, time String) row format delimited fields terminated by \t;加载数据load data local inpath /home/hivedata/dongtai.txt into table dy_part1;查看分区发现分区被自动创建了这个就是动态分区的效果。导入数据可以使用load的方式也可以使用从一个普通表查询数据插入动态分区表创建普通表并加载数据到普通表create table temp_part ( order_no string, type String, time String ) row format delimited fields terminated by \t;load data local inpath /home/hivedata/dongtai.txt into table temp_part;查询普通表数据并插入到动态分区表insert overwrite table dy_part1 partition (type, time) select order_no, type, time from temp_part;查看分区show partitions dy_part1;思考order_no, type, order_time 能过换成*insert overwrite table dy_part1 partition (type, time) select * from temp_part;虽然没有报错但是不建议动态分区需要依赖于两个字段的数据这两个数据必须是最后两个而且必须数据要照应.也就是说不管select 有多少个字段最后两个字段必须照应否则有问题insert overwrite table dy_part1 partition (type, time) select order_no, type, order_time from temp_part;混合分区的玩法创建一个分区表create table dy_part2( id int, name string ) partitioned by (year string,month string,day string) row format delimited fields terminated by ,;创建普通表create table temp_part2( id int, name string, year string, month string, day string ) row format delimited fields terminated by ,;编写一个数据文件temp_part2.txt1,廉德枫,2019,06,25 2,刘浩(小),2019,06,25 3,王鑫,2019,06,25 5,张三,2019,06,26 6,张小三,2019,06,26 7,王小四,2019,06,27 8,夏流,2019,06,27加载数据到普通表中load data local inpath /home/hivedata/temp_part2.txt into table temp_part2;将普通表的数据动态加载到分区表错误用法insert into dy_part2 partition (year2019,month,day) select * from temp_part2;会报错FAILED: SemanticException [Error 10044]: Line 1:12 Cannot insert into target table because column number/types are different day: Table insclause-0 has 4 columns, but query has 5 columns.正确用法insert into dy_part2 partition (year2019,month,day) select id,name,month,day from temp_part2;查看分区show partitions dy_part2;