Skip to Content
Course content

106: Building ETL Pipelines with Spark and Scala

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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 contains userId (String), amount (Double), and transactionDate (String).
  • Transform 1 (Cleaning): Filter out any transactions where the amount is negative or null (data corruption).
  • Transform 2 (Aggregation): Group by userId and calculate the sum of amount.
  • 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 DataFrame and return a DataFrame.
  • 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 run method should orchestrate the flow from read to write.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.