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
106: Building ETL Pipelines with Spark and Scala
When you first move from standard Scala collections to Apache Spark, there is a mental trap I see almost every developer fall into. They treat a Spark DataFrame as if it's just a very large List or a Pandas dataframe. They assume that when they call .filter() or .select(), Spark is immediately diving into the data, slicing it, and handing back a result.
Thinking Transformations Actually Move Data Immediately
If you write a piece of code that filters a billion rows of logs and then maps a function over them, you might expect the CPU to spike the moment that line executes. But it doesn't. In fact, Spark does almost nothing. I've had juniors spend hours debugging a "performance issue" where they thought a specific transformation was slow, only to realize that the slow part was actually the .save() call ten lines later.
// This looks like it's doing work, but it's actually just building a recipe.
val rawLogs = spark.read.json("s3://logs-bucket/raw/*.json")
val cleanedLogs = rawLogs.filter($"status" === 200)
val userCounts = cleanedLogs.groupBy("userId").count()
// Up to this point, ZERO bytes of data have been processed.
// The "work" only happens here:
userCounts.write.parquet("s3://logs-bucket/processed/counts")
The misconception is that Spark is eager. In reality, Spark is lazy. Everything you do is just adding a step to a Directed Acyclic Graph (DAG). Spark is essentially taking notes on what you want to do, and it waits until you call an "Action" (like .save(), .collect(), or .count()) before it optimizes the entire plan and executes it across the cluster.
Orchestrating the Data Flow with a Plan, Not a Loop
Once you realize you're building a plan rather than manipulating a list, your approach to ETL (Extract, Transform, Load) changes. Instead of thinking "first I do this, then I do that," you start thinking about how to compose functions that transform one DataFrame into another. This is where Scala's strengths really shine. I prefer to keep my transformation logic in pure functions that take a DataFrame and return a DataFrame.
Let's look at a real-world scenario: processing raw web traffic logs to find the most visited pages per user. If we jam this all into one giant block of code, it becomes a nightmare to test. Instead, we break it down:
case class WebEvent(userId: String, pageUrl: String, timestamp: Long, userAgent: String)
object LogPipeline {
// Pure transformation: No SparkSession needed here, just the DataFrame
def filterBots(df: DataFrame): DataFrame = {
df.filter(!$"userAgent".contains("Bot") && !$"userAgent".contains("Crawler"))
}
def aggregatePageViews(df: DataFrame): DataFrame = {
df.groupBy("userId", "pageUrl")
.count()
.withColumnRenamed("count", "visitCount")
}
def run(spark: SparkSession): Unit = {
import spark.implicits._
val rawData = spark.read.json("s3://company-logs/web-traffic/")
// We chain our logic together.
// This is still lazy; we're just refining the DAG.
val finalReport = aggregatePageViews(filterBots(rawData))
finalReport.write.mode("overwrite").parquet("s3://company-logs/reports/page-views/")
}
}
Handling the "Small File Problem" and Partitioning
Here is a bit of hard-won advice: your ETL pipeline is only as good as how you write the data back to disk. I've seen pipelines that work perfectly in dev with 1GB of data, but crash in production because they create 10,000 tiny files. This happens because Spark creates one file per partition.
If your filterBots step removes 90% of your data, you might end up with a few massive partitions and a lot of nearly empty ones. Before you write your final output, use .coalesce(n) to reduce the number of partitions if the dataset has shrunk significantly, or .repartition(col("date")) if you want to physically organize your data on S3 by a specific column. This makes downstream reads significantly faster because Spark can "skip" directories it doesn't need.
📋 Practical Task
Exercise: Building a High-Value Customer Aggregator
You are tasked with building an ETL pipeline that identifies "High-Value Customers" from a raw transaction dataset. Your goal is to implement a Scala object that processes raw JSON transaction data and outputs a Parquet file containing only users who have spent more than $1,000 in total.
Requirements:
- Extract: Read a JSON dataset from the path
"s3://warehouse/transactions/". The schema containsuserId(String),amount(Double), andtransactionDate(String). - Transform 1 (Cleaning): Filter out any transactions where the
amountis negative or null (data corruption). - Transform 2 (Aggregation): Group by
userIdand calculate the sum ofamount. - Transform 3 (Filtering): Keep only the users whose total sum is greater than
1000.0. - Load: Write the resulting DataFrame to
"s3://warehouse/high_value_customers/"in Parquet format.
Technical Constraints:
- Implement the transformations as separate functions that take a
DataFrameand return aDataFrame. - Ensure you use
.coalesce(1)before writing the final output to avoid creating an excessive number of small files (assuming the high-value list is relatively small). - Your final
runmethod should orchestrate the flow from read to write.
There are no comments for now.