Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a hive table without changing location?

Based on the Hive doc below:

Rename Table

ALTER TABLE table_name RENAME TO new_table_name;

This statement lets you change the name of a table to a different name.

As of version 0.6, a rename on a managed table moves its HDFS location as well. (Older Hive versions just renamed the table in the metastore without moving the HDFS location.)

Is there any way to rename a table without changing the location?

like image 218
Osiris Avatar asked Mar 12 '16 21:03

Osiris


People also ask

How do I change the default location of a Hive table?

Is it possible to change the default location of a managed table in Hive? Absolutely, by using the LOCATION keyword, we can change the default location of Managed tables while creating the managed table in Hive.

How do I change the path of a Hive table in HDFS?

An internal table is stored on HDFS in the /user/hive/warehouse directory which is its default storage location. This location can be changed by updating the path in the configuration file present in the config file – hive. metastore. warehouse.

How do I change the column name in Hive external table?

Refer to the command below: hive> ALTER TABLE <tablename> REPLACE COLUMNS (<old column name> INT, <new column name> STRING); This command will only change the schema of the table i.e., it will change the column name and the datatype but will not affect the data stored in the column.


1 Answers

Yeah we can do that. You just need to follow below three commands in sequence.

  1. Lets say you have a external table test_1 in hive. And you want to rename it test_2 which should point test_2 location not test_1. Then you need to convert this table into Managed table using below command. test_1 -> pointing to test_1 location

    ALTER TABLE db_name.test_1 SET TBLPROPERTIES('EXTERNAL'='FALSE'); 
  2. Rename the table name.

    ALTER TABLE db_name.test_1 RENAME TO db_name.test_2; 
  3. Again convert the managed table after renaming to external table.

    ALTER TABLE db_name.test_2 SET TBLPROPERTIES('EXTERNAL'='TRUE'); 

db_name.test_2 table will point the test_2 location. If we do it without making the managed table it will point the test_1 location.

like image 200
Sanjeev Avatar answered Sep 20 '22 22:09

Sanjeev