Last active
July 7, 2021 08:13
-
-
Save nbhusare/2ad313695b65ff62375a623856f6fc18 to your computer and use it in GitHub Desktop.
Hello World Flink program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import org.apache.flink.api.common.RuntimeExecutionMode; | |
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | |
/** | |
* Flink Streaming Dataflow (source -> map operator -> sink) | |
* The code is translated into a graph of tasks, as shown here - https://ci.apache.org/projects/flink/flink-docs-release-1.11/learn-flink/#parallel-dataflows | |
*/ | |
public class HelloWorldFlink { | |
public static void main(String[] args) throws Exception { | |
// Create a context in which the program is executed | |
// Creates an instance of the {@code LocalStreamEnvironment} | |
// Source operator | |
var env = StreamExecutionEnvironment.getExecutionEnvironment(); | |
// The Data in the example is finite | |
// This will select the BATCH runtime semantics/mode | |
// https://cwiki.apache.org/confluence/display/FLINK/FLIP-134%3A+Batch+execution+for+the+DataStream+API | |
env.setRuntimeMode(RuntimeExecutionMode.BATCH); | |
// Source operator | |
// Create a Collection Data source (bounded) | |
// The data stream source created will be non-parallel (parallelism set to one) | |
var namesDataStream = env.fromCollection(Arrays.asList("Neeraj", "Bhushan", "Kunal", "Yogesh")); | |
// Map operator | |
// The operator map has parallelism set to 8 (i.e. 8 subtasks) | |
var greetDataStream = namesDataStream.map(name -> "Hello, " + name); | |
// Sink operator | |
// Print the toString() value of each element on the standard output | |
// If the parallelism is grater than 1, the output is prepended with the | |
// identifier of the task which produced the output. | |
greetDataStream.print(); | |
env.execute("Hello World Flink"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment