External Vs Internal(Managed) Tables in Hive

Updated On February 12, 2021 | By Mahesh Mogal

In Hive, we have two kinds of tables available. Managed or internal tables that are controlled by the hive when it comes to their data and metadata. And the second type of tables is the External table, hive only control metadata for these tables. In this tutorial we will dive deep to learn more about these two types of tables.

Managed Tables in Hive

Hive is responsible for(more or less) the life cycle of these tables. By default hive creates managed tables. That means any table which we do not explicitly specify as an external table, will be created as an Internal or managed table.

When we drop managed tables from the hive, not only its metadata is deleted from Hive but also data is deleted from HDFS. This is why these tables are known as Managed tables, as Hive manages there Data as well. Let us see them in action.

Hive keeps managed tables in sub-directory created under the database directory. (Check table location in the above query). We can specify another location for Managed tables as well. But this may create confusion in the future. So it is advisable to use external tables if we want to use non default location for the table.

We can check that if we drop this table hive will also delete "/apps/hive/warehouse/maheshmogal.db/test" directory along with table metadata.

External Tables in Hive

When we create a table with the EXTERNAL keyword, it tells hive that table data is located somewhere else other than its default location in the database. That is why when we create the EXTERNAL table we need to specify its location in the create query.

So what happens when we drop the external table? Hive only drops metadata for that table keeping original data at its location. As it is an external table, Hive does not assume that it owns table data and keeps it as it is in HDFS.

We can validate this using below queries. HDFS directory is still there event after we have dropped Hive table from database.

Conclusion

We have learnt about two types of tables in Hive. Hive owns data for Managed tables along with Table metadata. However for external tables, Hive only owns table metadata. External tables add extra flexibility as our data is safe from accidental drops and that data can easily be shared by multiple entities operating on HDFS (like pig, spark, etc).

In most cases using external tables is better option but there are use-cases for managed tables as well like intermediate temporary tables ( as data will be deleted with table once they serve their purpose) I hope you found this article useful. See you in next one (Y).

Mahesh Mogal

I am passionate about Cloud, Data Analytics, Machine Learning, and Artificial Intelligence. I like to learn and try out new things. I have started blogging about my experience while learning these exciting technologies.

Stay Updated with Latest Blogs

Get latest blogs delivered to your mail directly.

Recent Posts

Spark Join Types With Examples

In this blog, we are going to learn different spark join types. We will also write code and validate data output for each join type to better understand them.

Read More
Integrate Spark with Jupyter Notebook and Visual Studio Code

In this blog, we are going to integrate spark with jupyter notebook and visual studio code to create easy-to-use development environment.

Read More
Reading Data From SQL Tables in Spark

In this blog, we are going to learn about reading data from SQL tables in Spark. We will create Spark data frames from tables and query results as well.

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram