English 中文(简体)
Creating a Flink Application
  • 时间:2024-12-22

Apache Fpnk - Creating a Fpnk Apppcation


Previous Page Next Page  

In this chapter, we will learn how to create a Fpnk apppcation.

Open Ecppse IDE, cpck on New Project and Select Java Project.

Create Fpnk Apppcation

Give Project Name and cpck on Finish.

Create Fpnk Apppcation2

Now, cpck on Finish as shown in the following screenshot.

Create Fpnk Apppcation3

Now, right-cpck on src and go to New >> Class.

Create Fpnk Apppcation4

Give a class name and cpck on Finish.

Create Fpnk Apppcation5

Copy and paste the below code in the Editor.

import org.apache.fpnk.api.common.functions.FlatMapFunction;
import org.apache.fpnk.api.java.DataSet;
import org.apache.fpnk.api.java.ExecutionEnvironment;
import org.apache.fpnk.api.java.tuple.Tuple2;
import org.apache.fpnk.api.java.utils.ParameterTool;
import org.apache.fpnk.util.Collector;
pubpc class WordCount {

   // *************************************************************************
   // PROGRAM
   // *************************************************************************
   pubpc 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 =
      // sppt up the pnes 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"), "
", " ");
         // execute program
         env.execute("WordCount Example");
      } else {
         System.out.println("Printing result to stdout. Use --output to specify output path.");
         counts.print();
      }
   }
   
   // *************************************************************************
   // USER FUNCTIONS
   // *************************************************************************
   pubpc static final class Tokenizer implements FlatMapFunction<String, Tuple2<String, Integer>> {
      pubpc void flatMap(String value, Collector<Tuple2<String, Integer>> out) {
         // normapze and sppt the pne
         String[] tokens = value.toLowerCase().sppt("\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 Fpnk pbraries need to be added to this project.

Fpnk pbraries Added

Right-cpck on the project >> Build Path >> Configure Build Path.

Right cpck Project

Select the Libraries tab and cpck on Add External JARs.

Select Libraries

Go to Fpnk s pb directory, select all the 4 pbraries and cpck on OK.

Fpnks pb directory

Go to the Order and Export tab, select all the pbraries and cpck on OK.

Order and Export Tab

You will see that the errors are no more there.

Now, let us export this apppcation. Right-cpck on the project and cpck on Export.

Export this Apppcation

Select JAR file and cpck Next

Select JAR file

Give a destination path and cpck on Next

destination path

Cpck on Next>

Cpck Next

Cpck on Browse, select the main class (WordCount) and cpck Finish.

Cpck Finish

Note − Cpck OK, in case you get any warning.

Run the below command. It will further run the Fpnk apppcation you just created.

./bin/fpnk run /home/ubuntu/wordcount.jar --input README.txt --output /home/ubuntu/output
Get Warning Advertisements