How to alter the type of the column in a Hive table? - Big Data In Real World

How to alter the type of the column in a Hive table?

What is the difference between reduceByKey and aggregateByKey in Spark?
April 19, 2021
How to fix “Could not locate executable winutils.exe” issue in Hadoop?
April 23, 2021
What is the difference between reduceByKey and aggregateByKey in Spark?
April 19, 2021
How to fix “Could not locate executable winutils.exe” issue in Hadoop?
April 23, 2021

Let’s say you created a table with one of the columns as STRING and after a couple of days you realized the column should actually be an INT. It is quite easy to alter the type of the column in Hive.

Solution

Let’s say the e_id column in the employee table is currently a STRING and we want to change it to an INT.

Use the ALTER command on the table with the CHANGE clause.

The ALTER command can also be used to change the column name. If you are not changing the column name, specify the same column name for both old and new column names.

Here we are not changing the column names so we specify e_id for both old and new column names followed by the data type.

ALTER TABLE <table-name> CHANGE <old-col-name> <new-col-name> <data-type>;
ALTER TABLE employee CHANGE e_id e_id INT;

Note that this changes the metadata of the table and not make any changes to the data.

Big Data In Real World
Big Data In Real World
We are a group of Big Data engineers who are passionate about Big Data and related Big Data technologies. We have designed, developed, deployed and maintained Big Data applications ranging from batch to real time streaming big data platforms. We have seen a wide range of real world big data problems, implemented some innovative and complex (or simple, depending on how you look at it) solutions.

Comments are closed.

How to alter the type of the column in a Hive table?
This website uses cookies to improve your experience. By using this website you agree to our Data Protection Policy.

Hadoop In Real World is now Big Data In Real World!

X