1.Database
SHOW databases;
USE database_name;
CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name
[COMMENT database_comment]
[LOCATION hdfs_path]
[WITH DBPROPERTIES (property_name=property_value, ...)];
CREATE DATABASE IF NOT EXISTS hive_test
COMMENT 'hive database for test'
LOCATION '/tmp/hive/test'
WITH DBPROPERTIES ('create'='yuanzhengme');
DESC DATABASE [EXTENDED] db_name;
DESC DATABASE EXTENDED hive_test;
DROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT|CASCADE];
DROP DATABASE IF EXISTS hive_test CASCADE;
2.创建表
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name
[(col_name data_type [COMMENT col_comment],... [constraint_specification])]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...)
[SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
[SKEWED BY (col_name, col_name, ...) ON ((col_value, col_value, ...),(col_value, col_value, ...),...)
[STORED AS DIRECTORIES]]
[[ROW FORMAT row_format] [STORED AS file_format] | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)] ]
[LOCATION hdfs_path]
[TBLPROPERTIES (property_name=property_value, ...)]
[AS select_statement];
CREATE TABLE emp(
empno INT,
ename STRING,
job STRING,
mgr INT,
hiredate TIMESTAMP,
sal DECIMAL(7,2),
comm DECIMAL(7,2),
deptno INT)
ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t";
CREATE EXTERNAL TABLE emp_external(
empno INT,
ename STRING,
job STRING,
mgr INT,
hiredate TIMESTAMP,
sal DECIMAL(7,2),
comm DECIMAL(7,2),
deptno INT)
ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t"
LOCATION '/hive/emp_external';
CREATE EXTERNAL TABLE emp_partition(
empno INT,
ename STRING,
job STRING,
mgr INT,
hiredate TIMESTAMP,
sal DECIMAL(7,2),
comm DECIMAL(7,2)
)
PARTITIONED BY (deptno INT)
ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t"
LOCATION '/hive/emp_partition';
CREATE EXTERNAL TABLE emp_bucket(
empno INT,
ename STRING,
job STRING,
mgr INT,
hiredate TIMESTAMP,
sal DECIMAL(7,2),
comm DECIMAL(7,2),
deptno INT)
CLUSTERED BY(empno) SORTED BY(empno ASC) INTO 4 BUCKETS
ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t"
LOCATION '/hive/emp_bucket';
CREATE EXTERNAL TABLE emp_skewed(
empno INT,
ename STRING,
job STRING,
mgr INT,
hiredate TIMESTAMP,
sal DECIMAL(7,2),
comm DECIMAL(7,2)
)
SKEWED BY (empno) ON (66,88,100)
ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t"
LOCATION '/hive/emp_skewed';
CREATE TEMPORARY TABLE emp_temp(
empno INT,
ename STRING,
job STRING,
mgr INT,
hiredate TIMESTAMP,
sal DECIMAL(7,2),
comm DECIMAL(7,2)
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t";
CREATE TABLE emp_copy AS SELECT * FROM emp WHERE deptno='20';
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name
LIKE existing_table_or_view_name
[LOCATION hdfs_path];
CREATE TEMPORARY EXTERNAL TABLE IF NOT EXISTS emp_co LIKE emp
load data local inpath "/usr/file/emp.txt" into table emp;
3.修改表
ALTER TABLE table_name RENAME TO new_table_name;
ALTER TABLE emp_temp RENAME TO new_emp;
ALTER TABLE table_name [PARTITION partition_spec] CHANGE [COLUMN] col_old_name col_new_name column_type[COMMENT col_comment] [FIRST|AFTER column_name] [CASCADE|RESTRICT];
ALTER TABLE emp_temp CHANGE empno empno_new INT;
ALTER TABLE emp_temp CHANGE sal sal_new decimal(7,2) AFTER ename;
ALTER TABLE emp_temp CHANGE mgr mgr_new INT COMMENT 'this is column mgr';
ALTER TABLE emp_temp ADD COLUMNS (address STRING COMMENT 'home address');
4.清空表/删除表
TRUNCATE TABLE table_name [PARTITION (partition_column = partition_col_value,...)];
TRUNCATE TABLE emp_mgt_ptn PARTITION (deptno=20);
DROP TABLE [IF EXISTS] table_name [PURGE];
5.其他命令
DESCRIBE|Desc DATABASE [EXTENDED] db_name;
DESCRIBE|Desc [EXTENDED|FORMATTED] table_name
SHOW (DATABASES|SCHEMAS) [LIKE 'identifier_with_wildcards'];
SHOW DATABASES like 'hive*';
SHOW TABLES [IN database_name] ['identifier_with_wildcards'];
SHOW TABLES IN default;
SHOW VIEWS [IN/FROM database_name] [LIKE 'pattern_with_wildcards'];
SHOW PARTITIONS table_name;
SHOW CREATE TABLE ([db_name.]table_name|view_name);