Hive内部表与外部表

Hive有两种类型的表:

内部表

外部表
下面我们详细介绍这两种表:

内部表

内部表的数据,会存放在HDFS中的特定的位置中,通常是/user/hduser/hive/warehouse.当然,也不一定,看你的Hive的配置文件中是如何配置的.

我们可以使用下面的命令来创建一个内部表并查看:

hive> create table test_mamaged_table(context string);
OK
Time taken: 0.289 seconds
hive> describe formatted test_mamaged_table;
OK
# col_name              data_type               comment             
         
context                 string                                      
         
# Detailed Table Information         
Database:               default                  
Owner:                  hadoop                   
CreateTime:             Wed May 30 04:50:41 PDT 2018     
LastAccessTime:         UNKNOWN                  
Protect Mode:           None                     
Retention:              0                        
Location:               hdfs://hadoop001:8020/user/hive/warehouse/test_mamaged_table     
Table Type:             MANAGED_TABLE            
Table Parameters:        
    transient_lastDdlTime   1527681041          
         
# Storage Information        
SerDe Library:          org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe   
InputFormat:            org.apache.hadoop.mapred.TextInputFormat     
OutputFormat:           org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat   
Compressed:             No                       
Num Buckets:            -1                       
Bucket Columns:         []                       
Sort Columns:           []                       
Storage Desc Params:         
    serialization.format    1                   
Time taken: 0.137 seconds, Fetched: 26 row(s)

我们使用下面的命令,从本地文件系统中加载一些数据到这张表中: 

hive> load data local inpath '/home/hadoop/data/hello.txt' into table test_mamaged_table;
Loading data to table default.test_mamaged_table
Table default.test_mamaged_table stats: [numFiles=1, totalSize=34]
OK
Time taken: 0.563 seconds

通过下面的命令,查看这张表在HDFS中的位置:

[hadoop@hadoop001 data]$ hdfs dfs -ls hdfs://hadoop001:8020/user/hive/warehouse/test_mamaged_table
18/05/30 04:53:51 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 1 items
-rwxr-xr-x   1 hadoop supergroup         34 2018-05-30 04:52 hdfs://hadoop001:8020/user/hive/warehouse/test_mamaged_table/hello.txt

我们可以从上图看到该表在HDFS中的位置.
下面我们删除这张表,我们成功地删除了这张表;我们再查看一下这种表在HDFS中是否存在:

hive> show tables;
OK
dept
emp
hive_wordcount
test_external_table
test_mamaged_table
Time taken: 0.023 seconds, Fetched: 5 row(s)
hive> drop table test_mamaged_table;
OK
Time taken: 0.873 seconds

[hadoop@hadoop001 data]$ hdfs dfs -ls hdfs://hadoop001:8020/user/hive/warehouse/test_mamaged_table
18/05/30 04:56:39 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
ls: `hdfs://hadoop001:8020/user/hive/warehouse/test_mamaged_table': No such file or directory

在上图中,我们可以看到,表和它的内容,都从HDFS中删除了.

外部表

外部表特别适用于想要在Hive之外使用表的数据的情况,当你删除外部表时,只是删除了表的元数据,它的数据并没有被删除.

我们创建一张外部表,并查看一下它的结构:

hive> create external table test_external_table(Name String, Sal Int) row format delimited fields terminated by ',';
OK
Time taken: 0.093 seconds
hive> describe formatted test_external_table;
OK
# col_name              data_type               comment             
         
name                    string                                      
sal                     int                                         
         
# Detailed Table Information         
Database:               default                  
Owner:                  hadoop                   
CreateTime:             Wed May 30 04:45:34 PDT 2018     
LastAccessTime:         UNKNOWN                  
Protect Mode:           None                     
Retention:              0                        
Location:               hdfs://hadoop001:8020/user/hive/warehouse/test_external_table    
Table Type:             EXTERNAL_TABLE           
Table Parameters:        
    EXTERNAL                TRUE                
    transient_lastDdlTime   1527680734          
         
# Storage Information        
SerDe Library:          org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe   
InputFormat:            org.apache.hadoop.mapred.TextInputFormat     
OutputFormat:           org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat   
Compressed:             No                       
Num Buckets:            -1                       
Bucket Columns:         []                       
Sort Columns:           []                       
Storage Desc Params:         
    field.delim             ,                   
    serialization.format    ,                   
Time taken: 0.078 seconds, Fetched: 29 row(s)

删除它

hive> show tables;
OK
dept
emp
hive_wordcount
test_external_table
Time taken: 0.022 seconds, Fetched: 4 row(s)
hive> drop table test_external_table;
OK
Time taken: 0.577 seconds

再检查一下,你可以看到,即使我们删除了表,它的数据仍然存在:

[hadoop@hadoop001 data]$ hdfs dfs -ls hdfs://hadoop001:8020/user/hive/warehouse/test_external_table
18/05/30 05:04:08 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 1 items
-rwxr-xr-x   1 hadoop supergroup         34 2018-05-30 04:57 hdfs://hadoop001:8020/user/hive/warehouse/test_external_table/hello.txt

什么时候使用哪种表?

内部表

数据是临时数据
外部的程序无法访问这些数据
数据会随着表的删除而删除

外部表

数据可以被外部程序访问
你不能基于已经存在的表再创建表
表被删除时,数据不会被删除

3 内部表和外部表的区别

 

 

4、使用场景是什么?

外部表使用场景:导入hdfs中的源数据
内部表使用场景:存放Hive处理的中间表、结果表

如:
每天将日志数据传入HDFS,一天一个目录;Hive基于流入的数据建立外部表,将每天HDFS上的原始日志映射到外部表的天分区中;
在外部表基础上做统计分析,使用内部表存储中间表、结果表,数据通过SELECT+INSERT进入内部表

 

 

 

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值