标签:local drop union 注意 不能 log use esc 联合查询
1 4.6.1 分区表基本操作 2 1.引入分区表(需要根据日期对日志进行管理) 3 /user/hive/warehouse/log_partition/20170702/20170702.log 4 /user/hive/warehouse/log_partition/20170703/20170703.log 5 /user/hive/warehouse/log_partition/20170704/20170704.log 6 2.创建分区表语法 7 hive (default)> create table dept_partition( 8 deptno int, dname string, loc string 9 ) 10 partitioned by (month string) 11 row format delimited fields terminated by ‘\t‘; 12 注意:分区字段不能是表中已经存在的数据,可以将分区字段看作表的伪列。 13 3.加载数据到分区表中 14 hive (default)> load data local inpath ‘/opt/module/datas/dept.txt‘ into table default.dept_partition partition(month=‘201709‘); 15 hive (default)> load data local inpath ‘/opt/module/datas/dept.txt‘ into table default.dept_partition partition(month=‘201708‘); 16 hive (default)> load data local inpath ‘/opt/module/datas/dept.txt‘ into table default.dept_partition partition(month=‘201707’); 17 注意:分区表加载数据时,必须指定分区 18 19 图6-5 加载数据到分区表 20 21 图6-6 分区表 22 4.查询分区表中数据 23 单分区查询 24 hive (default)> select * from dept_partition where month=‘201709‘; 25 多分区联合查询 26 hive (default)> select * from dept_partition where month=‘201709‘ 27 union 28 select * from dept_partition where month=‘201708‘ 29 union 30 select * from dept_partition where month=‘201707‘; 31 32 _u3.deptno _u3.dname _u3.loc _u3.month 33 10 ACCOUNTING NEW YORK 201707 34 10 ACCOUNTING NEW YORK 201708 35 10 ACCOUNTING NEW YORK 201709 36 20 RESEARCH DALLAS 201707 37 20 RESEARCH DALLAS 201708 38 20 RESEARCH DALLAS 201709 39 30 SALES CHICAGO 201707 40 30 SALES CHICAGO 201708 41 30 SALES CHICAGO 201709 42 40 OPERATIONS BOSTON 201707 43 40 OPERATIONS BOSTON 201708 44 40 OPERATIONS BOSTON 201709 45 5.增加分区 46 创建单个分区 47 hive (default)> alter table dept_partition add partition(month=‘201706‘) ; 48 同时创建多个分区 49 hive (default)> alter table dept_partition add partition(month=‘201705‘) partition(month=‘201704‘); 50 6.删除分区 51 删除单个分区 52 hive (default)> alter table dept_partition drop partition (month=‘201704‘); 53 同时删除多个分区 54 hive (default)> alter table dept_partition drop partition (month=‘201705‘), partition (month=‘201706‘); 55 7.查看分区表有多少分区 56 hive> show partitions dept_partition; 57 8.查看分区表结构 58 hive> desc formatted dept_partition; 59 60 # Partition Information 61 # col_name data_type comment 62 month string 63 4.6.2 分区表注意事项 64 1.创建二级分区表 65 hive (default)> create table dept_partition2( 66 deptno int, dname string, loc string 67 ) 68 partitioned by (month string, day string) 69 row format delimited fields terminated by ‘\t‘; 70 2.正常的加载数据 71 (1)加载数据到二级分区表中 72 hive (default)> load data local inpath ‘/opt/module/datas/dept.txt‘ into table 73 default.dept_partition2 partition(month=‘201709‘, day=‘13‘); 74 (2)查询分区数据 75 hive (default)> select * from dept_partition2 where month=‘201709‘ and day=‘13‘; 76 3.把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式 77 (1)方式一:上传数据后修复 78 上传数据 79 hive (default)> dfs -mkdir -p 80 /user/hive/warehouse/dept_partition2/month=201709/day=12; 81 hive (default)> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201709/day=12; 82 查询数据(查询不到刚上传的数据) 83 hive (default)> select * from dept_partition2 where month=‘201709‘ and day=‘12‘; 84 执行修复命令 85 hive> msck repair table dept_partition2; 86 再次查询数据 87 hive (default)> select * from dept_partition2 where month=‘201709‘ and day=‘12‘; 88 (2)方式二:上传数据后添加分区 89 上传数据 90 hive (default)> dfs -mkdir -p 91 /user/hive/warehouse/dept_partition2/month=201709/day=11; 92 hive (default)> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201709/day=11; 93 执行添加分区 94 hive (default)> alter table dept_partition2 add partition(month=‘201709‘, 95 day=‘11‘); 96 查询数据 97 hive (default)> select * from dept_partition2 where month=‘201709‘ and day=‘11‘; 98 (3)方式三:创建文件夹后load数据到分区 99 创建目录 100 hive (default)> dfs -mkdir -p 101 /user/hive/warehouse/dept_partition2/month=201709/day=10; 102 上传数据 103 hive (default)> load data local inpath ‘/opt/module/datas/dept.txt‘ into table 104 dept_partition2 partition(month=‘201709‘,day=‘10‘); 105 查询数据 106 hive (default)> select * from dept_partition2 where month=‘201709‘ and day=‘10‘;
标签:local drop union 注意 不能 log use esc 联合查询
原文地址:https://www.cnblogs.com/xjqi/p/12694235.html