hive入门之内部表和外部表

1.hive的内部表和外部表

  • 内部表:

使用建表语句:

create table if not exists student2(
id int, name string
)
row format delimited fields terminated by '\t'
stored as textfile;

在这里插入图片描述使用desc查看表结构:
在这里插入图片描述

  • 外部表:

使用建表语句:

create table if not exists student2(
id int, name string
)
row format delimited fields terminated by '\t'
stored as textfile
location '/user/hive/warehouse/student2';

或者不指定location(注意:如果路径是在/user/hive/warehouse这个默认的目录下面还是会被指定为内部表),加上external 关键字即可:

create external  table if not exists student3(
id int, name string
)
row format delimited fields terminated by '\t'
stored as textfile;

在这里插入图片描述在这里插入图片描述
查看表的详细结构:
在这里插入图片描述

  • 内部表和外部表的区别:

因为表是外部表,所以Hive并非认为其完全拥有这份数据。删除该表并不会删除掉这份数据,不过描述表的元数据信息会被删除掉。也就是说,在删除内部表的时候其数据和表结构全部没了,而外部表还会保存数据在hdfs上面。

  • 内部表和外部表的转换

将内部表转化为外部表(后面的false改为true就是将外部表改为内部表):

alter table student2 set tblproperties('EXTERNAL'='TRUE');

其中,external就是附加的意思,附加表为true也就是设置为外部表。

发布了39 篇原创文章 · 获赞 1 · 访问量 4620

猜你喜欢

转载自blog.csdn.net/thetimelyrain/article/details/104134275