In this section, we’ll look at handling elements left unconsumed by sinks. Sinks may process only a portion of the elements from an upstream source, leaving some elements as “leftovers.” Here’s how to collect or ignore these remaining elements.
Collecting Leftovers
If a sink doesn’t consume all elements from the upstream source, the remaining elements are called leftovers. To capture these leftovers, use Sink.collectLeftover, which returns a tuple containing the result of the sink operation and any unconsumed elements.
Example (Collecting Leftover Elements)
Ignoring Leftovers
If leftover elements are not needed, you can ignore them using Sink.ignoreLeftover. This approach discards any unconsumed elements, so the sink operation focuses only on the elements it needs.