[HIVE] 데이터 정의 언어 (Data Definition Language) - 데이터베이스(database), 테이블(table), 컬럼(column)
Hive 2016. 6. 22. 01:26
-- HIVE : 데이터 정의 언어(DATA DEFINITION LANGUAGE)
--------------------------------------------------------------------
-- DATABASE
------------------
SHOW DATABASES;
SHOW DATABASES LIKE 'a.*'; -- show any database, starting with 'a' character
-- 데이터베이스 생성 (creating database)
CREATE DATABASE my_database;
CREATE DATABASE IF NOT EXISTS my_database_2;
CREATE DATABASE my_database_3
LOCATION '/another/directory'; -- changing directory
-- 테이블 삭제 (dropping table)
DROP TABLE IF EXISTS my_table;
DROP TABLE IF EXISTS my_table CASCADE; -- if there are tables in database
-- 데이터베이스 설명 추가, 설명 보기
CREATE DATABASE my_database_3
COMMENT 'this is my 3rd database'; -- adding comment for description
DESCRIBE DATABASE my_database_3;
-- 데이터베이스 키-값 속성 지정
CREATE DATABASE my_database_3
WITH DBPROPERTIES ('creator' = 'HongKilDong', 'date' = '2016-06-17');
DESCRIBE DATABASE EXTENDED my_database_3;
-- 작업 데이터베이스 설정
USE my_database_3;
-- 현재 데이터베이스 출력
set hive.cli.print.current.db = true;
set hive.cli.print.current.db = false;
-------------------
-- TABLE
-------------------
-- 테이블 조회
USE mydb;
SHOW TABLES;
SHOW TABLES LIKE 'a.*'; -- show any tables, starting with 'a' character
USE yourdb;
SHOW TABLES IN mydb; -- IN : when current database is different
-- 테이블 생성 (creating table)
CREATE TABLE IF NOT EXISTS mydb.my_table (
var_1 STRING COMMENT 'var_1 is this',
var_2 FLOAT COMMENT 'var_2 is that',
var_3 INT COMMENT 'var_3 is those',
var_4 ARRAY<STRING> COMMENT 'arrary is...',
var_5 MAP<STRING, FLOAT> COMMENT 'map is...',
var_6 STRUCT<aa:STRING, bb:FLOAT, cc:INT> COMMENT 'structure is...'
COMMENT 'my_table in mydb'
TBLPROPERTIES ('creator'='HongKilDong', 'data'='2016-06-17', ...)
--LOCATION '/user/hive/warehouse/mydb.db/my_table'
)
PARTITIONED BY (year INT, month INT, day INT);
-- 테이블 설명 자세히 보기
DESCRIBE EXTENDED mydb.my_table;
-- 테이블 파티션 조회
SHOW PARTITIONS my_table;
SHOW PARTITIONS my_table PARTITION(year = '2016', month = '6', day = '17');
DESCRIBE EXTENDED my_table; -- detailed table information with partition Keys
-- 테이블 내 특정 칼럼의 스키마 보기
DESCRIBE mydb.my_table.var_1;
-- 테이블 스키마 복사 (copying table schema)
CREATE TABLE IF NOT EXISTS mydb.my_table_copy
LIKE mydb.my_table;
-- 테이블명 변경 (changing table name)
ALTER TABLE my_table RENAME TO my_table_2
-- 테이블 파티션 추가 (adding table partition)
ALTER TABLE my_table ADD IF NOT EXISTS
PARTITION (year = 2016, month = 6, day = 15) LOCATION '/para/2016/06/15'
PARTITION (year = 2016, month = 6, day = 16) LOCATION '/para/2016/06/16'
PARTITION (year = 2016, month = 6, day = 17) LOCATION '/para/2016/06/17';
-- 테이블 파티션 위치 변경 (changing table partition's location)
ALTER TABLE my_table PARTITION(year = 2016, month = 6, day = 17)
SET LOCATION 's3n://bucket/para/2016/06/17';
-- 테이블 파티션 삭제 (dropping table partition)
ALTER TABLE my_table DROP IF EXISTS PARTITION(year = 2016, month = 6, day = 17);
--------------------
-- COLUMN
--------------------
-- 컬럼명 변경, 주석 변경, 변수 위치 변경
ALTER TABLE my_table
CHANGE COLUMN var_1 var_1_1 INT -- changing column name
COMMENT 'change var_1 to var_1_1 after after_var' -- adding comment
AFTER var_2; -- changing variable's location, after var_2
-- 컬럼 추가 (adding column)
ALTER TABLE my_table
ADD COLUMNS (
mean FLOAT COMMENT 'mean of variable',
std_dev FLOAT COMMENT 'standard deviation',
coef_vari FLOAT COMMENT 'coefficient of variation'
);
-- 컬럼 삭제 및 교체 (dropping columns, replacing columns)
ALTER TABLE my_table REPLACE COLUMNS (
var_1_1 INT COMMENT 'changing column name from var_1 to var_1_1',
var_3 STRING COMMENT 'replacing; keeping var_3, dropping var_2'
);
-- 테이블 속성 변경 (changing table property)
ALTER TABLE my_table SET TBLPROPERTIES (
'note' = 'put note here; blah-blah..'
);
-- 테이블 삭제 방지 (guaranteeing no drop)
ALTER TABLE my_table
PARTITION(year = 2016, month = 6, day = 17) ENABLE NO_DROP; -- opposite: DISABLE NO_DROP
-- 테이블 쿼리 방지 (blocking query)
ALTER TABLE my_table
PARTITION(year = 2016, month = 6, day = 17) ENABLE OFFLINE; -- opposite : DISABLE OFFLINE
[Reference]
- Programing Hive, Edward Capriolo, Dean Wampler, Jason Rutherglen, O'REILLY