Structured Streaming and Splitting nested data into multiple datasets
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Structured Streaming is an Apache Spark API that enables scalable, high-throughput, fault-tolerant stream processing of live data streams. Data ingested from various sources such as Kafka, Flume, Kinesis, or TCP sockets can be processed using complex algorithms expressed with high-level functions like map, reduce, join, and window. Stream processing typically involves filtering, aggregating, or otherwise transforming the stream data into a form that can be output to databases, file systems, or live dashboards.
Understanding Structured Streaming
Structured Streaming treats a live data stream as a table that is being continuously appended. This model is known as the "table as a stream" paradigm. Every data item that is streamed into the system is like a new row being appended to an input table. A query on this input table generates the result table, and as data continues to be added to the input table, Structured Streaming updates the result table in an incremental fashion.
The core idea is that the user defines a streaming computation similarly to how they would define a batch computation on static data. The system automatically turns it into an incremental execution that continuously updates the final result as streaming data continues to arrive.
Splitting Nested Data into Multiple Datasets
When working with complex data structures such as JSON or XML, it's common to encounter nested data. Apache Spark provides powerful tools to deal with such structures, including the ability to split nested data (or structured data) into multiple datasets or DataFrames. This is crucial when different pieces of data are to be processed in different ways.
Technical Explanation with Example:
Consider a JSON object streaming into Spark Structured Streaming from a Kafka topic. The JSON object has the following format:
To process this data, you might need to split it into two different DataFrames: one for the user info and another for the messages.
Using Spark's DataFrame API, you can select and explode the nested structures:
Table: Key Functions Used in Spark for Splitting Nested Data
| Function | Description | Example Usage |
select() | Selects and retrieves specific columns from a DataFrame | df.select("user.name") |
explode() | Converts an array or a map into rows | explode(df.col("messages")) |
alias() | Renames a column or expression | col("user.name").alias("username") |
Additional Considerations
- Schema Inference and Enforcement: In Structured Streaming, it's important to define the schema of the incoming data explicitly, especially with complex nested data. This is essential for performance and to avoid issues at runtime.
- Event Time and Watermarking: For window-based aggregations or handling late data in streams, timestamps and watermarks are critical. These allow the system to handle out-of-order data and provide consistent results.
- Fault Tolerance and Checkpointing: Structured Streaming provides built-in fault tolerance and checkpointing capabilities to handle failures. Understanding how to configure these can ensure that your streams recover gracefully.
Structured Streaming with Apache Spark provides both a high-level abstraction for stream processing and robust means to handle complex and nested data structures. As real-time analytics continue to grow in importance, mastering these skills is becoming increasingly crucial.
Related reading
- structured streaming Kafka 2.1->Zeppelin 0.8->Spark 2.4 spark does not use jar
- Submitting offsets to kafka after storm batch
- Swapping out MSMQ for RabbitMQ in NServiceBus
- Switching from ActiveMQ to RabbitMQ
- Swoole with RabbitMQ
- Symfony Messenger with Apache Kafka as queue transport
- Synchronizing data from MSSQL to Elasticsearch using Apache Kafka
- Synchronous and blocking consumption in RabbitMQ using pika

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.