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
207: Building a Batch ETL Job with Spark
How do I actually set up the SparkSession and the entry point for a production batch job?
When you're moving from a notebook to a real Scala application, the biggest shift is how you manage the SparkSession. You don't want to be manually creating sessions all over your code. I usually wrap the logic in a main object and keep the session configuration centralized. In a real production environment, you won't hardcode the config; you'll let spark-submit handle the master and memory settings, but you still need the builder in your code to kick things off.
import org.apache.spark.sql.SparkSession object SalesETL { def main(args: Array[String]): Unit = { val spark = SparkSession.builder() .appName("DailySalesAggregation") .getOrCreate() try { // Your ETL logic goes here val job = new SalesProcessor(spark) job.run() } finally { spark.stop() } } }Pro tip: Always wrap your execution in a try-finally block to ensure the session stops. It sounds trivial, but in shared cluster environments, orphaned sessions are a great way to get a stern email from your DevOps team.
How do I handle the "T" in ETL without creating a giant, unreadable chain of transformations?
This is where most developers trip up. They write one massive 50-line chain of
.filter().select().withColumn()and then spend three days debugging a null pointer exception. Instead, I treat my transformations as a series of small, testable functions. Since we're in Scala, we can leverage case classes to give our data structure and type safety, moving from a genericDataFrameto a typedDataset.Let's say we're processing raw JSON transaction logs. I'd split the cleaning from the aggregation:
case class RawTransaction(transactionId: String, customerId: String, amount: Double, timestamp: String) case class CleanTransaction(transactionId: String, customerId: String, amount: Double, date: java.sql.Date) class SalesProcessor(spark: SparkSession) { import spark.implicits._ def run(): Unit = { val rawData = spark.read.json("s3://bucket/raw/transactions/*.json").as[RawTransaction] val cleanedData = cleanTransactions(rawData) val dailySummary = aggregateSales(cleanedData) saveResults(dailySummary) } def cleanTransactions(ds: Dataset[RawTransaction]): Dataset[CleanTransaction] = { ds.filter(_.amount > 0) // Remove errors or returns .map(tx => CleanTransaction( tx.transactionId, tx.customerId, tx.amount, java.sql.Date.valueOf(tx.timestamp.substring(0, 10)) )) } def aggregateSales(ds: Dataset[CleanTransaction]) = { ds.groupBy("date") .sum("amount") .withColumnRenamed("sum(amount)", "total_revenue") } }What's the best way to write the data so it's actually performant for the people using it?
Writing a single massive CSV file is the fastest way to make your data analysts hate you. In a batch ETL job, you want to write in a columnar format—Parquet is the gold standard here—and you should almost always partition your data. Partitioning physically organizes the data into folders on disk, which means Spark (and other tools like Athena or Presto) can skip entire directories of data when querying a specific date.
I usually partition by the coarsest grain needed for reporting, like
year,month, ordate. Here is how I'd handle the write step for our sales job:def saveResults(df: org.apache.spark.sql.DataFrame): Unit = { df.write .mode("overwrite") .partitionBy("date") .parquet("s3://bucket/gold/daily_sales_summary/") }By using
partitionBy("date"), Spark creates a folder structure likedate=2023-10-01/. When someone queries for just one day, they aren't scanning the entire dataset. It's a massive win for both cost and speed.
📋 Practical Task
Exercise: Building a Customer Lifetime Value (CLV) Batch Aggregator
Your goal is to build a batch ETL job that calculates the total spend per customer across multiple transaction files. You will be provided with a directory of JSON files containing transactions with the following schema: { "customerId": String, "amount": Double, "transactionDate": String }.
Requirements:
- Create a
case classto represent the transaction. - Implement a transformation pipeline that:
- Filters out any transactions where the
amountis negative. - Groups the data by
customerId. - Calculates the total sum of
amountfor each customer.
- Filters out any transactions where the
- Write the resulting Dataset to a Parquet file located at
/tmp/customer_clv/. - Ensure the final output is partitioned by a newly created column called
customer_segment(for this exercise, you can hardcode this as "General" usingwithColumn).
Evaluation Criteria: Your solution should demonstrate the separation of the SparkSession setup from the transformation logic and use a typed Dataset for the initial cleaning phase.
There are no comments for now.