Core Features
- Single-threaded execution: Non-parallel source always runs with parallelism 1, even if higher parallelism is set it will be ignored
- Resource exclusive: Occupies one complete Task Slot
- Sequential processing: Ensures data processing order
Typical Use Cases
- Single-point data sources (single file, single database connection)
- Centralized state management (global counters)
- External system limitations (systems only supporting single connection)
- Scenarios requiring strict order guarantees
Code Example
public class NoParallelSource implements SourceFunction<String> {
private Long count = 1L;
private boolean running = true;
@Override
public void run(SourceContext<String> ctx) throws Exception {
while (running) {
count++;
ctx.collect(String.valueOf(count));
Thread.sleep(1000);
}
}
@Override
public void cancel() {
running = false;
}
}
Usage:
DataStream<String> stream = env.addSource(new NoParallelSource()).setParallelism(1);
Advantages & Disadvantages
- Advantages: Simple implementation, avoids concurrency conflicts, clear state management
- Disadvantages: Limited throughput, easily becomes performance bottleneck