Hive: 内部表与外部表的区别探索

一、准备工作

1、文件内容

文件不在HDFS上:/home/zhangsan/test.txt
文件在HDFS上:hdfs://testcluster/user/zhangsan/test.txt

张三,23
李四,25

2、建表

-- 内部表
create table test.inner_test(
    name string,
    age int
)
row format delimited fields terminated by ',';

-- 外部表
create external table test.outer_test(
    name string,
    age int
)
row format delimited fields terminated by ',';

3、查看 hdfs

show create table test.inner_test;
show create table test.outer_test;

inner_test
outer_test

二、文件不在HDFS上

1、load data

load data local inpath '/home/zhangsan/test.txt'
    overwrite into table test.inner_test;

load data local inpath '/home/zhangsan/test.txt'
    overwrite into table test.outer_test;

2、查看文件

hadoop fs -ls hdfs://testcluster/user/hive/test.db/inter_test;
hadoop fs -ls hdfs://testcluster/user/hive/test.db/outer_test;

hdfs_path

3、删除表,查看文件是否还存在

hadoop fs -ls hdfs://testcluster/user/hive/test.db/inter_test;
hadoop fs -ls hdfs://testcluster/user/hive/test.db/outer_test;

4、结论

内部表与外部表,从外部文件系统导入数据后,都会在表的对应HDFS目录下新建文件;
删除表数据,内部表在HDFS中的文件已被删除,外部表在HDFS中的文件依旧存在,并且重新创建表后,表会自动关联到文件中的数据;
re_create_outer_table

三、文件在HDFS上

0、复制数据

hdfs dfs -copyFromLocal /home/zhangsan/test.txt hdfs://testcluster/user/zhangsan/

hdfs_file_path

1、load data

1.1 内部表

load data inpath 'hdfs://testcluster/user/zhangsan/test.txt'
    overwrite into table test.inner_test;
    
Loading data to table test.inner_test
chgrp: changing ownership of 'hdfs://testcluster/user/hive/zhangsan.db/inner_test/test.txt': User does not belong to hive
Table test.nner_test stats: [numFiles=1, numRows=0, totalSize=20, rawDataSize=0]
OK
Time taken: 0.324 seconds
-- 导入成功后,文件是被移动到指定的表目录下面了    

load_data_inner_hdfs

1.2 外部表

load data inpath 'hdfs://testcluster/user/zhangsan/test.txt'
    overwrite into table test.outer_test;
    
Loading data to table test.outer_test
chgrp: changing ownership of 'hdfs://testcluster/user/hive/test.db/outer_test/test.txt': User does not belong to hive
Table test.outer_test stats: [numFiles=1, numRows=0, totalSize=20, rawDataSize=0]
OK
Time taken: 0.368 seconds 
-- 导入成功后,文件是被移动到指定的表目录下面了 

load_data_outer_hdfs

2、删除表,查看文件是否还存在

hadoop fs -ls hdfs://testcluster/user/hive/test.db/inner_test
hadoop fs -ls hdfs://testcluster/user/hive/test.db/outer_test

delete_table_hdfs

3、结论

内部表与外部表,从HDFS导入数据后,都会在表的对应HDFS目录下新建文件;
删除表数据,内部表在HDFS中的文件已被删除,外部表在HDFS中的文件依旧存在,并且重新创建表后,表会自动关联到文件中的数据;

四、最终结论

通过此次试验,清楚了自己最初想要测试的内容是,将文件 load data 到指定表后,对应位置的文件是否存在?经过以上测试,结论:

  • 本地文件 load data后,对应文件不会受到影响;
  • HDFS 上的文件 load data 后,对应文件会被移动到指定的表目录下; 待 探 索 . . . \color{red}{待探索...} ...

markdown改变字体颜色:https://www.jianshu.com/p/c5ea545e4545

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值