Hive内表和外表的区别

本文以例子的形式介绍一下Hive内表和外表的区别。例子共有4个:不带分区的内表、带分区的内表、不带分区的外表、带分区的外表。

1 不带分区的内表

    #创建表

    create table innerTable(id int,name string) row format delimited fields terminated by '|';(show tables发现没有innerTable,只有innertable。不多说,记住了)

    #从HDFS上加载数据

    load data inpath 'hdfs://master:9000/user/root/test/innerTable' into table innertable; (查看HDFS上/user/root/test/innerTable,发现文件价innerTable还在,但是里面的文件已经不在了。去哪了,去innertable表中了)

    #删除刚刚创建的表

    drop table innertable;(到HDFS上看一下innertable文件夹及其中的文件都没有了。去哪了,删除表的时候删除了)


2 带分区的内表

    #创建表

    create table inner_table_with_p(id int,name string) partitioned by (part_num int);(HDFS 出现文件夹inner_table_with_p,文件夹中为空)

    #从HDFS加载数据

    load data inpath 'hdfs://master:9000/user/root/test/innerTable/part1' into table inner_table_with_p partition(part_num=1)(文件夹inner_table_with_p出现子文件夹part_num=1,innerTable中part1消失);

    load data inpath 'hdfs://master:9000/user/root/test/innerTable/part2' into table inner_table_with_p partition(part_num=2)(文件夹inner_table_with_p出现子文件夹part_num=2,innerTable中part2消失);

load data inpath 'hdfs://master:9000/user/root/test/innerTable/part3' into table inner_table_with_p partition(part_num=3)(文件夹inner_table_with_p出现子文件夹part_num=3,innerTable中part3消失);   

    #删除分区

    alter table inner_table_with_p drop partition(part_num=1);(part_num=1对应分区文件夹本删除)

    #删除表

    drop table inner_table_with_p;(HDFS上inner_table_with_p文件夹被删除)


3 不带分区的外表

    创建表

    create external table outer_table(id int,name string) row format delimited fields terminated by '|';      (hive仓储目录中出现outer_table)

    加载数据

     load data inpath '/user/root/test/outerTable/outer' into table outer_table;(outer_table中出现子文件outer,outerTable中outer消失)

     删除表

     drop table outer_table;     (outer_table及子文件outer依然存在,因为这是外表)

4 带分区的外表

    创建表

    create external table outer_table_with_p(id int,name string) partitioned by (part_num int) row format delimited fields terminated by '|'; (hive仓储目录中出现outer_table_with_p)

    加载数据

    load data inpath '/user/root/test/outerTable/part1' into table outer_table_with_p partiton(part_num=1);   (outer_table_with_p中出现子文件夹part_num=1)

    load data inpath '/user/root/test/outerTable/part2' into table outer_table_with_p partition(part_num=2);(outer_table_with_p中出现子文件夹part_num=2)

    load data inpath '/user/root/test/outerTable/part3' into table outer_table_with_p partition(part_num=3);(outer_table_with_p中出现子文件夹part_num=3)

     删除分区

     alter table outer_table_with_p drop partition(part_num=1);(HDFS上分区文件依旧存在)         

     删除表

     drop table outer_table_with_p;(HDFS上对应数据依旧存在)



总结:

1 删除内表时,内表数据会一并删除;

2 删除外表时,外表数据依旧存在。

    

    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值