[hive] ALTER TABLE 예시

hadoop 2016. 4. 19. 15:21






Hive의 ALTER TABLE 문은 많은 기능을 가진다.

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-AlterTable



단순한 컬럼 수정 뿐 아니라, 순서 변경, 추가, 완전 치환, 이름 변경 등의 기능을 가진다.



RENAME TO 절을 이용하여 이름을 변경한다. 



hive> CREATE TABLE users (`id` int, `location` string);

OK

hive> desc users;

OK

col_name data_type comment

id                   int

location             string




hive> ALTER TABLE users RENAME TO new_users;

OK

hive> desc new_users;

OK

col_name data_type comment

id                   int

location             string

Time taken: 0.036 seconds, Fetched: 2 row(s)




테이블 속성을 변경할 수 있다.


hive> ALTER TABLE users SET TBLPROPERTIES ('comment' = 'new user heros');

OK



컬럼을 추가할 수 있다.


hive> ALTER TABLE  users ADD COLUMNS (`work` string COMMENT 'comment');

OK

Time taken: 0.108 seconds

hive> desc users;

OK

col_name data_type comment

id                   int

location             string

work                 string               comment

Time taken: 0.104 seconds, Fetched: 3 row(s)





한 번에 다중 컬럼 추가 기능도 제공한다.


hive> ALTER TABLE  users ADD COLUMNS (`has_talk` string COMMENT 'checking whether user has talk', `json` string);

OK

Time taken: 0.041 seconds

hive> desc users;

OK

col_name data_type comment

id                   int

location             string

work                 string               comment

has_talk             string               checking whether user has talk

json                 string

Time taken: 0.036 seconds, Fetched: 5 row(s)





컬럼의 타입과 순서를 변경한다. 


hive> ALTER TABLE users CHANGE work work_name string AFTER json;

OK

Time taken: 0.047 seconds

hive> desc users;

OK

col_name data_type comment

id                   int

location             string

has_talk             string               checking whether user has talk

json                 string

work_name           string               comment

Time taken: 0.039 seconds, Fetched: 5 row(s)



컬럼을 삭제하기 위해 DROP 또는 DROP COLUMN 절을 사용하면 에러가 발생한다.

hive>ALTER TABLE users DROP COLUMN json;

error



따라서 DROP 대신 REPLACE 절을 이용하여 테이블 스키마를 수정한다.



hive> ALTER TABLE users REPLACE COLUMNS (`id` int, `location` string);

OK

Time taken: 0.071 seconds

hive> desc users;

OK

col_name data_type comment

id                   int

location             string

Time taken: 0.05 seconds, Fetched: 2 row(s)




Posted by '김용환'
,