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
83: Spark Streaming Basics
A few years ago, I worked with a developer who was tasked with monitoring server health across a massive fleet of machines. His initial approach was a classic "poor man's streaming" setup: a cron job that triggered a Spark batch job every ten minutes to process the last ten minutes of logs. It worked fine during the quiet hours, but during a major outage, the volume of error logs spiked so aggressively that the batch job started taking twelve minutes to run. He was effectively chasing a ghost, falling further and further behind the real-time state of the system until the entire pipeline collapsed under the heap pressure. He didn't need a faster batch job; he needed a way to process data as it arrived.
The Mental Shift to Micro-batches
When you first dive into Spark Streaming, you have to understand that Spark (specifically Structured Streaming) doesn't treat data as a continuous flow of individual events in the way something like Apache Flink does. Instead, it uses "micro-batching." Imagine your data is a river; Spark doesn't drink from the river one drop at a time. Instead, it dips a bucket in every few seconds, pulls out whatever accumulated, and processes that bucket using the same DataFrame API you've already mastered in the batch lessons.
I actually prefer this approach. Why? Because it means you don't have to learn a completely different set of operators for streaming. If you know how to filter a DataFrame or join two tables, you already know 90% of Spark Streaming. The primary difference is that you're no longer working with a static dataset, but an "unbounded table" that keeps growing as new data arrives.
Wiring Up a Stream with DataFrames
To get a stream running, you move from spark.read to spark.readStream. You define your source—which could be a Kafka topic, a socket, or even a directory of files—and Spark creates a streaming DataFrame. Let's look at a scenario where we're monitoring IoT temperature sensors that drop JSON files into a cloud storage folder.
val sensorData = spark.readStream
.schema(sensorSchema) // Streaming requires an explicit schema
.json("s3a://iot-bucket/incoming-temps/")
val alerts = sensorData
.filter("temperature > 100")
.select("sensor_id", "temperature", "timestamp")
Notice that sensorSchema is mandatory here. In batch mode, Spark can peek at the data to infer the schema. In streaming, it can't afford to wait for the "end" of the data to figure out what the columns are, so you have to be explicit. If you forget this, Spark will throw a fit.
Managing the Output Sink
Defining the transformation is the easy part. The tricky bit is the "Sink"—where the data actually goes. Unlike a batch job where you just call .save(), a stream needs a writeStream configuration. This is where you decide how often to trigger the micro-batch and where to store the result.
You'll typically see two main output modes: append (which only adds new rows) and complete (which rewrites the entire result table, usually used for aggregations). For our sensor alerts, we only care about the new overheating events, so append is the way to go.
val query = alerts.writeStream
.outputMode("append")
.format("console") // Great for debugging, avoid in production!
.option("checkpointLocation", "/mnt/checkpoints/temp-alerts")
.start()
query.awaitTermination()
That checkpointLocation is non-negotiable. If your cluster crashes, Spark uses that directory to remember exactly which files it has already processed. Without a checkpoint, you're essentially gambling with your data integrity, as you'll likely either skip data or process the same records twice upon restart.
📋 Practical Task
Building a Real-time Error Log Monitor
Your task is to implement a Structured Streaming pipeline that monitors a local directory for incoming JSON log files. The goal is to filter out only the "ERROR" level logs and stream them to the console in real-time.
Requirements:
- Define a schema for the logs containing:
timestamp(TimestampType),level(StringType), andmessage(StringType). - Use
spark.readStreamto monitor a folder named/tmp/log_stream. - Filter the stream to include only rows where the
levelis exactly "ERROR". - Configure the
writeStreamto useappendmode and output to theconsole. - Ensure you include a
checkpointLocationin/tmp/log_checkpointsto ensure the stream is fault-tolerant.
Testing your solution: Once your code is running, manually create a JSON file in /tmp/log_stream with the content {"timestamp": "2023-10-27 10:00:00", "level": "ERROR", "message": "Database connection failed"} and verify that it appears in your console output.
There are no comments for now.