Hive的内部表,外部表

在使用hive搭建数据仓库的时候,可以建立两种表格。一种是Managed Table 内部表,另一种是External Table 外部表。

Managed Table :这种表也被称作Internal Table.这是Hive中的默认的类型。如果你在创建表的时候没有指明Managed或者External,那么默认就会给你创建Managed Table.Managed Table的数据,会存放在HDFS特定的位置中,通常是/user/uduser/hive/warhouse.当然,也不一定,看你的Hive的配置文件中是如何配置的。内部表是先在hive里建一张表,然后想这个表插入数据,(用insert可以插入数据,也可以通过加载外部文件插入数据),这样的表称为hive的内部表。

内部表建表时不加修饰词即可:create table database_name.table_

                                                   (column1 string,

                                                   column2 string)

External Table :特别适用于想要在hive之外使用表的数据的情况,当你删除External Table时,只是删除了表的元数据,它的数据并没有被删除。HDFS里有已经有数据,比如有一个1.txt文件,里面存储了这样一些数据:1 tom,2 bob,先有数据,再建立hive表。外部表特点是删除外部表,但对应的数据仍然还在,在实际生产环境,大多数据创建都是外部表。然后,通过hive创建一张表来管理这个文件数据,当前表就是外部表。

外部表建表时需要加external:create table external database_name.table_

                                                 (column1 string,

                                                  column2 string

内部表跟外部表的区别:

对于内部表,内部表数据由hive自身管理,删除表格时,删除元数据(meta data)和存储数据,即表格,以及对应表格的数据文件。内部表数据存储位置是hive.metastore.warehouse.dir(默认:/user/hive/warehouse),对内部表修改会将修改直接同步给元数据。

对于外部表,外部表数据由HDFS管理,删除表格时,仅删除元数据(meta data)即仅删除表格,不会删除元数据。如果发现错误删除表格,可以重新建表,然后把数据load回新表中。外部表数据存储位置由自己制定(如果没有LOCATION.Hive将在HDFS上的/usre/hive/warehouse文件夹下以外部表的表名创建一个文件夹,并将属于这个表数据放在这里),对外部表的结构和分区进行修改则需要修复(MSCK REPAIR TABLE table_name;)。

一般情况下,在企业内部都是使用外部表的,因为会有多人操作数据仓库,可能会产生数据表误删操作,为了数据安全性,通常会使用外部表。当个人写测试代码时,可以使用内部表,这样当自己使用完成后,可以方便直接删除。

以下是官网中关于external表的介绍:

A table created without the EXTERNAL clause is called a managed table because Hive manages its data.
Managed and External Tables
By default Hive creates managed tables, where files, metadata and statistics are managed by internal Hive processes. A managed table is stored under the hive.metastore.warehouse.dir path property, by default in a folder path similar to /apps/hive/warehouse/databasename.db/tablename/. The default location can be overridden by the location property during table creation. If a managed table or partition is dropped, the data and metadata associated with that table or partition are deleted. If the PURGE option is not specified, the data is moved to a trash folder for a defined duration.
Use managed tables when Hive should manage the lifecycle of the table, or when generating temporary tables.
An external table describes the metadata / schema on external files. External table files can be accessed and managed by processes outside of Hive. External tables can access data stored in sources such as Azure Storage Volumes (ASV) or remote HDFS locations. If the structure or partitioning of an external table is changed, an MSCK REPAIR TABLE table_name statement can be used to refresh metadata information.
Use external tables when files are already present or in remote locations, and the files should remain even if the table is dropped.
Managed or external tables can be identified using the DESCRIBE FORMATTED table_name command, which will display either MANAGED_TABLE or EXTERNAL_TABLE depending on table type.
Statistics can be managed on internal and external tables and partitions for query optimization.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值