Hive(7):Hive四大表类型内部表、外部表、分区表和桶表

一、概述

总体上Hive有四种表:外部表,内部表(管理表),分区表,桶表。分别对应不同的需求。下面主要讲解各种表的适用情形、创建和加载数据方法。

二、具体内容

1.内部表

创建内部表和加载数据

create table emp_inner(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
row format delimited fields terminated by '\t' 
LOCATION '/user/hive/warehouse/hadoop.db/emp';

2.外部表

(1)适用情形:

  当一份日志需要多个小组一起分析,分析完了之后创建的表就可以删除了。但是普通的表删除的同时也会把数据删除,这样就会影响到其他小组的分析,而且日志数据也不能随便删除。所以,需要外部表,删除外部表,不会删除对应的hdfs上的数据。

(2)创建外部表

create EXTERNAL table dept_ext(
deptno int,
dname string,
loc string
)
row format delimited fields terminated by '\t' ;
load data local inpath '/opt/datas/dept.txt' into table dept_ext;

(3)对比外部表和内部表区别
    删除外部表,数据不会有任何改变,只是mysql中的元数据被修改,但是删除内部表(管理表),数据就会被删除。

    总结:hive内部表和外部表的区别
        1)创建表时:创建内部表时,会将数据移动到数据仓库指向的路径;若创建外部表,仅记录数据所在的路径, 不对数据的位置做任何改变。
        2)删除表时:在删除表的时候,内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。这样外部表相对来说更加安全些,数据组织也更加灵活,方便共享源数据

3、临时表

(1)适用情形

临时分析,在关闭hive客户端后,临时表就会消失。主要用于存储不重要中间结果集,不重要的表。

(2)创建临时表并加载数据

create TEMPORARY table dept_tmp(  
deptno int,  
dname string,
loc string
)
row format delimited  fields terminated by '\t';

load data local inpath '/opt/datas/dept.txt' into table dept_tmp;

(3)查看location信息

desc formatted dept_tmp;
Location:               hdfs://172.19.199.187:8020/tmp/hive/hadoop/68174383-f427-4629-9707-0ab1c9b07726/_tmp_space.db/d872efec-1294-48b0-9071-31cf98d46400    
Table Type:             MANAGED_TABLE     

4、分区表【***】

(1)适用情形

普通的表:select * from logs where date = '20171209',执行流程:对全表的数据进行查询,然后才过滤操作。

分区表:select * from logs where date = '20171209',执行流程:直接加载对应文件路径下的数据。适用于大数据量,可以通过分区快速定位需要查询的数据,分区表的作用主要是提高了查询检索的效率 。

(2)创建一级分区并且加载数据

create table emp_part(  
empno int,  
ename string,
job string,  
mgr int,
hiredate string,  
sal double,  
comm double,  
deptno int
)partitioned by (`datetime` string)
row format delimited  fields terminated by '\t';

load data local inpath '/opt/datas/emp.txt' into table emp_part partition(`datetime`='20171209');
load data local inpath '/opt/datas/emp.txt' into table emp_part partition(`datetime`='20171208');
		【在hdfs上形成两个文件夹,emp.txt存储在里面】
		/user/hive/warehouse/hadoop.db/emp_part/datetime=20171208
		/user/hive/warehouse/hadoop.db/emp_part/datetime=20171209
		
		查询结果:
		select * from emp_part where `datetime` = '20171209';

(3)创建二级分区并且加载数据

create table emp_part2(  
empno int,  
ename string,
job string,  
mgr int,
hiredate string,  
sal double,  
comm double,  
deptno int
)partitioned by (`datetime` string,hour string)
row format delimited  fields terminated by '\t';

load data local inpath '/opt/datas/emp.txt' into table emp_part2 partition(`datetime`='20171209',hour='01');

load data local inpath '/opt/datas/emp.txt' into table emp_part2 partition(`datetime`='20171209',hour='02');
	【在hdfs上,形成目录】
	/user/hive/warehouse/hadoop.db/emp_part2/datetime=20171209/hour=01
	/user/hive/warehouse/hadoop.db/emp_part2/datetime=20171209/hour=02

查询结果:

-》select * from emp_part2 where `datetime` = '20171209';
	查看/user/hive/warehouse/hadoop.db/emp_part2/datetime=20171209内的所有数据(即两倍的emp.txt数据)
-》select * from emp_part2 where `datetime` = '20171209' and hour = '01';
	查看/user/hive/warehouse/hadoop.db/emp_part2/datetime=20171209/hour=01内的所有数据(即emp.txt数据)

(4)创建外部分区表(删除的时候,只是元数据删除,数据是不会删除的)

create EXTERNAL table emp_test(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
) 
PARTITIONED BY(date string,hour string) 
row format delimited fields terminated by '\t';

(5)加载分区表数据方法

(a)直接通过load命令(指定partition),加载数据到表的某个分区中,select是可以查询到的。                        

load data local inpath '/opt/datas/emp.txt' into table emp_part2 partition(`datetime`='20171209',hour='01');

(b)手动创建目录/user/hive/warehouse/hadoop.db/emp_part2/datetime=20171209/hour=03,然后put上数据,表select查询是查询不到的。然后,使用alter将路径添加到原数据库mysql数据库中。

alter table emp_part2 add partition(`datetime`='20171209',hour='03');

5、桶表

(1)使用情形

数据有严重的数据倾斜,分布不均匀,但是相对来说每个桶中的数据量会比较平均。桶于桶之间做join等查询的时候,会有优化。

(2)创建并使用

首先,

 set hive.enforce.bucketing = true; 

然后,

create table emp_bu(  
empno int,  
ename string,
job string,  
mgr int,
hiredate string,  
sal double,  
comm double,  
deptno int
)CLUSTERED BY(deptno) INTO 4 BUCKETS
row format delimited  fields terminated by '\t';

最后加载数据,使用insert

insert overwrite table emp_bu_2 select * from emp;

 

  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值