Implementation:Heibaiying BigData Notes StreamExecutionEnvironment GetExecutionEnvironment
| Knowledge Sources | |
|---|---|
| Domains | Stream_Processing, Big_Data |
| Last Updated | 2026-02-10 10:00 GMT |
Overview
Concrete tool for obtaining a Flink streaming execution environment provided by the Apache Flink Streaming API.
Description
The StreamExecutionEnvironment.getExecutionEnvironment() factory method is the standard way to obtain a streaming execution context in Flink. It automatically determines whether the program is running locally (inside an IDE or as a standalone process) or within a Flink cluster, returning the appropriate environment instance. This method is a wrapper around Flink's internal context detection logic that shields the developer from managing environment specifics.
In the BigData-Notes repository, the StreamingJob class demonstrates this pattern as the entry point for a basic Flink streaming application. The environment is obtained once and then used to register sources, define transformations, and trigger execution.
Usage
Call this method as the first step in any Flink streaming application. It should be invoked exactly once per job and stored in a local variable for subsequent use throughout the job definition. The returned environment can be further configured with parallelism settings, checkpoint configuration, and restart strategies before any sources are added.
Code Reference
Source Location
- Repository: BigData-Notes
- File:
code/Flink/flink-basis-java/src/main/java/com/heibaiying/StreamingJob.java - Lines: 7-19
Signature
public static StreamExecutionEnvironment getExecutionEnvironment()
Import
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | N/A | N/A | This is a zero-argument factory method. It reads the runtime context internally to determine the execution mode. |
Outputs
| Name | Type | Description |
|---|---|---|
| env | StreamExecutionEnvironment | The execution environment instance, either a LocalStreamEnvironment (when running locally) or a RemoteStreamEnvironment (when running on a cluster). |
Usage Examples
Basic Usage
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
public class StreamingJob {
public static void main(String[] args) throws Exception {
// Obtain the streaming execution environment
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// Configure parallelism (optional)
env.setParallelism(1);
// ... register sources, define transformations, attach sinks ...
// Trigger execution
env.execute("Flink Streaming Job");
}
}