1、内部表
与数据库中的Table在概念上类似
每一个Table在Hive中都有一个相应的目录存数据
所有的Table数据(不包括External Table)都保存在这个目录中
删除表时,元数据与数据都会删除
(1)创建默认内部表
<span style="font-family: Arial, Helvetica, sans-serif;"> create table t1</span>
> (tid int,
> tname string,
> age int);
创建后,默认的HDFS的指定目录:/user/hive/warehouse/t1。
加入指定了分割符\t,此时有一文件11.txt:
1 yy 20
2 xx 30
3 zz 40
制表符分割,此时执行如下命令向t1中插入数据:
load data local inpath '/opt/softwares/11.txt' overwrite into table t1;
(2)指定目录
create table t2(
> tid int,
> tname string,
> age int)
> location '/user/hive/t2';
(3)指定分割符
默认是制表符,指定分隔符:
create table t3(
> tid int,
> tname string,
> age int)
> row format delimited fields terminated by ',';
(4)指定数据
create table t4
as
select * from xx;
即t4的数据来自于xx
(4)指定数据和分隔符
create table t5
row format delimited fields terminated by ','
as
select * from xx;
数据用,分割。
(5)修改表结构
alter table t1 add columns(col int);
(6)删除表
drop table t1;
2、分区表
Partition对应于数据库的partition列的密集索引
在Hive中,表中的一个Partition对应于表下的一个目录,所有的Partition的数据都存储在对应的目录中
create table partition_table
(sid int,sname string)
partitioned by(gender string)
row format delimited fields terminated by ',';
如下图:
插入数据:
insert into table partition_table(sex='M') select sid,sname from sample_data where sex='M';
insert into table partition_table(sex='F') select sid,sname from sample_data where sex='F';
3、外部表
指向已经在HDFS中存在数据的表,可以创建partition
它和内部表在元数据的组织上是相同的,而实际数据的存储则有较大的差异
外部表只有一个过程,加载数据和创建表同时完成,并不会移动数据到数据仓储目录中,只是与外部数据建立一个链接。当删除一个外部表时
,仅删除该链接。
create external table external_table
(sid int,
sname string,
age int
)row format delimited fields terminated by ','
location '/input';
其中location指向的是HDFS上的文件目录。
4、桶表
桶表(bucket table)是对数据进行哈希取值,然后放到不同文件中存储(根据HASH值是否相同)。
create table bucket_table
(sid int,
sname string,
age int
) clustered by(sname) into 3 buckets;
对sname进行哈希,放到3个桶中。