Hive 之 内部表和外部表区别

来源:互联网 发布:童装淘宝店简介怎么写 编辑:程序博客网 时间:2024/06/06 02:24

在使用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 itemsdrwxrwxrwx   - yarn   hadoop          0 2017-06-27 19:54 /app-logsdrwxr-xr-x   - hdfs   hdfs            0 2017-06-27 19:28 /appsdrwxr-xr-x   - yarn   hadoop          0 2017-06-27 16:50 /atsdrwxr-xr-x   - hdfs   hdfs            0 2017-06-27 16:50 /hdpdrwxr-xr-x   - hdfs   hdfs            0 2017-11-08 15:53 /in_table_datadrwxr-xr-x   - mapred hdfs            0 2017-06-27 16:50 /mapreddrwxrwxrwx   - mapred hadoop          0 2017-06-27 19:27 /mr-historydrwxrwxrwx   - spark  hadoop          0 2017-11-08 15:58 /spark-historydrwxrwxrwx   - hdfs   hdfs            0 2017-10-26 20:57 /tmpdrwxr-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_dataFound 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/jintxFound 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 itemsdrwxrwxrwx   - yarn   hadoop          0 2017-06-27 19:54 /app-logsdrwxr-xr-x   - hdfs   hdfs            0 2017-06-27 19:28 /appsdrwxr-xr-x   - yarn   hadoop          0 2017-06-27 16:50 /atsdrwxr-xr-x   - hdfs   hdfs            0 2017-11-08 16:10 /ex_table_datadrwxr-xr-x   - hdfs   hdfs            0 2017-06-27 16:50 /hdpdrwxr-xr-x   - hdfs   hdfs            0 2017-11-08 16:01 /in_table_datadrwxr-xr-x   - mapred hdfs            0 2017-06-27 16:50 /mapreddrwxrwxrwx   - mapred hadoop          0 2017-06-27 19:27 /mr-historydrwxrwxrwx   - spark  hadoop          0 2017-11-08 16:10 /spark-historydrwxrwxrwx   - hdfs   hdfs            0 2017-10-26 20:57 /tmpdrwxr-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 itemsdrwxrwxrwx   - yarn   hadoop          0 2017-06-27 19:54 /app-logsdrwxr-xr-x   - hdfs   hdfs            0 2017-06-27 19:28 /appsdrwxr-xr-x   - yarn   hadoop          0 2017-06-27 16:50 /atsdrwxr-xr-x   - hdfs   hdfs            0 2017-11-08 16:10 /ex_table_datadrwxr-xr-x   - hdfs   hdfs            0 2017-06-27 16:50 /hdpdrwxr-xr-x   - mapred hdfs            0 2017-06-27 16:50 /mapreddrwxrwxrwx   - mapred hadoop          0 2017-06-27 19:27 /mr-historydrwxrwxrwx   - spark  hadoop          0 2017-11-08 16:21 /spark-historydrwxrwxrwx   - hdfs   hdfs            0 2017-10-26 20:57 /tmpdrwxr-xr-x   - hdfs   hdfs            0 2017-11-07 15:25 /user

外部表删除

hive> drop table ex_table;
[hdfs@HadoopMaster root]$ hadoop fs -ls /Found 10 itemsdrwxrwxrwx   - yarn   hadoop          0 2017-06-27 19:54 /app-logsdrwxr-xr-x   - hdfs   hdfs            0 2017-06-27 19:28 /appsdrwxr-xr-x   - yarn   hadoop          0 2017-06-27 16:50 /atsdrwxr-xr-x   - hdfs   hdfs            0 2017-11-08 16:10 /ex_table_datadrwxr-xr-x   - hdfs   hdfs            0 2017-06-27 16:50 /hdpdrwxr-xr-x   - mapred hdfs            0 2017-06-27 16:50 /mapreddrwxrwxrwx   - mapred hadoop          0 2017-06-27 19:27 /mr-historydrwxrwxrwx   - spark  hadoop          0 2017-11-08 16:23 /spark-historydrwxrwxrwx   - hdfs   hdfs            0 2017-10-26 20:57 /tmpdrwxr-xr-x   - hdfs   hdfs            0 2017-11-07 15:25 /user

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

内部表与外部表的区别

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

原创粉丝点击