Introduction
A colleague says, "The table exists, so the sales report must be ready." Yet a table can exist while its fields are misread, its location contains no files, or a new day's files have not been registered. These cases can produce successful SQL execution and still give an incorrect report.
You will compare three deliberately incorrect mappings with a healthy reference. For each case, connect the query symptom to metadata and file evidence before changing the smallest relevant definition. The source CSV records are correct; they must remain intact. A short final diagnostic note will explain which layer caused each symptom.
Hive is ready, with independent copies of the familiar six-order fixture and several disposable external table definitions. Use the table, LOCATION, and static-partition operations already taught. No Metastore administration, service reinstallation, new programming language, or previous VM state is required. Use the Terminal.
Establish a Healthy Reference
In this step, you will establish what correct mapping looks like before diagnosing a failure. Start in the workspace:
cd /home/labex/project
Read the correct local source and its HDFS copy:
cat /home/labex/project/data/orders.csv
hdfs dfs -cat /user/labex/hive-diagnostics/orders/orders.csv
Both contain the same six comma-delimited records plus a header. The healthy reference table points at this directory and uses the correct delimiter and types. Inspect it with the metadata command learned earlier:
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "DESCRIBE FORMATTED retail_diagnostics.reference_orders;"
Beeline's -e sends SQL to the prepared service. The following query counts all rows and the rows with a non-NULL price. COUNT(column) counts non-NULL values, while COUNT(*) counts rows:
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "SELECT COUNT(*) AS rows_seen, COUNT(unit_price) AS known_prices FROM retail_diagnostics.reference_orders;" > /home/labex/project/output/healthy-counts.tsv
cat /home/labex/project/output/healthy-counts.tsv
Expect 6 rows and 6 known prices. Read a compact typed projection:
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "SELECT order_id, quantity, unit_price FROM retail_diagnostics.reference_orders ORDER BY order_id;" > /home/labex/project/output/healthy-orders.tsv
cat /home/labex/project/output/healthy-orders.tsv
Order o1 should have quantity 2 and price 10.00; the other five records have known quantities and prices too. A useful diagnostic sequence is: compare source bytes, inspect the table location and parsing settings, then compare actual row and field values. SHOW TABLES alone cannot establish these facts.
Repair a Delimiter Mismatch
In this step, you will explain why rows can exist while their typed columns are NULL. Query the disposable bad mapping:
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "SELECT order_id, quantity, unit_price FROM retail_diagnostics.bad_delimiter LIMIT 2;" > /home/labex/project/output/bad-delimiter-before.tsv
cat /home/labex/project/output/bad-delimiter-before.tsv
The first field may contain an entire comma-separated line, while numeric fields become NULL. This table expects a pipe (|) delimiter although the source uses commas. The data did not suddenly lose its prices: the parser never split the intended fields.
Inspect the definition's delimiter setting and compare it with the CSV you read:
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "DESCRIBE FORMATTED retail_diagnostics.bad_delimiter;"
For this exercise, rebuild only the disposable external definition with the already learned comma-separated mapping. It has external.table.purge=false, so dropping that definition preserves its shared source files. Do not drop the healthy reference or edit the CSV to suit the wrong definition:
cat > /home/labex/project/queries/fix-delimiter.sql <<'SQL'
DROP TABLE retail_diagnostics.bad_delimiter;
CREATE EXTERNAL TABLE retail_diagnostics.bad_delimiter (
order_id STRING, customer_id STRING, product_id STRING,
quantity INT, unit_price DECIMAL(10,2), order_status STRING, order_date DATE
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION 'hdfs://localhost:9000/user/labex/hive-diagnostics/orders'
TBLPROPERTIES ('skip.header.line.count'='1', 'external.table.purge'='false');
SQL
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -f /home/labex/project/queries/fix-delimiter.sql
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "SELECT order_id, quantity, unit_price FROM retail_diagnostics.bad_delimiter ORDER BY order_id;" > /home/labex/project/output/delimiter-fixed.tsv
cat /home/labex/project/output/delimiter-fixed.tsv
Expect the same six correctly typed records as the reference, including o1/2/10.00. Confirm the underlying file is still identical to the original. The pipe passes HDFS file contents to local diff; its final - means read that comparison input from standard input instead of a second local filename. A successful comparison prints nothing:
hdfs dfs -cat /user/labex/hive-diagnostics/orders/orders.csv | diff /home/labex/project/data/orders.csv -
No differences should appear. The repair changed interpretation metadata while retaining the source.
Repair a Table Pointing at an Empty Directory
In this step, you will diagnose missing rows by checking location evidence before changing a schema. The wrong_location table uses the correct fields and delimiter, but returns no rows:
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "SELECT COUNT(*) FROM retail_diagnostics.wrong_location;" > /home/labex/project/output/location-before.tsv
cat /home/labex/project/output/location-before.tsv
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "DESCRIBE FORMATTED retail_diagnostics.wrong_location;"
Its LOCATION ends in empty-a. Compare that directory with the known source directory:
hdfs dfs -ls /user/labex/hive-diagnostics/empty-a /user/labex/hive-diagnostics/orders
The intended file exists under orders; empty-a contains no delivery. A zero row count here is a pointer problem, rather than a need to replace numeric NULL values or reinstall Hive.
Use the ALTER TABLE SET LOCATION operation learned in the external-table lab:
cat > /home/labex/project/queries/fix-location.sql <<'SQL'
ALTER TABLE retail_diagnostics.wrong_location SET LOCATION 'hdfs://localhost:9000/user/labex/hive-diagnostics/orders';
SQL
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -f /home/labex/project/queries/fix-location.sql
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "SELECT order_id, quantity, unit_price FROM retail_diagnostics.wrong_location ORDER BY order_id;" > /home/labex/project/output/location-fixed.tsv
cat /home/labex/project/output/location-fixed.tsv
The six intended rows should now appear without a file move. For practice, a second disposable table has a different wrong pointer. Inspect it before choosing the same known source:
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "DESCRIBE FORMATTED retail_diagnostics.practice_location;"
Its LOCATION ends in empty-b. That is also an empty directory; it is not a second delivery. Apply the corresponding repair and check its count:
cat > /home/labex/project/queries/fix-practice-location.sql <<'SQL'
ALTER TABLE retail_diagnostics.practice_location SET LOCATION 'hdfs://localhost:9000/user/labex/hive-diagnostics/orders';
SQL
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -f /home/labex/project/queries/fix-practice-location.sql
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "SELECT COUNT(*) FROM retail_diagnostics.practice_location;" > /home/labex/project/output/practice-count.tsv
cat /home/labex/project/output/practice-count.tsv
Expect 6. The first useful check was the actual LOCATION and its files, not whether the table name appeared in a listing.
Register the Missing Date and Record the Diagnosis
In this step, you will fix missing partition metadata without changing the files. Both daily directories contain data:
hdfs dfs -ls /user/labex/hive-diagnostics/day14 /user/labex/hive-diagnostics/day15
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "SHOW PARTITIONS retail_diagnostics.partitioned_orders;"
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "SELECT COUNT(*) FROM retail_diagnostics.partitioned_orders;" > /home/labex/project/output/partition-before.tsv
cat /home/labex/project/output/partition-before.tsv
Only October 14 is registered, so the table returns 3 rows. The second directory's presence does not register it automatically, as you observed in the partition lab.
Add the missing date. ADD IF NOT EXISTS keeps an already registered identical partition when repeating this repair; it is not a command to change a wrong existing location:
cat > /home/labex/project/queries/fix-partition.sql <<'SQL'
ALTER TABLE retail_diagnostics.partitioned_orders ADD IF NOT EXISTS
PARTITION (order_date='2026-10-15')
LOCATION 'hdfs://localhost:9000/user/labex/hive-diagnostics/day15';
SQL
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -f /home/labex/project/queries/fix-partition.sql
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "SHOW PARTITIONS retail_diagnostics.partitioned_orders;"
beeline -u 'jdbc:hive2://localhost:10000/default' -n labex --silent=true --showHeader=false --outputformat=tsv2 -e "SELECT order_id, quantity, unit_price FROM retail_diagnostics.partitioned_orders ORDER BY order_id;" > /home/labex/project/output/partition-fixed.tsv
cat /home/labex/project/output/partition-fixed.tsv
Both dates and all six orders should now be visible. Collect your saved repair scripts into one reviewable SQL file. The local cat command concatenates the scripts and > writes the combined file:
cat /home/labex/project/queries/fix-delimiter.sql /home/labex/project/queries/fix-location.sql /home/labex/project/queries/fix-practice-location.sql /home/labex/project/queries/fix-partition.sql > /home/labex/project/queries/fix-data-mapping.sql
Save a short explanation connecting each symptom to its cause. This note accompanies the executable repair; it is not a substitute for checking the actual tables:
cat > /home/labex/project/output/diagnosis.md <<'NOTE'
Delimiter mismatch: rows existed, but comma-separated fields were not parsed into typed columns. Rebuilt only the disposable external definition.
Wrong LOCATION: a valid schema pointed at an empty directory. Changed each pointer to the existing orders source.
Missing partition: both date directories existed, but only one date was registered. Added the missing date location.
Original source files were preserved throughout.
NOTE
In a new incident, use the symptom to choose evidence: malformed fields suggest parsing checks, an empty table suggests location checks, and a missing date in a partitioned table suggests comparing directories with SHOW PARTITIONS. More than one issue can exist, so confirm actual rows after each focused repair.
Summary
You distinguished malformed fields, an empty source location, and an unregistered date using real file and catalog evidence. You repaired the corresponding metadata, retained the correct CSV sources, and collected a reviewable repair script. The final course challenge now combines filtering, joining, date selection, and repeatable ORC reporting on a new delivery.



