How to rename a table in Hive?
Contents
Alter table statement in Hive
Alter table statement helps to change the structure of the table in Hive. It allows us to rename the table,add columns/partitions,rename columns/partitions and so on in Hive table.Hive versions prior to 0.6 just renamed the table in the metastore without moving the HDFS location. But the later version moves its HDFS location if you rename on a managed table.
Rename table syntax in Hive
1 |
ALTER TABLE <table_name> RENAME TO <new_table_name>; |
As mentioned in the syntax, mention your current table name after the ALTER TABLE command and specify the new table name after the RENAME TO command.
Example for Rename table in Hive
Let’s look at an example that uses the ALTER TABLE statement to rename the table. In this example, we will rename the prime_customer table to prime_customer_bkup in Hive.
Current table details in Hive
The prime_customer table has the below customer details in the test_db database.
This table is created as managed table in Hive. Lets see the structure of the table and its HDFS location before renaming the table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
hive> describe formatted prime_customer; OK # col_name data_type comment account_id int name varchar(50) country varchar(50) account_type varchar(50) # Detailed Table Information Database: test_db Owner: revisit_class CreateTime: Sat May 23 00:20:23 PDT 2020 LastAccessTime: UNKNOWN Protect Mode: None Retention: 0 Location: hdfs://test_env/apps/hive/warehouse/test_db.db/prime_customer Table Type: MANAGED_TABLE Time taken: 0.425 seconds, Fetched: 37 row(s) |
From the output of describe formatted command, we can see that the HDFS location of the table is hdfs://test_env/apps/hive/warehouse/test_db.db/prime_customer.
Renaming the Hive table
The Alter table statement has successfully renamed the table from prime_customer to prime_customer_bkup in Hive. Also it changed the HDFS location of the file from hdfs://test_env/apps/hive/warehouse/test_db.db/prime_customer to hdfs://test_env/apps/hive/warehouse/test_db.db/prime_customer_bkup.
1 2 3 4 5 6 7 8 9 |
# Detailed Table Information Database: test_db Owner: revisit_class CreateTime: Sat May 23 00:20:23 PDT 2020 LastAccessTime: UNKNOWN Protect Mode: None Retention: 0 Location: hdfs://test_env/apps/hive/warehouse/test_db.db/prime_customer_bkup Table Type: MANAGED_TABLE |
The old table prime_customer doesn’t exist in Hive and renamed table prime_cusomer_bkup has the records as shown below.
Recommended Articles
- Copy the data or table structure from one table to another in Hive
- Hive describe command to check the meta data of the Hive table