Output modes decide how new data is added or shown in a streaming result. They help control what you see when data keeps coming in.
Output modes (append, complete, update) in Apache Spark
df.writeStream.outputMode("mode") .format("console") .start()
Replace "mode" with one of: "append", "complete", or "update".
Output mode controls how the streaming results are written to the sink.
df.writeStream.outputMode("append") .format("console") .start()
df.writeStream.outputMode("complete") .format("console") .start()
df.writeStream.outputMode("update") .format("console") .start()
This program reads streaming data, groups it by 10-second windows, counts rows, and prints the full aggregated result each time it updates using the 'complete' output mode.
from pyspark.sql import SparkSession from pyspark.sql.functions import expr spark = SparkSession.builder.appName("OutputModesExample").getOrCreate() # Create streaming DataFrame from rate source (rows with timestamp and value) inputStream = spark.readStream.format("rate").option("rowsPerSecond", 5).load() # Aggregate count of rows per 10-second window aggStream = inputStream.groupBy(expr("window(timestamp, '10 seconds')")).count() # Start query with output mode 'complete' to see full aggregation each time query = aggStream.writeStream.outputMode("complete")\ .format("console")\ .option("truncate", False)\ .start() query.awaitTermination(10000) # Run for 10 seconds query.stop() spark.stop()
Append mode only works if you add new rows without updating old ones.
Complete mode outputs the entire result table every time, which can be costly for large data.
Update mode outputs only changed rows but requires sinks that support updates.
Output modes control how streaming results are shown or saved.
Append adds new rows only, complete shows all results, update shows changed rows.
Choose mode based on your data update needs and sink capabilities.
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
