天天看點

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;

hive資料模型之分區表
hive資料模型之分區表

 動态分區

啟動動态分區:

 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;

繼續閱讀