I am working with hive version 0.9 and I need delete columns of a hive table. I have searched in several manuals of hive commands but I only I have found commands to version 0.14. Is possible to delete a column of a hive table in hive version 0.9? What is the command? Thanks.
6 Answers
We can’t simply drop a table column from a hive table using the below statement like sql.
ALTER TABLE tbl_name drop column column_name ---- it will not work.
So there is a shortcut to drop columns from a hive table.
Let’s say we have a hive table.
From this table I want to drop the column Dob. You can use the ALTER TABLE REPLACE statement to drop a column.
ALTER TABLE test_tbl REPLACE COLUMNS(ID STRING,NAME STRING,AGE STRING); you have to give the column names which you want to keep in the table

- 1,259
- 1
- 14
- 27
-
1@BalajiBoggaramRamanarayan What is the correct workaround? Please answer the question as you've left later people having no idea how to do what the original question asks. – Alan Nov 09 '17 at 18:13
There isn't a drop column or delete column in Hive.
A SELECT statement can take regex-based column specification in Hive releases prior to 0.13.0, or in 0.13.0 and later releases if the configuration property hive.support.quoted.identifiers is set to none.
That being said you could create a new table or view using the following:
drop table if exists database.table_name;
create table if not exists database.table_name as
select `(column_to_remove_1|...|column_to_remove_N)?+.+`
from database.some_table
where
...
;
This will create a table that has all the columns from some_table except the columns named column_to_remove_1, ... , to column_to_remove_N. You can also choose to create a view instead.

- 3,819
- 2
- 20
- 35
ALTER TABLE table_name REPLACE COLUMNS ( c1 int, c2 String);
NOTE: eliminate column from column list. It will keep matched columns and removed unmentioned columns from table schema.

- 23,310
- 7
- 53
- 72

- 73
- 7
we can not delete column from hive table . But droping a table(if its external) in hive and the recreating table(with column excluded) ,wont delete ur data . so what can u do is(if u dont have table structure) run this command :
show create table database_name.table_name;
Then you can copy it and edit it (with column eliminated).Afterwards you can do as per invoke the shell

- 20,106
- 8
- 49
- 101

- 349
- 9
- 22
-
-
@DashK: i updated answer.On dropping internal table data is deleted but its not the case in external table. – Ankit Agrahari Jul 29 '15 at 12:25
-
Could you elaborate on what you meant by external table? Is it a table that does not exist in Hive? – DashK Aug 12 '15 at 14:46
-
@DashK [http://stackoverflow.com/questions/17038414/difference-between-hive-internal-tables-and-external-tables] refer this link – Ankit Agrahari Aug 12 '15 at 16:11
table details are empid,name,dept,salary ,address. i want remove address column. Just write REPLACE COLUMNS like below query jdbc:hive2://> alter table employee replace columns(empid int, name string,dept string,salary int);

- 1
As mentioned before, you can't drop table using an alter statement.
Alter - replace is not guaranteed to work in all the cases.
I found the best answer for this here: https://stackoverflow.com/a/48921280/4385453

- 1,086
- 1
- 11
- 20