1 介绍
hive数据存储基于HDFS,没有专门的数据存储格式。
数据结构主要包括:
- 数据库
- 文件
- 表
- 视图
可以直接加载文本文件,创建表时可以指定hive数据的列分隔符与行分隔符。
2 表
2.1 内部表 table
1)介绍
- 与数据库中的table在概念是是类似的
- 每一个table在hive中都有一个相应的目录存储数据,所有的table数据(不包含external table)都保存在这个目录中
- 删除表时,元数据和数据都会被删除
2)示例
create table t1 (tid int, tname string, age int);
-- 将表存放在HDFS上
create table t2 (tid int, tname string, age int) location '/mytable/hive/t2';
-- 指定列的分隔符
create table t3 (tid int, tname string, age int) row format delimited fields terminated by ',';
-- 给表t1增加一列
alter table t1 add columns(english int);
hive> desc t1;
OK
tid int
tname string
age int
english int
Time taken: 0.218 seconds, Fetched: 4 row(s)
hive> desc t2
> ;
OK
tid int
tname string
age int
Time taken: 0.109 seconds, Fetched: 3 row(s)
hive> desc t3;
OK
tid int
tname string
age int
Time taken: 0.122 seconds, Fetched: 3 row(s)
2.2 分区表 partition
1)介绍
- partition对应于数据库的partition列的密集索引
- 在hive中,表中的一个partition对应于表下的一个目录,所有的partition的数据都存储在对应的目录中
2)示例
create table partiton_table(sage int, sname string)
partitioned by (gender string)
row format delimited fields terminated by ',';
insert into table partiton_table partition(gender='f')
select age, name from student where gender='f';
insert into table partiton_table partition(gender='m')
select age, name from student where gender='m';
2.3 外部表 external table
1)介绍
- 指向已经在HDFS中存在的数据,可以创建partition。
- 它和内部表在元数据的组织上是相同的,而实际数据的存储则有较大的差异。
- 外部表只有一个过程,加载数据和创建表同时完成,并不会移动到数据仓库目录中,只是与外部数据建立一个链接,当删除一个外部表时,只是删除该链接。
2)示例
create external table external_student(sid int, sname string, age int)
row format delimited fields terminated by ','
location '/hive/input';
2.4 桶表
1)介绍
对数据进行哈希取值,然后放到不同文件中存储。
2)示例
3 视图
1)介绍
- 是一种虚表,是一个逻辑概念;可以跨越多张表。
- 建立在已有表的基础上,视图赖以建立的这些表称为基表。。
- 可以简化复杂的查询。
2)示例
create view empinfo
as
select *
from emp e, dept d
where e.id = d.id;