注:技术交流可以加我VX:k-loop,昵称:默读者。
1,首先在虚拟机的note-01的节点(也就是hive服务器所在的Linux电脑上)上创建一个user_partition.txt文件。
cd /root/hivefile
vim user_partition.txt
向user_partition.txt文件添加如下内容
1,tom,usa,newyork
2,james,usa,newyork
3,jon,usa,newyork
4,zhangsan,china,liaoning
5,lisi,china,liaoning
6,wangwu,china,jilin
7,zhaoba,china,jilin
2,在虚拟机的note-03节点(也就是hive的客户端)创建动态分区表
-- 创建普通表
create table user_tmp(id int ,name string,country string,province string)
row format delimited fields terminated by ',';
-- 将数据加载到普通表里
load data local inpath '/root/hivefile/user_partition.txt' into table user_tmp;
-- 查看是否开启动态 true 是 ,false 否
set hive.exec.dynamic.partition;
-- 查看动态分区模式 指定模式 严格strict 非严格模式nostrict
set hive.exec.dynamic.partition.mode;
--在使用动态分区功能之前 需要开启动态分区的功能并且指定模式
set hive.exec.dynamic.partition=true; --开启动态分区
set hive.exec.dynamic.partition.mode=nostrict; --指定模式 严格strict 非严格模式nostrict
-- 创建动态分区表
create table user_partition(id int ,name string) partitioned by (country string,province string)
row format delimited fields terminated by ',';
-- 将普通表里的数据添加到分区表里
insert into table user_partition(id,name) partition(country,province)
select id,name,country,province from user_tmp;
-- 查询动态分区表内容
select * from user_partition
至此动态分区表创建完毕。
此处有个知识点
-- 创建普通表
create table user_tmp(id int ,name string,country string,province string)
row format delimited fields terminated by ',';
-- 将数据加载到普通表里
load data local inpath '/root/hivefile/user_partition.txt' into table user_tmp;
-- 查看是否开启动态 true 是 ,false 否
set hive.exec.dynamic.partition;
-- 查看动态分区模式 指定模式 严格strict 非严格模式nostrict
set hive.exec.dynamic.partition.mode;
--在使用动态分区功能之前 需要开启动态分区的功能并且指定模式
set hive.exec.dynamic.partition=true; --开启动态分区
-- 如果设置成严格
set hive.exec.dynamic.partition.mode=strict; --指定模式 严格strict 非严格模式nostrict
-- 创建动态分区表
create table user_partition(id int ,name string) partitioned by (country string,province string)
row format delimited fields terminated by ',';
-- 将普通表里的数据添加到分区表里
insert into table user_partition(id,name) partition(country='china',province)
select id,name,country,province from user_tmp where country='china';
-- 如果设置成严格,插入分区表的时候,第一个分区必须指定一个值,第二个分区可以是动态的,也可以指定。
-- 查询动态分区表内容
select * from user_partition
3,批量删除分区表
-- 批量删除分区
alter table user_partition drop partition (country>='china');
-- 如果只想删除其中的一个分区
alter table user_partition drop partition (country='china');
-- 不支持的批量删除语句 错误示范如下
alter table user_partition drop partition (country in('china','newyouk');
4,创建静态分区表
-- 创建普通表
create table user_tmp(id int ,name string,country string,province string)
row format delimited fields terminated by ',';
-- 将数据加载到普通表里
load data local inpath '/root/hivefile/user_partition.txt' into table user_tmp;
-- 创建静态分区表
create table user_partition(id int ,name string) partitioned by (country string,province string)
row format delimited fields terminated by ',';
-- 将普通表里的数据添加到分区表里
insert into table user_partition(id,name) partition(country='china' and province='liaoning')
select id,name from user_tmp where country='china' and province='liaoning';
-- 查询静态分区表内容
select * from user_partition