手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Hive内表和外表的区别

[日期:2013-07-14] 来源:Linux社区  作者:lovemelovemycode [字体: ]

本文以例子的形式介绍一下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 删除外表时,外表数据依旧存在。

推荐阅读:

Hive本地独立模式安装 http://www.linuxidc.com/Linux/2013-06/86104.htm

[Hive] 完全分布式安装过程(MetaStore: MySQL) http://www.linuxidc.com/Linux/2013-05/84085.htm

linux
相关资讯       Hive 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款