Hive常用分区操作命令20180805

环境:hadoop-2.7 , hive 2.1

操作hive数据仓库中的表有个前提,就是被操作的表必须是有分区的。需要在建表的时候指定分区,具体如何创建有分区的表,请看我的另一篇文章 《在hive中创建分区表,再关联到hdfs有关位置,而不需导入数据到hive表》

在linux命令行对指定的表以循环方式插入分区

for d in “4 5”; do hive -e “ALTER TABLE machine_logs.uniform_id ADD IF NOT EXISTS PARTITION (dt=’2018-08-0$d’)”;echo —— ; done

Logging initialized using configuration in file:/etc/hive/2.5.0.0-1245/0/hive-log4j.properties
OK
Time taken: 3.087 seconds
------

显示执行成功了,但是我语法出问题,实际效果却是:

[root@kmr-40452c0f-gn-386277d7-master-1-001 ~]# hive -e "show partitions gz_logs.uniform_payment;"
… (省略)
dt=2018-07-29
dt=2018-07-30
dt=2018-07-31
dt=2018-08-01
dt=2018-08-02
dt=2018-08-03
dt=2018-08-04 5

在Linux命令行更正,使用DROP PARTITION

hive -e “ALTER TABLE gz_logs.uniform_payment DROP IF EXISTS PARTITION (dt=’2018-08-04 5’)”

Logging initialized using configuration in file:/etc/hive/2.5.0.0-1245/0/hive-log4j.properties
Dropped the partition dt=2018-08-04 5
OK
Time taken: 4.271 seconds
[root@kmr-40452c0f-gn-386277d7-master-1-001 ~]# hive -e "ALTER TABLE gz_logs.uniform_payment ADD IF NOT EXISTS PARTITION (dt='2018-08-04')"

Logging initialized using configuration in file:/etc/hive/2.5.0.0-1245/0/hive-log4j.properties
OK
Time taken: 2.624 seconds
[root@kmr-40452c0f-gn-386277d7-master-1-001 ~]# hive -e "ALTER TABLE gz_logs.uniform_payment ADD IF NOT EXISTS PARTITION (dt='2018-08-05')"

Logging initialized using configuration in file:/etc/hive/2.5.0.0-1245/0/hive-log4j.properties
OK
Time taken: 3.022 seconds

查看结果,正确✔️

…(省略)
dt=2018-07-29
dt=2018-07-30
dt=2018-07-31
dt=2018-08-01
dt=2018-08-02
dt=2018-08-03
dt=2018-08-04
dt=2018-08-05

批量修复表的历史分区

前面都是单独添加一个分区的,批量修复表的历史分区使用MSCK
在hive shell中使用MSCK REPAIRT TABLE db_name.table_name;
或者在Linux命令行使用hive –e “MSCK REPAIRT TABLE db_name.table_name;”

猜你喜欢

转载自blog.csdn.net/qq_31598113/article/details/81433041