![Apache Flink Tutorial](/apache_flink/images/apache-flink-mini-logo.jpg)
- Apache Flink Tutorial
- Apache Flink - Home
- Apache Flink - Big Data Platform
- Batch vs Real-time Processing
- Apache Flink - Introduction
- Apache Flink - Architecture
- Apache Flink - System Requirements
- Apache Flink - Setup/Installation
- Apache Flink - API Concepts
- Apache Flink - Table API and SQL
- Creating a Flink Application
- Apache Flink - Running a Flink Program
- Apache Flink - Libraries
- Apache Flink - Machine Learning
- Apache Flink - Use Cases
- Apache Flink - Flink vs Spark vs Hadoop
- Apache Flink - Conclusion
- Apache Flink Resources
- Apache Flink - Quick Guide
- Apache Flink - Useful Resources
- Apache Flink - Discussion
Apache Flink - Creating a Flink Application
In this chapter, we will learn how to create a Flink application.
Open Eclipse IDE, click on New Project and Select Java Project.
![Create Flink Application](/apache_flink/images/create_flink_application.jpg)
Give Project Name and click on Finish.
![Create Flink Application2](/apache_flink/images/create_flink_application2.jpg)
Now, click on Finish as shown in the following screenshot.
![Create Flink Application3](/apache_flink/images/create_flink_application3.jpg)
Now, right-click on src and go to New >> Class.
![Create Flink Application4](/apache_flink/images/create_flink_application4.jpg)
Give a class name and click on Finish.
![Create Flink Application5](/apache_flink/images/create_flink_application5.jpg)
Copy and paste the below code in the Editor.
import org.apache.flink.api.common.functions.FlatMapFunction; import org.apache.flink.api.java.DataSet; import org.apache.flink.api.java.ExecutionEnvironment; import org.apache.flink.api.java.tuple.Tuple2; import org.apache.flink.api.java.utils.ParameterTool; import org.apache.flink.util.Collector; public class WordCount { // ************************************************************************* // PROGRAM // ************************************************************************* public static void main(String[] args) throws Exception { final ParameterTool params = ParameterTool.fromArgs(args); // set up the execution environment final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); // make parameters available in the web interface env.getConfig().setGlobalJobParameters(params); // get input data DataSet<String> text = env.readTextFile(params.get("input")); DataSet<Tuple2<String, Integer>> counts = // split up the lines in pairs (2-tuples) containing: (word,1) text.flatMap(new Tokenizer()) // group by the tuple field "0" and sum up tuple field "1" .groupBy(0) .sum(1); // emit result if (params.has("output")) { counts.writeAsCsv(params.get("output"), "\n", " "); // execute program env.execute("WordCount Example"); } else { System.out.println("Printing result to stdout. Use --output to specify output path."); counts.print(); } } // ************************************************************************* // USER FUNCTIONS // ************************************************************************* public static final class Tokenizer implements FlatMapFunction<String, Tuple2<String, Integer>> { public void flatMap(String value, Collector<Tuple2<String, Integer>> out) { // normalize and split the line String[] tokens = value.toLowerCase().split("\\W+"); // emit the pairs for (String token : tokens) { if (token.length() > 0) { out.collect(new Tuple2<>(token, 1)); } } } } }
You will get many errors in the editor, because Flink libraries need to be added to this project.
![Flink libraries Added](/apache_flink/images/flink_libraries_added.jpg)
Right-click on the project >> Build Path >> Configure Build Path.
![Right click Project](/apache_flink/images/right_click_project.jpg)
Select the Libraries tab and click on Add External JARs.
![Select Libraries](/apache_flink/images/select_libraries.jpg)
Go to Flink's lib directory, select all the 4 libraries and click on OK.
![Flinks lib directory](/apache_flink/images/flinks_lib_directory.jpg)
Go to the Order and Export tab, select all the libraries and click on OK.
![Order and Export Tab](/apache_flink/images/order_and_export_tab.jpg)
You will see that the errors are no more there.
Now, let us export this application. Right-click on the project and click on Export.
![Export this Application](/apache_flink/images/export_this_application.jpg)
Select JAR file and click Next
![Select JAR file](/apache_flink/images/select_jar_file.jpg)
Give a destination path and click on Next
![destination path](/apache_flink/images/destination_path.jpg)
Click on Next>
![Click Next](/apache_flink/images/click_next.jpg)
Click on Browse, select the main class (WordCount) and click Finish.
![Click Finish](/apache_flink/images/click_finish.jpg)
Note − Click OK, in case you get any warning.
Run the below command. It will further run the Flink application you just created.
./bin/flink run /home/ubuntu/wordcount.jar --input README.txt --output /home/ubuntu/output
![Get Warning](/apache_flink/images/get_warning.jpg)
To Continue Learning Please Login