• 程序打包和分布式运行
    • Packaging Programs
    • Packaging Programs through Plans
    • Summary

    程序打包和分布式运行

    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.

    Packaging Programs through Plans

    Additionally, we support packaging programs as Plans. Instead of defining a program in the mainmethod and callingexecute() on the environment, plan packaging returns the Program Plan, which is a description ofthe program’s data flow. To do that, the program must implement theorg.apache.flink.api.common.Program interface, defining the getPlan(String…) method. Thestrings passed to that method are the command line arguments. The program’s plan can be created fromthe environment via the ExecutionEnvironment#createProgramPlan() method. When packaging theprogram’s plan, the JAR manifest must point to the class implementing theorg.apache.flink.api.common.Program interface, instead of the class with the main method.

    Summary

    The overall procedure to invoke a packaged program is as follows:

    • 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.

    • If the entry point class implements the org.apache.flink.api.common.Program, then the systemcalls the getPlan(String…) method to obtain the program plan to execute.

    • If the entry point class does not implement the org.apache.flink.api.common.Program interface,the system will invoke the main method of the class.