hive----静态、动态分区表
分区概念:
分区------处理海量数据根据数据文件结构考虑分门别类保存(大化小),使用分区,创建分区表,提高查询效率
指定分区字段,分区字段被称为伪列。数据文件中没有和分区字段对应的数据
分区可以分为一级分区(分区字段是一个字段),二级分区(分区字段是二个)。。。。。。
分区可以是静态分区(导入数据时 ,指定分区字段的值)
动态分区(导入数据时,不需要指定分区字段的值,由系统根据数据文件的情况自行判断)。使用动态分区时,要注意:向动态分区表去导入数据的时,要先执行:set hive.exec.dynamic.partition.mode=nonstrict;把动态分区的模式改为非严格模式。
静态分区表
新建数据库:
create database part;

使用数据库:
use part;

创建分区表
create table tf_log(
ip string,
url string,
devuce string,
os string //真实列
)partitioned by (dt string) //分区字段是伪列
row format delimited
fields terminated by ',';

插入数据: (数据是和分区表的真实列对应的)
2023-7-23.txt
192.168.33.12,http://qqyinyue.com, zhangsan,ios
192.168.33.13,http://baidu.com, lisi,ios
192.168.33.14,http://zhihu.com, wnagwu,ios
192.168.33.15,http://apache.org.hadoop.com, zhangsi,anzhuo
192.168.33.16,http://hbase.com, zhangwu,anzhuo
192.168.33.17,http://zookeeper.com, liwu,ios
2023-7-24.txt
192.168.33.18,http://qqyinyue.com, zhangsan,ios
192.168.33.19,http://baidu.com, lisi,ios
192.168.33.20,http://zhihu.com, wnagwu,ios
192.168.33.21,http://apache.org.hadoop.com, zhangsi,anzhuo
192.168.33.22,http://hbase.com, zhangwu,anzhuo
192.168.33.23,http://zookeeper.com, liwu,ios
2023-7-25.txt
192.168.33.24,http://qqyinyue.com, zhangsan,ios
192.168.33.25,http://baidu.com, lisi,ios
192.168.33.26,http://zhihu.com, wnagwu,ios
192.168.33.27,http://apache.org.hadoop.com, zhangsi,anzhuo
192.168.33.28,http://hbase.com, zhangwu,anzhuo
192.168.33.29,http://zookeeper.com, liwu,ios

导入数据:
load data local inpath '/opt/datas/2023-7-23.txt' into table tf_log partition(dt='2023-7-23');
load data local inpath ''/opt/datas/2023-7-24.txt' into table tf_log partition(dt='2023-7-24');
load data local inpath ''/opt/datas/2023-7-25.txt' into table tf_log partition(dt='2023-7-25');

注意:
如果遇到空值,需修改原数据并且重新覆盖值

load data local inpath '/opt/datas/2023-7-24.txt' overwrite into table tf_log partition(dt='2023-7-24');

查询分区信息:
show partitions tf_log;

查询某个指定分区的数据:
select * from tf_log where dt='2023-7-23';
select * from tf_log where dt='2023-7-24';
select * from tf_log where dt='2023-7-25';



删除一个分区:
alter table tf_log drop partition(dt='2023-7-23');
添加分区:
load data local inpath '/opt/datas/2023-7-23.txt' into table tf_log partition(dt='2023-7-23');
alter table tf_log add partition(dt='2023-7-23');
动态分区表
新建动态分区表
CREATE TABLE IF NOT EXISTS student_info (
id INT,
name STRING,
gender STRING
)
PARTITIONED BY (class STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','

插入以下数据:
cd /opt/datas
vi 2025-10-16.txt
1001,张三,M,class_1
1002,李四,M,class_1
1003,王五,M,class_1
1004,赵六,M,class_1
1005,孙七,M,class_1
1006,周八,F,class_1
1007,吴九,F,class_1
1008,郑十,F,class_1
1009,钱一,F,class_1
1010,冯二,F,class_1
2001,陈一,M,class_2
2002,林二,M,class_2
2003,黄三,M,class_2
2004,刘四,M,class_2
2005,梁五,M,class_2
2006,谢六,F,class_2
2007,宋七,F,class_2
2008,唐八,F,class_2
2009,许九,F,class_2
2010,韩十,F,class_2
3001,曹一,M,class_3
3002,彭二,M,class_3
3003,曾三,M,class_3
3004,石四,M,class_3
3005,龙五,M,class_3
3006,叶六,F,class_3
3007,苏七,F,class_3
3008,卢八,F,class_3
3009,蒋九,F,class_3
3010,魏十,F,class_3
![]()
创建临时表
CREATE TABLE IF NOT EXISTS student_staging (
id INT,
name STRING,
gender STRING,
class STRING
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS TEXTFILE;

导入数据到临时表
LOAD DATA LOCAL INPATH '/opt/datas/2025-10-16.txt' overwrite into table student_staging;

将动态分区的模式改为非严格模式:
set hive.exec.dynamic.partition.mode=nonstrict;
将数据设为本地模式
set hive.exec.mode.local.auto=true;
![]()
插入数据到各个分区
INSERT OVERWRITE TABLE student_info PARTITION (class)
SELECT id, name, gender, class FROM student_staging;

查看所有数据
SELECT * FROM student_info;

查看分区
SHOW PARTITIONS student_info;

更多推荐

所有评论(0)