Spark Streaming: Real-Time Data Processing
Test the fundamentals of streaming in Spark with Structured Streaming, from micro-batches, triggers and Kafka to windows, watermarks, state and exactly-once results, plus the legacy DStream API.
Question 1 of 20
Question 1
What is Spark Streaming?
Not this time. Spark processes data streams as data arrives. Its current engine, Structured Streaming, runs streaming queries with the DataFrame and SQL API; the older Spark Streaming (DStream) API is a legacy project.
Question 2
How does Spark Streaming process data?
Not this time. By default, Structured Streaming runs a query as a series of small micro-batches, each processing the data that arrived since the previous one.
Question 3
Which abstraction did the legacy Spark Streaming API use to represent a stream?
Not this time. A discretised stream (DStream) represented a stream as a sequence of RDDs, one per batch interval. Structured Streaming uses DataFrames instead.
Question 4
What is the difference between DStreams and DataFrames in Spark Streaming?
Not this time. DStreams work with RDDs, while Structured Streaming uses DataFrames and SQL for easier optimizations and better performance.
Question 5
Which entry point starts reading a stream in Structured Streaming?
Not this time. spark.readStream returns a DataStreamReader for sources such as Kafka, files and tables. StreamingContext was the entry point of the legacy DStream API.
Question 6
If no trigger is set, when does Structured Streaming start the next micro-batch?
Not this time. With no trigger, the query runs in micro-batch mode and starts each micro-batch as soon as the previous one has finished. A fixed interval or availableNow can be set with trigger().
Question 7
How does PySpark read a stream from Apache Kafka?
Not this time. Structured Streaming's Kafka source reads topics with spark.readStream.format("kafka"). The DStream Kafka integration with createDirectStream is available only for Scala and Java.
Question 8
Why is checkpointing important in Spark Streaming?
Not this time. The checkpoint location stores the source offsets processed, the commits and the query's state, so a restarted query continues where it stopped with exactly-once results.
Question 9
What is a sliding window operation in Spark Streaming?
Not this time. Sliding windows aggregate data over a time frame that moves at a fixed interval, allowing rolling computations.
Question 10
How does Structured Streaming handle late-arriving data?
Not this time. Watermarking allows defining event-time thresholds to manage delayed data while maintaining performance.
Question 11
Which function groups a streaming DataFrame into time windows for aggregation?
Not this time. Use window() on the event-time column inside groupBy(), for example groupBy(window("event_time", "10 minutes", "5 minutes")). reduceByKeyAndWindow() did this in the legacy DStream API.
Question 12
How can you optimize Spark Streaming performance?
Not this time. Optimizations include increasing parallelism, using structured streaming, and reducing shuffle operations.
Question 13
What happens if a micro-batch takes longer than its trigger interval?
Not this time. The next micro-batch starts as soon as the late one finishes, so data waits in the source and latency builds up. Add resources, reduce the work per batch or lengthen the interval.
Question 14
How do you limit how much data each micro-batch reads in Structured Streaming?
Not this time. Sources have rate limits such as maxOffsetsPerTrigger for Kafka and maxFilesPerTrigger for files. They keep micro-batches a predictable size, for example when a query starts with a large backlog.
Question 15
Which API replaces DStreams in modern Spark Streaming applications?
Not this time. Structured Streaming replaces DStreams by leveraging DataFrames and SQL-based optimizations.
Question 16
How do you stop a running Structured Streaming query?
Not this time. Call stop() on the StreamingQuery returned by start() or toTable(). With trigger(availableNow=True), the query stops by itself once it has processed the available data.
Question 17
Which operations can you use on a streaming DataFrame?
Not this time. Most DataFrame operations work on streaming DataFrames: select, filter, withColumn, joins, and aggregations. A few, such as sorting outside aggregations, aren't supported on streams.
Question 18
How does Structured Streaming handle exactly-once semantics?
Not this time. Structured Streaming gets exactly-once results from replayable sources, checkpointed offsets and state, and sinks that are idempotent or transactional.
Question 19
Which state store suits streaming queries with large state?
Not this time. The RocksDB state store provider keeps state on local disk with a memory cache, so large state doesn't strain the executors' heap the way the default in-memory store does.
Question 20
How can Spark Streaming integrate with external storage systems?
Not this time. Spark Streaming can write data to HDFS, databases, and cloud storage using output sinks such as
foreachBatch().
Work it out
- CalculatorPartitions, brokers and disk for a Kafka topicMessages, their size, retention and readers in; the partitions, disk, network and brokers out.Kafka sizing calculator
- CalculatorSize a Spark or Databricks clusterThe Databricks workers to pick, or the executors, memory and shuffle partitions for your nodes, with every number explained.Spark sizing calculator