##提交任务的命令行
./bin/flink run -m yarn-cluster -ynm liufen_contry_jzhk -ys 5 --parallelism 2 -yD yarn.containers.vcores=1 -c app.JZHK ./liufen_contry-jzhk.jar
##根据命令行,可以知道提交flink任务是启动了一个名为flink的脚本,然后后面跟了一堆参数# Add Client-specific JVM optionsFLINK_ENV_JAVA_OPTS="${FLINK_ENV_JAVA_OPTS}${FLINK_ENV_JAVA_OPTS_CLI}"#####这句代码是真正执行入口Java类的代码--CliFrontend,前面是读取配置和解析的逻辑# Add HADOOP_CLASSPATH to allow the usage of Hadoop file systemsexec$JAVA_RUN$JVM_ARGS$FLINK_ENV_JAVA_OPTS"${log_setting[@]}" -classpath "`manglePathList "$CC_CLASSPATH:$INTERNAL_HADOOP_CLASSPATHS"`" org.apache.flink.client.cli.CliFrontend "$@"
2、入口Java类:CliFrontend
/*
*当前类:org.apache.flink.client.cli.CliFrontend
*/publicstaticvoidmain(finalString[] args){EnvironmentInformation.logEnvironmentInfo(LOG,"Command Line Client", args);// 1. find the configuration directory/*TODO 获取flink的conf目录的路径*/finalString configurationDirectory =getConfigurationDirectoryFromEnv();// 2. load the global configuration/*TODO 根据conf路径,加载配置*/finalConfiguration configuration =GlobalConfiguration.loadConfiguration(configurationDirectory);// 3. load the custom command lines/*TODO 根据之前获取的conf路径(configurationDirectory)和用conf路径拿到的配置属性对象(configuration)封装命令行接口:按顺序Generic、Yarn、Default*/finalList<CustomCommandLine> customCommandLines =loadCustomCommandLines(
configuration,
configurationDirectory);try{//拿到配置属性和包含配置的命令行列表,创建CliFrontend对象(Cli前端)finalCliFrontend cli =newCliFrontend(
configuration,
customCommandLines);//可能是根据安全配置属性加载上下文的动作SecurityUtils.install(newSecurityConfiguration(cli.configuration));//调用runSecured,启动上下文,runSecured函数当中传入lambda表达式--CliFrontend对象cli调用cli.parseAndRun(args)方法,传入命令行当中包含的参数,解析并运行,以此为入口在yarn当中部署集群并提交运行任务int retCode =SecurityUtils.getInstalledContext().runSecured(()-> cli.parseAndRun(args));System.exit(retCode);}catch(Throwable t){finalThrowable strippedThrowable =ExceptionUtils.stripException(t,UndeclaredThrowableException.class);
LOG.error("Fatal error while running command line interface.", strippedThrowable);
strippedThrowable.printStackTrace();System.exit(31);}}
/*
*当前类:org.apache.flink.client.cli.CliFrontend
*Clifrontend.main()->cli.parseAndRun(args)
*/publicintparseAndRun(String[] args){// check for actionif(args.length <1){CliFrontendParser.printHelp(customCommandLines);System.out.println("Please specify an action.");return1;}// get action,把之前传进来的第一个参数,作为actionString action = args[0];// remove action from parameters(从参数当中拿走第一个参数action,因为已经读取完了,剩下的传到run方法当中去)finalString[] params =Arrays.copyOfRange(args,1, args.length);try{// do actionswitch(action){case ACTION_RUN://之前解析到的第一个参数是“run”,第一个参数作为action,触发case when当中的ACTION_RUNrun(params);return0;case ACTION_RUN_APPLICATION:runApplication(params);return0;case ACTION_LIST:list(params);return0;
……………………
case"-h":case"--help":CliFrontendParser.printHelp(customCommandLines);return0;case"-v":case"--version":String version =EnvironmentInformation.getVersion();String commitID =EnvironmentInformation.getRevisionInformation().commitId;System.out.print("Version: "+ version);System.out.println(commitID.equals(EnvironmentInformation.UNKNOWN)?"":", Commit ID: "+ commitID);return0;default:System.out.printf("\"%s\" is not a valid action.\n", action);System.out.println();System.out.println("Valid actions are \"run\", \"list\", \"info\", \"savepoint\", \"stop\", or \"cancel\".");System.out.println();System.out.println("Specify the version option (-v or --version) to print Flink version.");System.out.println();System.out.println("Specify the help option (-h or --help) to get help on the command.");return1;}}catch(CliArgsException ce){returnhandleArgException(ce);}catch(ProgramParametrizationException ppe){returnhandleParametrizationException(ppe);}catch(ProgramMissingJobException pmje){returnhandleMissingJobException();}catch(Exception e){returnhandleError(e);}}
/*TODO 根据之前添加的顺序,挨个判断是否active:Generic、Yarn、Default*/finalCustomCommandLine activeCommandLine =validateAndGetActiveCommandLine(checkNotNull(commandLine));/*TODO 根据之前添加的顺序:来自于CliFrontend中的:final List<CustomCommandLine> customCommandLines = *loadCustomCommandLines(
* configuration,
* configurationDirectory);
*/publicstaticList<CustomCommandLine>loadCustomCommandLines(Configuration configuration,String configurationDirectory){List<CustomCommandLine> customCommandLines =newArrayList<>();
customCommandLines.add(newGenericCLI(configuration, configurationDirectory));// Command line interface of the YARN session, with a special initialization here// to prefix all options with y/yarn.finalString flinkYarnSessionCLI ="org.apache.flink.yarn.cli.FlinkYarnSessionCli";try{
customCommandLines.add(loadCustomCommandLine(flinkYarnSessionCLI,
configuration,
configurationDirectory,"y","yarn"));}catch(NoClassDefFoundError|Exception e){finalString errorYarnSessionCLI ="org.apache.flink.yarn.cli.FallbackYarnSessionCli";try{
LOG.info("Loading FallbackYarnSessionCli");
customCommandLines.add(loadCustomCommandLine(errorYarnSessionCLI, configuration));}catch(Exception exception){
LOG.warn("Could not load CLI class {}.", flinkYarnSessionCLI, e);}}// Tips: DefaultCLI must be added at last, because getActiveCustomCommandLine(..) will get the// active CustomCommandLine in order and DefaultCLI isActive always return true.
customCommandLines.add(newDefaultCLI());return customCommandLines;}//现在回到run方法当中:validateAndGetActiveCommandLine()