Hive内部表和外部表

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

  • 创建表时使用关键字external创建的表就是外部表,没有使用该关键字创建的表就是内部表。
  • 删除表时(drop table)内部表会删除hdfs对应路径,而外部表不会删除hdfs对应的路径, 删除表无论是内部表和外部表都会删除元数据(metastore.TBLS、metastore.COLUMNS_V2)

二:location关键字

用于指定hdfs路径,如果不指定则使用默认的路径,默认路径规则为/<hive.metastore.warehouse.dir>/<数据库名称>.db/<表名>,location既可以用于内部表也可以用于外部表。

hive-site.xml

<property>
    <name>hive.metastore.warehouse.dir</name>
    <value>/data/hive/warehouse</value>
</property>

<property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://localhost:3306/metastore?characterEncoding=UTF-8&amp;createDatabaseIfNotExist=true</value>
</property>

三:测试

1. 创建普通的内部表
create table tbl_line(line string) 
row format delimited 
fields terminated by '\n'
stored as textfile;
2. 使用location关键字创建内部表
create table tbl_line_loc(line string) 
row format delimited 
fields terminated by '\n'
stored as textfile
location "/line/loc";
3. 使用external关键字创建外部表
create external table tbl_line_ext(line string) 
row format delimited 
fields terminated by '\n'
stored as textfile;
4. 加载数据
echo "Hadoop Common\nHadoop Distributed File System\nHadoop YARN\nHadoop MapReduce " > /tmp/foobar.txt
hive> load data local inpath '/tmp/foobar.txt' into table tbl_line;
hive> load data local inpath '/tmp/foobar.txt' into table tbl_line_loc;
hive> load data local inpath '/tmp/foobar.txt' into table tbl_line_ext;
5. 查看hdfs路径
hadoop fs -ls -R /

在这里插入图片描述

6. 删除表
drop table tbl_line;
drop table tbl_line_loc;
drop table tbl_line_ext;
7. 再次查看hdfs路径
hadoop fs -ls -R /

内部表删除会删除hdfs对应的路径,外部表删除不会删除hdfs对应的路径。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风流 少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值