1、选择一张分区表(本次取的是内部表),把分区数据取到本地。
[root@hadoop001 hiveData]# hadoop fs -get /user/hive/warehouse/emp_dept_partition/deptno=30/000000_0 emp_dept_partition-deptno30 [root@hadoop001 hiveData]# ls dept emp_dept_partition-deptno30 emp.txt
2、删除该分区
hive> ALTER TABLE emp_dept_partition DROP IF EXISTS PARTITION(deptno=30); Dropped the partition deptno=30 OK Time taken: 0.652 seconds hive> select * from emp_dept_partition where deptno=30; OK Time taken: 0.507 seconds deptno=30分区对应的文件夹也不存在了。 [root@hadoop001 hiveData]# hadoop fs -ls /user/hive/warehouse/emp_dept_partition/ Found 2 items drwxr-xr-x - root supergroup 0 2018-01-08 20:59 /user/hive/warehouse/emp_dept_partition/deptno=10 drwxr-xr-x - root supergroup 0 2018-01-08 20:59 /user/hive/warehouse/emp_dept_partition/deptno=20
3、将deptno=30的数据从本地放回emp_dept_partition对应的hdfs路径
[root@hadoop001 hiveData]# hadoop fs -mkdir /user/hive/warehouse/emp_dept_partition/deptno=30 [root@hadoop001 hiveData]# hadoop fs -put emp_dept_partition-deptno30 /user/hive/warehouse/emp_dept_partition/deptno=30 [root@hadoop001 hiveData]# hadoop fs -cat /user/hive/warehouse/emp_dept_partition/deptno=30/emp_dept_partition-deptno30 7499 ALLEN SALESMAN 7698 1981/2/20 1600.0 300.0 7521 WARD SALESMAN 7698 1981/2/22 1250.0 500.0 7654 MARTIN SALESMAN 7698 1981/9/28 1250.0 1400.0 7698 BLAKE MANAGER 7839 1981/5/1 2850.0 \N 7844 TURNER SALESMAN 7698 1981/9/8 1500.0 0.0 7900 JAMES CLERK 7698 1981/12/3 950.0 \N [root@hadoop001 hiveData]#
4、检查分区表的数据,发现查询不到任何数据,原因是因为元数据还没添加
hive> select * from emp_dept_partition where deptno=30; OK Time taken: 0.116 seconds
5、刷新分区表信息
语法:ALTER TABLE table_name ADD [IF NOT EXISTS] PARTITION partition_spec [LOCATION 'location'][, PARTITION partition_spec [LOCATION 'location'], ...];
hive> ALTER TABLE emp_dept_partition ADD IF NOT EXISTS PARTITION (deptno=30); OK Time taken: 0.209 seconds hive> select * from emp_dept_partition where deptno=30; OK 7499 ALLEN SALESMAN 7698 1981/2/20 1600.0 300.0 30 7521 WARD SALESMAN 7698 1981/2/22 1250.0 500.0 30 7654 MARTIN SALESMAN 7698 1981/9/28 1250.0 1400.0 30 7698 BLAKE MANAGER 7839 1981/5/1 2850.0 NULL 30 7844 TURNER SALESMAN 7698 1981/9/8 1500.0 0.0 30 7900 JAMES CLERK 7698 1981/12/3 950.0 NULL 30 Time taken: 0.168 seconds, Fetched: 6 row(s)
总结:
直接建立分区表的分区的文件夹,并上传对应分区的数据文件后,这些数据都是手动添加的,所以mysql并无记录对应分区的元数据,所以hive不能查询对应的结果。
需要在hive刷新分区信息(说白了就是想存储元数据的数据库添加分区资料)。
【来自@若泽大数据】