show databases;
USE database_name;
語法:git
CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name --DATABASE|SCHEMA 是等價的 [COMMENT database_comment] --數據庫註釋 [LOCATION hdfs_path] --存儲在 HDFS 上的位置 [WITH DBPROPERTIES (property_name=property_value, ...)]; --指定額外屬性
示例:github
CREATE DATABASE IF NOT EXISTS hive_test COMMENT 'hive database for test' WITH DBPROPERTIES ('create'='heibaiying');
語法:正則表達式
DESC DATABASE [EXTENDED] db_name; --EXTENDED 表示是否顯示額外屬性
示例:sql
DESC DATABASE EXTENDED hive_test;
語法:數據庫
DROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT|CASCADE];
示例:apache
DROP DATABASE IF EXISTS hive_test CASCADE;
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';
使用 desc format emp_external
命令能夠查看錶的詳細信息以下:session
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 --按照員工編號散列到四個 bucket 中 ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t" LOCATION '/hive/emp_bucket';
經過指定一個或者多個列常常出現的值(嚴重偏斜),Hive 會自動將涉及到這些值的數據拆分爲單獨的文件。在查詢時,若是涉及到傾斜值,它就直接從獨立文件中獲取數據,而不是掃描全部文件,這使得性能獲得提高。ide
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) --指定 empno 的傾斜值 66,88,100 ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t" LOCATION '/hive/emp_skewed';
臨時表僅對當前 session 可見,臨時表的數據將存儲在用戶的暫存目錄中,並在會話結束後刪除。若是臨時表與永久表表名相同,則對該表名的任何引用都將解析爲臨時表,而不是永久表。臨時表還具備如下兩個限制:性能
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
加載數據到表中屬於 DML 操做,這裏爲了方便你們測試,先簡單介紹一下加載本地數據到表中:
-- 加載數據到 emp 表中 load data local inpath "/usr/file/emp.txt" into table emp;
其中 emp.txt 的內容以下,你能夠直接複製使用,也能夠到本倉庫的resources 目錄下載:
7369 SMITH CLERK 7902 1980-12-17 00:00:00 800.00 20 7499 ALLEN SALESMAN 7698 1981-02-20 00:00:00 1600.00 300.00 30 7521 WARD SALESMAN 7698 1981-02-22 00:00:00 1250.00 500.00 30 7566 JONES MANAGER 7839 1981-04-02 00:00:00 2975.00 20 7654 MARTIN SALESMAN 7698 1981-09-28 00:00:00 1250.00 1400.00 30 7698 BLAKE MANAGER 7839 1981-05-01 00:00:00 2850.00 30 7782 CLARK MANAGER 7839 1981-06-09 00:00:00 2450.00 10 7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 1500.00 20 7839 KING PRESIDENT 1981-11-17 00:00:00 5000.00 10 7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 1500.00 0.00 30 7876 ADAMS CLERK 7788 1987-05-23 00:00:00 1100.00 20 7900 JAMES CLERK 7698 1981-12-03 00:00:00 950.00 30 7902 FORD ANALYST 7566 1981-12-03 00:00:00 3000.00 20 7934 MILLER CLERK 7782 1982-01-23 00:00:00 1300.00 10
加載後可查詢表中數據:
語法:
ALTER TABLE table_name RENAME TO new_table_name;
示例:
ALTER TABLE emp_temp RENAME TO new_emp; --把 emp_temp 表重命名爲 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; -- 修改字段 sal 的名稱 並將其放置到 empno 字段後 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');
語法:
-- 清空整個表或表指定分區中的數據 TRUNCATE TABLE table_name [PARTITION (partition_column = partition_col_value, ...)];
Cannot truncate non-managed table XXXX
。示例:
TRUNCATE TABLE emp_mgt_ptn PARTITION (deptno=20);
語法:
DROP TABLE [IF EXISTS] table_name [PURGE];
查看數據庫:
DESCRIBE|Desc DATABASE [EXTENDED] db_name; --EXTENDED 是否顯示額外屬性
查看錶:
DESCRIBE|Desc [EXTENDED|FORMATTED] table_name --FORMATTED 以友好的展示方式查看錶詳情
1. 查看數據庫列表
-- 語法 SHOW (DATABASES|SCHEMAS) [LIKE 'identifier_with_wildcards']; -- 示例: SHOW DATABASES like 'hive*';
LIKE 子句容許使用正則表達式進行過濾,可是 SHOW 語句當中的 LIKE 子句只支持 *
(通配符)和 |
(條件或)兩個符號。例如 employees
,emp *
,emp * | * ees
,全部這些都將匹配名爲 employees
的數據庫。
2. 查看錶的列表
-- 語法 SHOW TABLES [IN database_name] ['identifier_with_wildcards']; -- 示例 SHOW TABLES IN default;
3. 查看視圖列表
SHOW VIEWS [IN/FROM database_name] [LIKE 'pattern_with_wildcards']; --僅支持 Hive 2.2.0 +
4. 查看錶的分區列表
SHOW PARTITIONS table_name;
5. 查看錶/視圖的建立語句
SHOW CREATE TABLE ([db_name.]table_name|view_name);
更多大數據系列文章能夠參見 GitHub 開源項目: 大數據入門指南