程序打包和分布式运行

As described earlier, Flink programs can be executed onclusters by using a remote environment. Alternatively, programs can be packaged into JAR Files(Java Archives) for execution. Packaging the program is a prerequisite to executing them through thecommand line interface.

Packaging Programs

To support execution from a packaged JAR file via the command line or web interface, a program mustuse the environment obtained by StreamExecutionEnvironment.getExecutionEnvironment(). This environmentwill act as the cluster’s environment when the JAR is submitted to the command line or webinterface. If the Flink program is invoked differently than through these interfaces, theenvironment will act like a local environment.

To package the program, simply export all involved classes as a JAR file. The JAR file’s manifestmust point to the class that contains the program’s entry point (the class with the publicmain method). The simplest way to do this is by putting the main-class entry into themanifest (such as main-class: org.apache.flinkexample.MyProgram). The main-class attribute isthe same one that is used by the Java Virtual Machine to find the main method when executing a JARfiles through the command java -jar pathToTheJarFile. Most IDEs offer to include that attributeautomatically when exporting JAR files.

Summary

The overall procedure to invoke a packaged program consists of two steps:

  • The JAR’s manifest is searched for a main-class or program-class attribute. If bothattributes are found, the program-class attribute takes precedence over the _main-class_attribute. Both the command line and the web interface support a parameter to pass the entry pointclass name manually for cases where the JAR manifest contains neither attribute.

  • The system invokes the main method of the class.