Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
209: Data Quality Monitoring in ETL Pipelines
I've seen this happen more times than I care to admit: a pipeline runs perfectly. The logs are green, the job finishes in record time, and the dashboard shows everything is "healthy." Then, a business analyst pings you because the monthly revenue report is off by 15%. You dive in and realize that three weeks ago, an upstream team changed a field from a required double to an optional string, and your code has been silently swallowing the nulls ever since.
case class Transaction(id: String, amount: Option[Double], currency: String)
def processTransactions(txs: List[Transaction]): Double = {
// The "Silent Killer" pattern
txs.map(_.amount.getOrElse(0.0)).sum
}
The Danger of Silent Defaults
The code above is "safe" in the sense that it won't throw a NullPointerException. But in a production ETL pipeline, safety isn't just about avoiding crashes; it's about ensuring data integrity. By using .getOrElse(0.0), you've effectively hidden a data quality crisis. You aren't distinguishing between a transaction that actually cost zero dollars and a transaction where the data is missing. Over a million rows, a 1% failure rate in your source data becomes a massive financial discrepancy that is nearly impossible to trace backward.
Tracking Corruption with Validated Types
To fix this, we need to stop treating data quality as a binary (it works or it crashes) and start treating it as a metric. I prefer using a pattern that separates "Clean" records from "Quarantined" records. Instead of forcing a default value, we wrap the result in a type that tells us exactly what went wrong.
sealed trait ValidationResult[+A]
case class Valid[A](data: A) extends ValidationResult[A]
case class Invalid(reason: String, rawRecord: String) extends ValidationResult[Nothing]
case class TransactionRecord(id: String, amountStr: String)
def validateTransaction(rec: TransactionRecord): ValidationResult[Transaction] = {
val amountOpt = scala.util.Try(rec.amountStr.toDouble).toOption
amountOpt match {
case Some(amt) if amt >= 0 => Valid(Transaction(rec.id, Some(amt), "USD"))
case Some(amt) => Invalid(s"Negative amount: $amt", rec.toString)
case None => Invalid(s"Invalid numeric format: ${rec.amountStr}", rec.toString)
}
}
Implementing a Quality Threshold Guardrail
Now that we can identify bad records, we don't just log them—we monitor the ratio of bad to good data. In a real-world scenario, you might be okay with 0.1% of records being malformed, but 5% suggests a breaking schema change upstream that requires an immediate stop to the pipeline.
def runPipeline(rawRecords: List[TransactionRecord]): Unit = {
val results = rawRecords.map(validateTransaction)
val clean = results.collect { case Valid(tx) => tx }
val dirty = results.collect { case Invalid(reason, raw) => (reason, raw) }
val corruptionRate = dirty.size.toDouble / rawRecords.size
// This is your "Circuit Breaker"
if (corruptionRate > 0.05) {
throw new Exception(s"Data Quality Threshold Exceeded! Corruption rate: ${corruptionRate * 100}%")
}
println(s"Processed ${clean.size} records. Corruption rate: ${corruptionRate * 100}%")
// Proceed to save 'clean' to the database...
}
By shifting the logic this way, the pipeline fails loudly and specifically when the data degrades. You get a clear error message telling you the corruption rate, and you have a list of Invalid records (the "Dead Letter Queue") that you can hand back to the upstream team to fix. You've moved from "guessing" why your numbers are wrong to "knowing" exactly where the data broke.
📋 Practical Task
Building a Transaction Quality Guardrail
You are tasked with implementing a data quality monitor for a stream of sensor readings. Each reading arrives as a SensorReading(id: String, value: String). A reading is considered "Valid" if the value is a number between -50.0 and 150.0. Any other value is considered "Corrupt".
Write a Scala program that does the following:
- Defines a
ValidationResultalgebraic data type (ADT) to handleValidandInvalidstates. - Implements a
validateReadingfunction that parses the string value and checks the range. - Processes a provided list of readings and calculates the percentage of corrupt records.
- Throws a
RuntimeExceptionif the corruption rate exceeds 10%, otherwise prints the total number of valid records processed.
Test Data to use:
val readings = List(
SensorReading("S1", "23.5"),
SensorReading("S2", "160.0"), // Invalid: Too high
SensorReading("S3", "abc"), // Invalid: Not a number
SensorReading("S4", "-10.2"),
SensorReading("S5", "45.0")
)There are no comments for now.