一、静态分区
1、创建分区表
1 hive (default)> create table order_mulit_partition( 2 > order_number string, 3 > event_time string 4 > ) 5 > PARTITIONED BY(event_month string, step string) 6 > row format delimited fields terminated by ‘\t‘;
2、加载数据到分区表
1 load data local inpath ‘/opt/data/order_created.txt‘ overwrite into table order_mulit_partition PARTITION(event_month=‘201405‘, step=‘1‘);
order_created.txt内容如下
1 order_number event_time 2 10703007267488 2014-05-01 06:01:12.334+01 3 10101043505096 2014-05-01 07:28:12.342+01 4 10103043509747 2014-05-01 07:50:12.33+01 5 10103043501575 2014-05-01 09:27:12.33+01 6 10104043514061 2014-05-01 09:03:12.324+01
3、这种手动指定分区加载数据,就是常说的静态分区的使用。但是在日常工作中用的比较多的是动态分区。
二、动态分区
需求:按照不同部门作为分区导数据到目标表
以上需求如果用静态分区的话,数据量大你是不是很懵逼??所以这个需求一般采用动态分区来实现。
1、创建目标表
1 hive (default)> create table emp_dynamic_partition( 2 > empno int, 3 > ename string, 4 > job string, 5 > mgr int, 6 > hiredate string, 7 > sal double, 8 > comm double) 9 > PARTITIONED BY(deptno int) 10 > row format delimited fields terminated by ‘\t‘;
2、采用动态方式加载数据到目标表
加载之前先设置一下下面的参数
1 hive (default)> set hive.exec.dynamic.partition.mode=nonstrict
开始加载
1 insert into table emp_dynamic_partition partition(deptno) 2 select empno , ename , job , mgr , hiredate , sal , comm, deptno from emp;
上面加载数据方式并没有指定具体的分区,只是指出了分区字段。在select最后一个字段必须跟你的分区字段,这样就会自行根据deptno的value来分区。
3、验证一下
有值
1 hive (default)> select * from emp_dynamic_partition; 2 OK 3 emp_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.deptno 4 7782 CLARK MANAGER 7839 1981-6-9 2450.0 NULL 10 5 7839 KING PRESIDENT NULL 1981-11-17 5000.0 NULL 10 6 7934 MILLER CLERK 7782 1982-1-23 1300.0 NULL 10 7 7369 SMITH CLERK 7902 1980-12-17 800.0 NULL 20 8 7566 JONES MANAGER 7839 1981-4-2 2975.0 NULL 20 9 7788 SCOTT ANALYST 7566 1987-4-19 3000.0 NULL 20 10 7876 ADAMS CLERK 7788 1987-5-23 1100.0 NULL 20 11 7902 FORD ANALYST 7566 1981-12-3 3000.0 NULL 20 12 7499 ALLEN SALESMAN 7698 1981-2-20 1600.0 300.0 30 13 7521 WARD SALESMAN 7698 1981-2-22 1250.0 500.0 30 14 7654 MARTIN SALESMAN 7698 1981-9-28 1250.0 1400.0 30 15 7698 BLAKE MANAGER 7839 1981-5-1 2850.0 NULL 30 16 7844 TURNER SALESMAN 7698 1981-9-8 1500.0 0.0 30 17 7900 JAMES CLERK 7698 1981-12-3 950.0 NULL 30 18 8888 HIVE PROGRAM 7839 1988-1-23 10300.0 NULL NULL
有分区(自动分区)
1 hive (default)> show partitions emp_dynamic_partition; 2 OK 3 partition 4 deptno=10 5 deptno=20 6 deptno=30 7 deptno=__HIVE_DEFAULT_PARTITION__ 8 Time taken: 0.29 seconds, Fetched: 4 row(s)
4、emp表的具体你内容如下
1 hive (default)> select * from emp; 2 OK 3 emp.empno emp.ename emp.job emp.mgr emp.hiredate emp.sal emp.comm emp.deptno 4 7369 SMITH CLERK 7902 1980-12-17 800.0 NULL 20 5 7499 ALLEN SALESMAN 7698 1981-2-20 1600.0 300.0 30 6 7521 WARD SALESMAN 7698 1981-2-22 1250.0 500.0 30 7 7566 JONES MANAGER 7839 1981-4-2 2975.0 NULL 20 8 7654 MARTIN SALESMAN 7698 1981-9-28 1250.0 1400.0 30 9 7698 BLAKE MANAGER 7839 1981-5-1 2850.0 NULL 30 10 7782 CLARK MANAGER 7839 1981-6-9 2450.0 NULL 10 11 7788 SCOTT ANALYST 7566 1987-4-19 3000.0 NULL 20 12 7839 KING PRESIDENT NULL 1981-11-17 5000.0 NULL 10 13 7844 TURNER SALESMAN 7698 1981-9-8 1500.0 0.0 30 14 7876 ADAMS CLERK 7788 1987-5-23 1100.0 NULL 20 15 7900 JAMES CLERK 7698 1981-12-3 950.0 NULL 30 16 7902 FORD ANALYST 7566 1981-12-3 3000.0 NULL 20 17 7934 MILLER CLERK 7782 1982-1-23 1300.0 NULL 10 18 8888 HIVE PROGRAM 7839 1988-1-23 10300.0 NULL NULL
--------------------- 本文来自 A_ChunUnique 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/Gavin_chun/article/details/78174492
原文地址:https://www.cnblogs.com/shujuxiong/p/9712907.html
时间: 2024-10-12 15:23:09