hive数据模型之分区表
分区表:
有两种类型
静态分区:需要事先指定分区的条件,不是很灵活
根据员工的部门号建立分区
create table emp_part_1
(empno int,
ename string,
job string,
mgr int,
hiredate string,
sal int,
comm int)
partitioned by (deptno int)
row format delimited fields terminated by ',';
和普通表的区别就是把分区条件的字段写在了分区条件上 ;
插入数据使用具体值进行插入,需要7列就可以了
insert into table emp_part_1 partition(deptno=10) values(7499,'ALLEN','SALESMAN',7698,'1981/2/20',1600,300);
使用查询值插入语句,需要查询8列,最后一列是部门号
insert into table emp_part_1 partition(deptno=10) select empno,ename,job,mgr,hiredate,sal,comm from emp where deptno=10;
insert into table emp_part_1 partition(deptno=20) select empno,ename,job,mgr,hiredate,sal,comm from emp where deptno=20;
insert into table emp_part_1 partition(deptno=30) select empno,ename,job,mgr,hiredate,sal,comm from emp where deptno=30;
动态分区
启动动态分区:
set hive.exec.dynamic.partition =true;
默认false,表示是否开启动态分区功能
set hive.exec.dynamic.partition.mode = nonstrict;
默认strict,表示允许所有分区都是动态的,如果是strict必须有静态分区字段
(*)根据一个字段创建动态分区表
create table emp2
(empno int,ename string,sal int)
partitioned by (job string);
插入数据 insert into table emp2 select empno,ename,sal,job from emp;
注意:根据插入数据的最后一个字段动态建立分区。
(*)根据多个字段创建动态分区表
create table emp3
(empno int,ename string,sal int)
partitioned by (deptno int,job string);
插入数据 insert into table emp3 select empno,ename,sal,deptno,job from emp;
注意:根据插入数据的最后两个字段动态建立分区。
(*)结合静态分区和动态分区
create table emp4
(empno int,ename string,sal int)
partitioned by (deptno int,job string);
插入数据
insert into table emp4 partition(deptno=10,job) select empno,ename,sal,job from emp where deptno=10;