Overview

9 Data Cleaning and Transformation Pipelines in Practice

The chapter brings together the core ideas of AI-assisted cleaning, transformation, and loading into a more realistic workflow built around noisy, continuously arriving event data. Using a World Series play-by-play dataset as the example, it shows how raw natural-language records can be turned into structured, analysis-ready rows through a pipeline that handles schema inference, extraction, validation, and enrichment. The emphasis is on moving from small, tidy exercises to production-like thinking, where data quality and transformation logic must work reliably on messy inputs.

A major theme is orchestration: instead of relying on time-based scheduling alone, the chapter explains how task-based pipelines and directed acyclic graphs help manage dependencies, retries, and monitoring. Apache Airflow is presented as a practical orchestration tool for defining workflows as code, making pipelines more reproducible and maintainable. The text also introduces event-driven architecture and pub/sub concepts, showing how producers, topics, and consumers resemble ETL stages but better fit streaming and near-real-time scenarios.

The practical pipeline starts by assigning stable identifiers to each pitch event, then uses a structured prompt and Pydantic schema to extract fields such as inning, count, pitcher, batter, pitch type, velocity, and result. It then standardizes inconsistent names and pitch labels with canonical reference tables, and applies a second AI pass to derive higher-level features like whether a play is a key moment and how exciting it is. The resulting enriched dataset supports familiar analytics and visualizations, such as pitcher workload summaries, batter contact patterns, and an excitement arc across the game, while also highlighting the need to manage ambiguity, subjectivity, and model variability carefully.

Ideal time based scheduling where extracts from sources A, B, and C, the transform, and the load all complete on time.
Real world time based scheduling with late arriving data, long running extracts, and transform errors that lead to stale downstream data products.
Sources A, B, and C feed a single Transform task that flows into a Load task, forming a simple DAG where dependencies control execution, which can run ephemerally or be started on a schedule, such as every fifteen minutes, once an hour, or via an external trigger.
A simple publish and subscribe pattern where a producer writes events to a topic, and multiple consumers subscribe to that topic to read and process the events independently.
Excitement arc for World Series Game 7, plotting pitch by pitch excitement scores with red markers highlighting key moments and a dashed line showing the smoothed excitement trend across the game.
Excitement arc for World Series Game 7, plotting pitch by pitch excitement scores with red markers highlighting key moments and a dashed line showing the smoothed excitement trend across the game.
Result of aggregating pitcher performance.
Result of visualizing aggregated pitcher performance.
Result of visualizing aggregated batter performance on a table format.
Result of visualizing aggregated batter performance on a bar charts.

FAQ

What is data orchestration in a pipeline?Data orchestration is the coordination of tasks so data moves through extraction, transformation, validation, and loading in the correct order. It makes pipelines repeatable, observable, and reliable instead of depending on scattered scripts or manual handoffs.
Why is scheduling alone not enough for ETL pipelines?Scheduling runs jobs at fixed times, but it does not guarantee upstream data is ready, complete, or valid. In real pipelines, late arrivals, retries, and failures can cause stale or incorrect downstream results if execution depends only on the clock.
What role does Apache Airflow play in this chapter?Apache Airflow is used as an orchestration framework for defining workflows as code in DAGs. It lets you connect tasks, manage dependencies, handle retries, and monitor execution for more production-like data pipelines.
What is a DAG in data engineering?A DAG, or Directed Acyclic Graph, is a set of tasks connected by dependencies where each task runs only after its upstream tasks succeed. It is the core structure used to represent orchestration workflows such as ETL pipelines.
What are events in an event-driven architecture?Events are records that something happened at a specific point in time, such as a click, transaction, or order update. They are often structured as self-contained messages with a type, identifier, timestamp, and context fields for downstream systems.
How do pub/sub systems and topics work?In a publish/subscribe pattern, producers write events to a named topic and consumers subscribe to read those events independently. Topics act like logical streams that group related messages, making it easier to route data to analytics, operational systems, or alerts.
Why use AI to infer a schema from raw play-by-play text?AI can help identify likely fields in messy natural-language event data, such as inning, batter, pitcher, pitch type, and count. This is useful when the source text is too unstructured for simple rules or regular expressions alone.
Why are canonical IDs like play_id and play_hash important?Canonical IDs give each row a stable identity so downstream tables can join back to the original event data. A sequential ID is easy to read, while a hash provides a consistent identifier even if row order changes.
Why is data quality transformation needed after AI extraction?AI extraction can produce variations like different spellings or partial player names, which makes analysis inconsistent. Canonical reference tables and mapping steps standardize names and values so grouping, joining, and reporting work correctly.
What is the main risk of using AI for excitement scores and key moment labels?These labels are subjective, so the same play may receive different scores across runs, models, or prompts. They should be treated as useful features rather than absolute truth and validated like any other metric in a production pipeline.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn AI Data Engineering ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn AI Data Engineering ebook for free