且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Hive中重命名分区值?

更新时间:2023-01-22 16:17:45

旧版本的Hive带有重命名分区的问题.对于您的情况,这也可能是一个问题.请参阅此链接详细信息.

There was an issue with old version of Hive with renaming partition. This might be an issue for your case too. Please see this link for detail.

如果使用的是较旧版本的Hive,则在执行重命名分区命令之前,需要设置以下两个属性.

You need to set below two property before executing the rename partition command if you are using Older version of Hive.

set fs.hdfs.impl.disable.cache=false; 
set fs.file.impl.disable.cache=false; 

现在通过设置此属性来运行查询.

Now run the query by setting this property.

hive> set fs.hdfs.impl.disable.cache=false;
hive> set  fs.file.impl.disable.cache=false;
hive> ALTER TABLE partition_test PARTITION (year='2016',day='1') RENAME TO PARTITION (year='2016',day='01');
OK
Time taken: 0.28 seconds
hive> show partitions partition_test;
OK
year=2016/day=01
Time taken: 0.091 seconds, Fetched: 1 row(s)
hive>

此问题已在Hive最新版本中修复.在我的情况下,Hive版本为1.2.1,并且无需设置该属性即可工作.请参见下面的示例.

This issue is fixed in Hive latest version. In my case Hive version is 1.2.1 and it works, without setting that property. Please see the example below.

创建分区表.

hive> create table partition_test(
    > name string,
    > age int)
    > partitioned by (year string, day string);
OK
Time taken: 5.35 seconds
hive> 

现在添加分区并检查新添加的分区.

Now add the partition and check the newly added partition.

hive> alter table partition_test ADD PARTITION (year='2016', day='1');
OK
Time taken: 0.137 seconds
hive>


hive> show partitions partition_test;
OK
year=2016/day=1
Time taken: 0.169 seconds, Fetched: 1 row(s)
hive>

使用 RENAME TO PARTITION 命令重命名分区并进行检查.

Rename the partition using RENAME TO PARTITION command and check it.

hive> ALTER TABLE partition_test PARTITION (year='2016',day='1') RENAME TO PARTITION (year='2016',day='01');
OK
Time taken: 0.28 seconds
hive> show partitions partition_test;
OK
year=2016/day=01
Time taken: 0.091 seconds, Fetched: 1 row(s)
hive>

希望它对您有帮助.