Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive Alter table change Column Name

I am trying to rename a columnName in Hive. Is there a way to rename column name in Hive .

tableA (column1 ,_c1,_c2) to tableA(column1,column2,column3) ??

like image 897
user2978621 Avatar asked Feb 26 '14 12:02

user2978621


People also ask

How do I rename a column in Hive table?

ALTER TABLE name DROP [COLUMN] column_name ALTER TABLE name CHANGE column_name new_name new_type ALTER TABLE name REPLACE COLUMNS (col_spec[, col_spec ...])

Can we change the column name of an existing table?

ALTER TABLE table_name RENAME TO new_table_name; Columns can be also be given new name with the use of ALTER TABLE. QUERY: Change the name of column NAME to FIRST_NAME in table Student.

How do you update columns in Hive?

You use the UPDATE statement to modify data already stored in an Apache Hive table. You construct an UPDATE statement using the following syntax: UPDATE tablename SET column = value [, column = value ...]


2 Answers

Change Column Name/Type/Position/Comment:

ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name]

Example:

CREATE TABLE test_change (a int, b int, c int);  // will change column a's name to a1 ALTER TABLE test_change CHANGE a a1 INT; 
like image 150
Remus Rusanu Avatar answered Nov 01 '22 01:11

Remus Rusanu


Command works only if "use" -command has been first used to define the database where working in. Table column renaming syntax using DATABASE.TABLE throws error and does not work. Version: HIVE 0.12.

EXAMPLE:

hive> ALTER TABLE databasename.tablename CHANGE old_column_name new_column_name;    MismatchedTokenException(49!=90)         at org.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken(BaseRecognizer.java:617)         at org.antlr.runtime.BaseRecognizer.match(BaseRecognizer.java:115)         at org.apache.hadoop.hive.ql.parse.HiveParser.alterStatementSuffixExchangePartition(HiveParser.java:11492)         ...  hive> use databasename;  hive> ALTER TABLE tablename CHANGE old_column_name new_column_name;  OK 
like image 21
user11788 Avatar answered Nov 01 '22 01:11

user11788