Data Exploration and Preparation
This domain covers profiling unfamiliar tables, cleaning types and nulls, deduplicating, and producing trustworthy curated tables. Exam items are practical: given a messy dataset description, which check or transformation reveals the defect and which silently corrupts results.
Profiling before you transform
DESCRIBE FORMATTED tblshows SerDe, location, input format, and whether the table is managed, external, or transactional.SHOW PARTITIONS tblandSHOW TABLE STATS tblreveal partition layout;SHOW COLUMN STATSgives distinct counts, null counts, and min/max in Impala.- Basic profile per column:
COUNT(*),COUNT(col),COUNT(DISTINCT col),MIN,MAX, and a null/blank ratio.
Cleaning rules that matter
- Null versus empty string: in Hive and Impala,
''is notNULL. UseNULLIF(TRIM(col),'')before null checks. - Safe casting:
CAST('abc' AS INT)returnsNULLrather than raising an error, so bad rows vanish silently. Detect them withCASE WHEN col RLIKE '^[0-9]+$' THEN CAST(col AS INT) END. - Date ambiguity:
2024-03-05is unambiguous;03/05/2024is not. Pin the format withto_date(from_unixtime(unix_timestamp(col,'MM/dd/yyyy')))in Hive orTO_DATE(col,'MM/dd/yyyy')in Impala, and confirm with a min/max sanity check. - Deduplication: use
ROW_NUMBER() OVER (PARTITION BY business_key ORDER BY ingest_ts DESC)and keeprn = 1, then persist with CTAS so downstream queries are cheap. - Malformed CSV: set
skip.header.line.countand an explicit SerDe; a single bad row can shift every subsequent column.
Worked example
A customers landing table has 10,000,000 rows but COUNT(DISTINCT customer_id) returns 9,400,000. Profiling shows duplicates created by re-runs. Build a curated table:
CREATE TABLE customers_cur AS
SELECT customer_id, email, region, updated_at
FROM (SELECT c.*, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC) rn
FROM customers_landing c) x
WHERE rn = 1;
Then add COMPUTE STATS customers_cur and verify COUNT(*) = COUNT(DISTINCT customer_id).
Traps
- Using
COUNT(*)for a null check - onlyCOUNT(col)excludes nulls. - Deduplicating with
DISTINCTwhen you need the latest record per key, not any record. - Sampling with
LIMIT(first-N, not random) instead ofTABLESAMPLEorORDER BY RAND(). - Trusting a schema-on-read table whose columns silently shifted after a delimited-file change.