ROW FORMAT DELIMITED [FIELDS TERMINATED BY char] [COLLECTION ITEMS TERMINATED BY char] [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char] | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, …)] 用户在建表的时候可以自定义 SerDe 或者使用自带的 SerDe。如果没有指定 ROW FORMAT 或者 ROW FORMAT DELIMITED,将会使用自带的 SerDe。在建表的时候, 用户还需要为表指定列,用户在指定表的列的同时也会指定自定义的 SerDe,Hive 通过 SerDe 确定表的具体的列的数据。 SerDe是Serialize/Deserilize的简称,目的是用于序列化和反序列化。
STORED AS 指定存储文件类型 常用的存储文件类型:SEQUENCEFILE(二进制序列文件)、TEXTFILE(文本)、 RCFILE(列式存储格式文件) 如果文件数据是纯文本,可以使用 STORED AS TEXTFILE。如果数据需要压缩, 使用 STORED AS SEQUENCEFILE。
createtable if notexists student2( id int, name string ) row format delimited fields terminated by'\t' stored as textfile location '/user/hive/warehouse/student2';
根据查询结果创建表(查询的结果会添加到新创建的表中)
1 2
createtable if notexists student3 asselect id, name from student;
根据已经存在的表结构创建表
1
createtable if notexists student4 like student;
查询表的类型
1
desc formatted student2;
外部表
建表语句 创建部门表
1 2 3 4 5
createexternaltable if notexists default.dept( deptno int, dname string, loc int ) row format delimited fields terminated by'\t';
创建员工表
1 2 3 4 5 6 7 8 9 10
createexternaltable if notexists default.emp( empno int, ename string, job string, mgr int, hiredate string, sal double, comm double, deptno int) row format delimited fields terminated by'\t';
向外部表中导入数据
1 2
load data local inpath '/opt/module/datas/dept.txt' into table default.dept; load data local inpath '/opt/module/datas/emp.txt' into table default.emp;
查询结果
1 2
select*from emp; select*from dept;
查看表格式化数据
1
desc formatted dept;
管理表与外部表的互相转换
修改内部表student2为外部表
1
altertable student2 set tblproperties('EXTERNAL'='TRUE');
查询表的类型
1
desc formatted student2;
修改外部表student2为内部表
1
altertable student2 set tblproperties('EXTERNAL'='FALSE');
createtable dept_partition( deptno int, dname string, loc string ) partitioned by (month string) row format delimited fields terminated by'\t';
加载数据到分区表中
1 2 3
load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201809'); load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201808'); load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201807');
查询分区表中数据
单分区查询
1
select*from dept_partition wheremonth='201809';
多分区联合查询
1 2 3 4 5
select*from dept_partition wheremonth='201809' union select*from dept_partition wheremonth='201808' union select*from dept_partition wheremonth='201807';