🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 2.1 建表语法 [详细笔记请点击](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Hive%E5%B8%B8%E7%94%A8DDL%E6%93%8D%E4%BD%9C.md#%E4%B8%80Database) ~~~sql 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]; --从查询结果创建表 ~~~ ### 2.2 内部表 ~~~sql 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"; ~~~ ### 2.3 外部表 ~~~sql 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'; ~~~ > 内部表和外部表的区别在于:内部表删除时连数据也删除,外部表仅删除表不删除数据。 > 普通表和分区表区别:有大量数据增加的需要建分区表。 ### CTAS创建表 支持从查询语句的结果创建表: ~~~sql CREATE TABLE emp_copy AS SELECT * FROM emp WHERE deptno='20'; ~~~ ### 2.9 复制表结构 语法: ~~~sql CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name --创建表表名 LIKE existing_table_or_view_name --被复制表的表名 [LOCATION hdfs_path]; --存储位置 ~~~ 示例: ~~~sql CREATE TEMPORARY EXTERNAL TABLE IF NOT EXISTS emp_co LIKE emp ~~~ ### 2.10 加载数据到表 * 加载数据到表中属于 DML 操作,这里为了方便大家测试,先简单介绍一下加载本地数据到表中: ~~~sql -- 加载数据到 emp 表中 LOAD DATA LOCAL INPATH '/home/bizzbee/work/app/hive-1.1.0-cdh5.15.1/data/emp.txt' OVERWRITE INTO TABLE emp; ~~~ ![](https://img.kancloud.cn/7f/c9/7fc94d22bdd03e1701ea62ce210bab38_835x94.png) 其中 emp.txt 的内容如下,你可以直接复制使用,也可以到本仓库的[resources](https://github.com/heibaiying/BigData-Notes/tree/master/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 ~~~