Hive 之 内部表和外部表区别

在使用hive创建表的时候,默认创建的是内部表,那什么又是内部表?和外部表有什么区别吗?

内部表与外部表的创建和数据导入

内部表创建

首先来看一下Hive创建表的语句

hive> create table in_table (str String) location '/in_table_data';

注意这里的location指向的是hdfs系统上的路径,而不是本地机器上的路径。因为hive并没有指定该表为external,所以int_table是一个内部表 。

如果创建内部表时没有指定location,就会在/user/Hive/warehouse/下新建一个表目
录。在这里我们指定了 location 为’/in_table_data’,那么从hdfs上load过来的数据就会保存在这。

[hdfs@HadoopMaster root]$ hadoop fs -ls /
Found 10 items
drwxrwxrwx   - yarn   hadoop          0 2017-06-27 19:54 /app-logs
drwxr-xr-x   - hdfs   hdfs            0 2017-06-27 19:28 /apps
drwxr-xr-x   - yarn   hadoop          0 2017-06-27 16:50 /ats
drwxr-xr-x   - hdfs   hdfs            0 2017-06-27 16:50 /hdp
drwxr-xr-x   - hdfs   hdfs            0 2017-11-08 15:53 /in_table_data
drwxr-xr-x   - mapred hdfs            0 2017-06-27 16:50 /mapred
drwxrwxrwx   - mapred hadoop          0 2017-06-27 19:27 /mr-history
drwxrwxrwx   - spark  hadoop          0 2017-11-08 15:58 /spark-history
drwxrwxrwx   - hdfs   hdfs            0 2017-10-26 20:57 /tmp
drwxr-xr-x   - hdfs   hdfs            0 2017-11-07 15:25 /user

可以看到在hdfs上已经新建了一个in_table_data的目录了,该目录现在什么都没有,因为我们还没导入数据到对应的表中。

内部表数据导入

接下来往in_table插入数据,将hdfs中/user/jintx/a.txt的文件插入到in_table中。

hive> load data inpath '/user/jintx/a.txt' into table in_table;

注意load!!它会将/user/jintx/a.txt的文件转移到/in_table_data下,而不是复制。可以看到/in_table_data目录下的确多出了a.txt的一个文件。

[hdfs@HadoopMaster root]$ hadoop fs -ls /in_table_data
Found 1 items
-rwxr-xr-x   3 hdfs hdfs         52 2017-11-08 15:48 /in_table_data/a.txt

而hdfs下原本存在/user/jintx下的a.txt已经不存在了。

[hdfs@HadoopMaster root]$ hadoop fs -ls /user/jintx
Found 3 items
-rw-r--r--   3 hdfs hdfs       1216 2017-11-07 17:02 /user/jintx/HBASE.txt
-rw-r--r--   3 hdfs hdfs         52 2017-11-08 14:56 /user/jintx/WordCount.txt
-rw-r--r--   3 hdfs hdfs         52 2017-11-08 15:48 /user/jintx/b.txt
外部表创建

上面的过程针对内部表与外部表是没什么区别的。我也建一个外部表演示一下。

hive> create  external table ex_table (str String) location '/ex_table_data';
外部表数据导入

然后把/user/jintx/b.txt导入到该表中

load data inpath '/user/jintx/b.txt' into table ex_table;

可以看到hdfs有一个存放内部表数据的目录in_table_data,和存放外部表数据的目录ex_table_data

[hdfs@HadoopMaster root]$ hadoop fs -ls /
Found 11 items
drwxrwxrwx   - yarn   hadoop          0 2017-06-27 19:54 /app-logs
drwxr-xr-x   - hdfs   hdfs            0 2017-06-27 19:28 /apps
drwxr-xr-x   - yarn   hadoop          0 2017-06-27 16:50 /ats
drwxr-xr-x   - hdfs   hdfs            0 2017-11-08 16:10 /ex_table_data
drwxr-xr-x   - hdfs   hdfs            0 2017-06-27 16:50 /hdp
drwxr-xr-x   - hdfs   hdfs            0 2017-11-08 16:01 /in_table_data
drwxr-xr-x   - mapred hdfs            0 2017-06-27 16:50 /mapred
drwxrwxrwx   - mapred hadoop          0 2017-06-27 19:27 /mr-history
drwxrwxrwx   - spark  hadoop          0 2017-11-08 16:10 /spark-history
drwxrwxrwx   - hdfs   hdfs            0 2017-10-26 20:57 /tmp
drwxr-xr-x   - hdfs   hdfs            0 2017-11-07 15:25 /user

内部表与外部表的删除

内部表删除

其实内部表与外部表的一个区别就在于删除,先来看一下结果。

hive> drop table in_table;

再来看hdfs,看到in_table_data已经不存在了。

[hdfs@HadoopMaster root]$ hadoop fs -ls /
Found 10 items
drwxrwxrwx   - yarn   hadoop          0 2017-06-27 19:54 /app-logs
drwxr-xr-x   - hdfs   hdfs            0 2017-06-27 19:28 /apps
drwxr-xr-x   - yarn   hadoop          0 2017-06-27 16:50 /ats
drwxr-xr-x   - hdfs   hdfs            0 2017-11-08 16:10 /ex_table_data
drwxr-xr-x   - hdfs   hdfs            0 2017-06-27 16:50 /hdp
drwxr-xr-x   - mapred hdfs            0 2017-06-27 16:50 /mapred
drwxrwxrwx   - mapred hadoop          0 2017-06-27 19:27 /mr-history
drwxrwxrwx   - spark  hadoop          0 2017-11-08 16:21 /spark-history
drwxrwxrwx   - hdfs   hdfs            0 2017-10-26 20:57 /tmp
drwxr-xr-x   - hdfs   hdfs            0 2017-11-07 15:25 /user
外部表删除
hive> drop table ex_table;
[hdfs@HadoopMaster root]$ hadoop fs -ls /
Found 10 items
drwxrwxrwx   - yarn   hadoop          0 2017-06-27 19:54 /app-logs
drwxr-xr-x   - hdfs   hdfs            0 2017-06-27 19:28 /apps
drwxr-xr-x   - yarn   hadoop          0 2017-06-27 16:50 /ats
drwxr-xr-x   - hdfs   hdfs            0 2017-11-08 16:10 /ex_table_data
drwxr-xr-x   - hdfs   hdfs            0 2017-06-27 16:50 /hdp
drwxr-xr-x   - mapred hdfs            0 2017-06-27 16:50 /mapred
drwxrwxrwx   - mapred hadoop          0 2017-06-27 19:27 /mr-history
drwxrwxrwx   - spark  hadoop          0 2017-11-08 16:23 /spark-history
drwxrwxrwx   - hdfs   hdfs            0 2017-10-26 20:57 /tmp
drwxr-xr-x   - hdfs   hdfs            0 2017-11-07 15:25 /user

但是你会发现,存储外部表的目录还是在的。这是为什么呢?

内部表与外部表的区别

在删除内部表的时候,Hive将会把属于表的元数据和数据全部删掉;而删除外部表的时候,Hive仅仅删除外部表的元数据,数据是不会删除的!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值