Hive静态分区和动态分区

来源:互联网 发布:赵 和谐 家人 知乎 编辑:程序博客网 时间:2024/06/02 07:28

一、静态分区
1、创建分区表

hive (default)> create table order_mulit_partition(              > order_number string,              > event_time string              > )              > PARTITIONED BY(event_month string, step string)              > row format delimited fields terminated by '\t';

2、加载数据到分区表

 load data local inpath '/opt/data/order_created.txt' overwrite into table order_mulit_partition PARTITION(event_month='201405', step='1');

order_created.txt内容如下

 order_number           event_time 10703007267488  2014-05-01 06:01:12.334+0110101043505096  2014-05-01 07:28:12.342+0110103043509747  2014-05-01 07:50:12.33+0110103043501575  2014-05-01 09:27:12.33+0110104043514061  2014-05-01 09:03:12.324+01

3、这种手动指定分区加载数据,就是常说的静态分区的使用。但是在日常工作中用的比较多的是动态分区。

二、动态分区
需求:按照不同部门作为分区导数据到目标表
以上需求如果用静态分区的话,数据量大你是不是很懵逼??所以这个需求一般采用动态分区来实现。
1、创建目标表

hive (default)> create table emp_dynamic_partition(              > empno int,               > ename string,               > job string,               > mgr int,               > hiredate string,               > sal double,               > comm double)              > PARTITIONED BY(deptno int)              > row format delimited fields terminated by '\t';

2、采用动态方式加载数据到目标表
加载之前先设置一下下面的参数

hive (default)> set hive.exec.dynamic.partition.mode=nonstrict

开始加载

insert into table emp_dynamic_partition partition(deptno)select empno , ename , job , mgr , hiredate , sal , comm, deptno from emp;

上面加载数据方式并没有指定具体的分区,只是指出了分区字段。在select最后一个字段必须跟你的分区字段,这样就会自行根据deptno的value来分区。

3、验证一下
有值

hive (default)> select * from emp_dynamic_partition;OKemp_dynamic_partition.empno     emp_dynamic_partition.ename     emp_dynamic_partition.job       emp_dynamic_partition.mgr       emp_dynamic_partition.hiredate     emp_dynamic_partition.sal       emp_dynamic_partition.comm      emp_dynamic_partition.deptno7782    CLARK   MANAGER 7839    1981-6-9        2450.0  NULL    107839    KING    PRESIDENT       NULL    1981-11-17      5000.0  NULL    107934    MILLER  CLERK   7782    1982-1-23       1300.0  NULL    107369    SMITH   CLERK   7902    1980-12-17      800.0   NULL    207566    JONES   MANAGER 7839    1981-4-2        2975.0  NULL    207788    SCOTT   ANALYST 7566    1987-4-19       3000.0  NULL    207876    ADAMS   CLERK   7788    1987-5-23       1100.0  NULL    207902    FORD    ANALYST 7566    1981-12-3       3000.0  NULL    207499    ALLEN   SALESMAN        7698    1981-2-20       1600.0  300.0   307521    WARD    SALESMAN        7698    1981-2-22       1250.0  500.0   307654    MARTIN  SALESMAN        7698    1981-9-28       1250.0  1400.0  307698    BLAKE   MANAGER 7839    1981-5-1        2850.0  NULL    307844    TURNER  SALESMAN        7698    1981-9-8        1500.0  0.0     307900    JAMES   CLERK   7698    1981-12-3       950.0   NULL    308888    HIVE    PROGRAM 7839    1988-1-23       10300.0 NULL    NULL

有分区(自动分区)

hive (default)> show partitions emp_dynamic_partition;OKpartitiondeptno=10deptno=20deptno=30deptno=__HIVE_DEFAULT_PARTITION__Time taken: 0.29 seconds, Fetched: 4 row(s)

4、emp表的具体你内容如下

hive (default)> select * from emp;OKemp.empno       emp.ename       emp.job emp.mgr emp.hiredate    emp.sal emp.comm        emp.deptno7369    SMITH   CLERK   7902    1980-12-17      800.0   NULL    207499    ALLEN   SALESMAN        7698    1981-2-20       1600.0  300.0   307521    WARD    SALESMAN        7698    1981-2-22       1250.0  500.0   307566    JONES   MANAGER 7839    1981-4-2        2975.0  NULL    207654    MARTIN  SALESMAN        7698    1981-9-28       1250.0  1400.0  307698    BLAKE   MANAGER 7839    1981-5-1        2850.0  NULL    307782    CLARK   MANAGER 7839    1981-6-9        2450.0  NULL    107788    SCOTT   ANALYST 7566    1987-4-19       3000.0  NULL    207839    KING    PRESIDENT       NULL    1981-11-17      5000.0  NULL    107844    TURNER  SALESMAN        7698    1981-9-8        1500.0  0.0     307876    ADAMS   CLERK   7788    1987-5-23       1100.0  NULL    207900    JAMES   CLERK   7698    1981-12-3       950.0   NULL    307902    FORD    ANALYST 7566    1981-12-3       3000.0  NULL    207934    MILLER  CLERK   7782    1982-1-23       1300.0  NULL    108888    HIVE    PROGRAM 7839    1988-1-23       10300.0 NULL    NULL