Output modes (append, complete, update) in Apache Spark - Time & Space Complexity
When working with streaming data in Apache Spark, it's important to know how the output mode affects processing time.
We want to understand how the time to write results grows as the data size increases for different output modes.
Analyze the time complexity of this streaming output code snippet.
streamingDF.writeStream
.outputMode("append")
.format("console")
.start()
streamingDF.writeStream
.outputMode("complete")
.format("console")
.start()
streamingDF.writeStream
.outputMode("update")
.format("console")
.start()
This code writes streaming data to the console using three output modes: append, complete, and update.
Look at what happens each time new data arrives in the stream.
- Primary operation: Writing output rows to the sink (console here).
- How many times: For each batch of data, the output mode decides how many rows are written.
As the total data processed grows, the amount of output work changes by mode.
| Input Size (n rows) | Append Mode | Complete Mode | Update Mode |
|---|---|---|---|
| 10 | Writes ~10 new rows | Writes all 10 rows | Writes changed rows only |
| 100 | Writes ~100 new rows | Writes all 100 rows | Writes changed rows only |
| 1000 | Writes ~1000 new rows | Writes all 1000 rows | Writes changed rows only |
Pattern observation: Append mode writes only new rows, so work grows with new data size. Complete mode writes all rows every time, so work grows with total data size. Update mode writes only changed rows, which can vary but often less than total data.
Time Complexity: O(n)
This means the time to write output grows linearly with the number of rows processed or updated, depending on the mode.
[X] Wrong: "All output modes take the same time regardless of data size."
[OK] Correct: Different modes write different amounts of data each time, so their time grows differently with input size.
Understanding how output modes affect processing time helps you explain trade-offs in streaming applications clearly and confidently.
"What if we changed the output sink from console to a database? How might that affect the time complexity for each output mode?"
Practice
Solution
Step 1: Understand output modes
Append mode adds only new rows to the output sink without changing existing data.Step 2: Compare with other modes
Complete mode outputs the entire result table, and Update mode outputs only changed rows.Final Answer:
Append -> Option CQuick Check:
Append = adds new rows only [OK]
- Confusing append with complete mode
- Thinking update mode adds only new rows
- Assuming overwrite is an output mode
Solution
Step 1: Recall Spark streaming syntax
The correct method to set output mode is outputMode("mode_name") on writeStream.Step 2: Check options
query.writeStream.outputMode("complete") uses outputMode("complete"), which is correct syntax. Others use incorrect method names or casing.Final Answer:
query.writeStream.outputMode("complete") -> Option AQuick Check:
outputMode("complete") is correct method [OK]
- Using mode() instead of outputMode()
- Using underscores instead of camelCase
- Trying setOutputMode() which doesn't exist
val query = df.writeStream
.format("console")
.outputMode("update")
.start()
What does the 'update' mode do here?Solution
Step 1: Understand 'update' mode meaning
Update mode outputs only rows that have changed since the last trigger, not the full table.Step 2: Compare with other modes
Append outputs only new rows, complete outputs full table, overwrite is not a streaming mode.Final Answer:
Outputs only rows that have changed since last trigger -> Option DQuick Check:
Update = changed rows only [OK]
- Confusing update with append mode
- Thinking update outputs full table
- Assuming overwrite is a streaming output mode
df.writeStream
.format("console")
.outputMode("append")
.start()
The query fails because the DataFrame has aggregations. What is the error and how to fix it?Solution
Step 1: Identify cause of error
Append mode cannot be used with aggregations because new rows alone can't be identified.Step 2: Fix by changing output mode
Use complete mode to output full aggregation results instead of append.Final Answer:
Append mode not allowed with aggregations; use complete mode instead -> Option AQuick Check:
Aggregations require complete mode, not append [OK]
- Trying to fix by changing format instead of mode
- Ignoring aggregation restrictions
- Assuming spelling or method call errors
Solution
Step 1: Understand the use case
Stateful aggregation updates counts per key, so some counts change over time.Step 2: Choose output mode for efficient updates
Update mode outputs only changed rows, ideal for sinks supporting updates to avoid full rewrites.Step 3: Exclude other modes
Append mode can't handle updates, complete mode outputs full data (less efficient), overwrite replaces all data (not streaming mode).Final Answer:
Update mode, because it outputs only changed counts for efficient updates -> Option BQuick Check:
Update mode = changed rows only, best for stateful updates [OK]
- Choosing append mode for updates
- Using complete mode causing full data writes
- Confusing overwrite with streaming output mode
Ready to prove it?
Pass the test and this pattern moves unknown
