Hive static partition table, detailed explanation of dynamic partition table, case demo

Hive static partition table, detailed explanation of dynamic partition table, case demo

Data text, student.txt

1	zhansgan	12	man
2	lisi	13	man
3	xiaohong	16	woman

Static partition: Assign data to a certain partition.
Create static partition table case column

#创建表
create table student(
id string,
name string,
age string,
sex string
)
PARTITIONED BY(student_age string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'; 
#加载数据
load data local inpath '/root/student.txt' overwrite into table student0
PARTITION  (student_age='12');

The file storage format on the static partition table hdfs is as follows:
Insert picture description here
Insert picture description here Dynamic partition: According to the value of one or several fields of the data, the data is dynamically divided into a certain partition to
create a dynamic partition table. Case 1: When multiple partition fields are used, all are implemented Dynamic partition to insert data

#开启动态分区
set hive.exec.dynamic.partition=true;
#设置为非严格模式 
set hive.exec.dynamic.partition.mode=nonstrict;
#创建元表,导数据到动态分区表用
create table student0(
id string,
name string,
age_partition string,
sex_partition  string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
#导入数据
load data local inpath '/root/student.txt' overwrite into table student0;


#创建动态分区表,分区字段sex_partition ,age_partition。
#注意:分区字段sex_partition ,age_partition, 
#必须在student0表中,但不能在student2表中,不然会导入数据失败。
create table student2(
id2 string,
name2 string,
age2 string,
sex2 string
)
PARTITIONED BY(sex_partition string,age_partition string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'; 

#从student0导入数据到student2
#注意:前四个字段是studet0中字段,后两个字段是分区字段
insert into table student2 PARTITION (sex_partition,age_partition)
select id,name,age_partition,sex_partition,sex_partition,age_partition from student0;

#查询数据
select * from student2 where sex_partition='man' and age_partition='12';

The file storage format on the dynamic partition table hdfs is as follows:
Insert picture description hereInsert picture description hereInsert picture description hereCreate a dynamic partition table case 2: When multiple partition fields, realize semi-automatic partitioning (some fields are statically partitioned, note that the static partition field must be in front of the dynamic)

#从student0导入数据到student2
insert into table student2 PARTITION (sex_partition='man',age_partition)
select id,name,age_partition,sex_partition,age_partition from student0;

The file storage format on the dynamic partition table hdfs is as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43614067/article/details/108637441