We use tables to organize data in Hadoop. Managed and external tables help decide who controls the data files and what happens when we delete the table.
External vs managed tables in Hadoop
CREATE TABLE table_name (column1 TYPE, column2 TYPE, ...)
STORED AS file_format;
-- For external tables, add: LOCATION 'path_to_data';Managed tables do not use the LOCATION clause; Hadoop stores data in its default folder.
External tables use LOCATION to point to data outside Hadoop's control.
CREATE TABLE managed_table ( id INT, name STRING ) STORED AS PARQUET;
CREATE EXTERNAL TABLE external_table (
id INT,
name STRING
)
STORED AS PARQUET
LOCATION '/user/data/external_table/';This example creates one managed and one external table, inserts data, and queries both. Managed table data is stored inside Hadoop's control. External table data is stored at the specified location.
CREATE TABLE managed_employees ( emp_id INT, emp_name STRING ) STORED AS TEXTFILE; CREATE EXTERNAL TABLE external_employees ( emp_id INT, emp_name STRING ) STORED AS TEXTFILE LOCATION '/user/hadoop/external_employees/'; -- After creating, insert some data INSERT INTO managed_employees VALUES (1, 'Alice'); INSERT INTO external_employees VALUES (2, 'Bob'); -- Query both tables SELECT * FROM managed_employees; SELECT * FROM external_employees;
Deleting a managed table deletes its data files automatically.
Deleting an external table only deletes the table metadata, not the data files.
Use external tables to share data across different systems without moving files.
Managed tables let Hadoop control data storage and cleanup.
External tables keep data files where you choose and only manage metadata.
Choose based on whether you want Hadoop to manage your data files or not.
Practice
Solution
Step 1: Understand managed table behavior
Managed tables store data files inside Hadoop's warehouse directory and Hadoop controls their lifecycle.Step 2: Understand external table behavior
External tables only manage metadata in Hadoop but keep data files at an external location you specify.Final Answer:
Managed tables store and manage data files within Hadoop, while external tables only manage metadata and keep data files outside Hadoop. -> Option CQuick Check:
Managed = data + metadata, External = metadata only [OK]
- Confusing where data files are stored
- Thinking external tables delete data on drop
- Assuming managed tables don't store data
Solution
Step 1: Recall correct syntax for external table creation
The keyword EXTERNAL comes immediately after CREATE and before TABLE.Step 2: Check location clause placement
The LOCATION clause specifies the external data path and comes after the column definitions.Final Answer:
CREATE EXTERNAL TABLE my_table (id INT, name STRING) LOCATION '/user/data'; -> Option DQuick Check:
EXTERNAL after CREATE, LOCATION after columns [OK]
- Placing EXTERNAL after TABLE keyword
- Misplacing LOCATION clause
- Omitting EXTERNAL keyword for external tables
CREATE EXTERNAL TABLE ext_table (id INT) LOCATION '/data/ext'; CREATE TABLE man_table (id INT);
What happens if you drop
ext_table and man_table respectively?Solution
Step 1: Understand drop behavior for external tables
Dropping an external table removes only metadata; data files at '/data/ext' remain untouched.Step 2: Understand drop behavior for managed tables
Dropping a managed table removes metadata and deletes data files stored in Hadoop's warehouse.Final Answer:
Dropping ext_table deletes only metadata; dropping man_table deletes both metadata and data files. -> Option BQuick Check:
External drop = metadata only, Managed drop = metadata + data [OK]
- Assuming external drop deletes data files
- Thinking managed drop keeps data files
- Confusing metadata and data deletion
Solution
Step 1: Check requirement of LOCATION for external tables
External tables require LOCATION to specify where data files reside outside Hadoop's warehouse.Step 2: Understand consequence of missing LOCATION
Without LOCATION, Hive cannot find external data, so query fails with an error.Final Answer:
Query will fail because LOCATION is mandatory for external tables. -> Option AQuick Check:
External tables need LOCATION [OK]
- Assuming default location is used for external tables
- Thinking query returns empty data silently
- Believing Hive auto-assigns external location
/user/hadoop/data. You want to create a table in Hive to query this data but ensure that deleting the table does NOT delete the data files. Which table type should you create and what is the correct approach?Solution
Step 1: Identify requirement to keep data files safe on table drop
Only external tables keep data files untouched when the table is dropped.Step 2: Use external table with explicit LOCATION
Specify LOCATION '/user/hadoop/data' so Hive knows where data files are stored externally.Step 3: Understand managed table behavior
Managed tables store data in warehouse and delete data files on drop, so not suitable here.Final Answer:
Create an external table with LOCATION '/user/hadoop/data' so Hive manages metadata only and leaves data intact on drop. -> Option AQuick Check:
External + LOCATION = safe data on drop [OK]
- Using managed table and losing data on drop
- Omitting LOCATION for external table
- Assuming managed table can protect external data
Ready to prove it?
Pass the test and this pattern moves unknown
