Databricks Data Engineer Professional Question 92
Select 2You are tasked with implementing deduplication logic in a real-time data pipeline using Spark Structured Streaming. The data contains events with the following schema: event_id (unique identifier), event_timestamp (event creation time), and data (payload). You want to deduplicate events by ensuring that only the latest event for each event_id is retained within a sliding 10-minute window. Which of the following Spark Structured Streaming approaches will correctly implement this deduplication logic?
- A
Use
withWatermarkon theevent_timestampcolumn with a 10-minute delay, followed by adropDuplicatestransformation on theevent_idcolumn. - B
Use
groupByon theevent_idcolumn and aggregate the latest event using themax(event_timestamp)function. - C
Use
withWatermarkon theevent_timestampcolumn with a 10-minute delay, followed by agroupByon bothevent_idandevent_timestamp, keeping the latest entry using a window function. - D
Use
flatMapGroupsWithStatewith event deduplication logic implemented in the custom state update function. - E
Use a
distincttransformation directly on the input DataFrame to remove duplicateevent_idvalues.
Show answer and explanation
Correct answers: A, D
Explanation
In Spark Structured Streaming, deduplication can be achieved using either built-in transformations like withWatermark and dropDuplicates or by implementing custom stateful logic using flatMapGroupsWithState. The first approach is simpler and leverages the structured streaming engine's optimizations, while the second provides more flexibility and control over the deduplication process. Other options either lack proper handling of late data or are not suitable for streaming scenarios.
- A. Correct.
Correct. Using
withWatermarkensures that late data beyond the watermark threshold is discarded, anddropDuplicatesremoves duplicateevent_idvalues while processing the stream. - B. Incorrect.
Incorrect. While
groupByandmax(event_timestamp)can identify the latest event, this approach is not suitable for use in streaming pipelines without a watermark to handle late data and a deduplication mechanism. - C. Incorrect.
Incorrect. Grouping by both
event_idandevent_timestampwould not guarantee deduplication of events, as it may retain multiple rows with the sameevent_idbut different timestamps. - D. Correct.
Correct.
flatMapGroupsWithStateallows for fine-grained stateful processing where custom logic can be written to track and deduplicate events. This is a valid approach for achieving deduplication in a streaming context. - E. Incorrect.
Incorrect. The
distincttransformation is not designed for use in streaming pipelines and does not guarantee proper deduplication over a sliding window.