Flink提交任务(第一篇)——打包任务程序


针对flink-1.7-release

1. 引言

Flink客户端通过命令行的形式提交任务时,即执行./flink run xxxx.jar时,真正执行的run逻辑如下:

protected void run(String[] args) throws Exception {
   
		LOG.info("Running 'run' command.");

		final Options commandOptions = CliFrontendParser.getRunCommandOptions();

		final Options commandLineOptions = CliFrontendParser.mergeOptions(commandOptions, customCommandLineOptions);

		final CommandLine commandLine = CliFrontendParser.parse(commandLineOptions, args, true);

		// 运行参数
		final RunOptions runOptions = new RunOptions(commandLine);

		// 判断下是否是help操作
		if (runOptions.isPrintHelp()) {
   
			CliFrontendParser.printHelpForRun(customCommandLines);
			return;
		}

		// 必须要指定任务的jar包路径
		if (runOptions.getJarFilePath() == null) {
   
			throw new CliArgsException("The program JAR file was not specified.");
		}
        // 初始化打包的任务执行程序
		final PackagedProgram program;
		try {
   
			LOG.info("Building program from JAR file");
			program = buildProgram(runOptions);
		}
		catch (FileNotFoundException e) {
   
			throw new CliArgsException("Could not build the program from JAR file.", e);
		}

		final CustomCommandLine<?> customCommandLine = getActiveCustomCommandLine(commandLine);

		try {
   
			// 执行任务程序
			runProgram(customCommandLine, commandLine, runOptions, program);
		} finally {
   
			program.deleteExtractedLibraries();
		}
	}

这段逻辑主要做了两件事情,首先,就是构建执行程序包;然后执行任务程序包。我们本文就重点分析下:program = buildProgram(runOptions);这段逻辑。怎样通过run的命令行参数来打包程序。

2. buildProgram的执行逻辑

继续分析 buildProgram(runOptions)的逻辑:

/**
  * 从给定的命令行参数中创建打包程序
  *
  * @return A PackagedProgram (upon success)
  */
PackagedProgram buildProgram(ProgramOptions options) throws FileNotFoundException, ProgramInvocationException {
   
    // 程序运行参数
    String[] programArgs = options.getProgramArgs();
    // 程序jar包路径
    String jarFilePath = options.getJarFilePath();
    // 依赖的classpaths
    List<URL> classpaths = options.getClasspaths();
    // 程序jar包不能为空
    if (jarFilePath == null) {
   
        throw new IllegalArgumentException("The program JAR file was not specified.");
    }
   
    File jarFile = new File(jarFilePath);

    // 程序jar包路径必须存在
    if (!jarFile.exists()) {
   
        throw new FileNotFoundException("JAR file does not exist: " + jarFile);
    }
    else if (!jarFile.isFile()) {
   
        throw new FileNotFoundException("JAR file is not a file: " + jarFile);
    }

    // 程序执行入口函数
    String entryPointClass = options.getEntryPointClassName();
    
    // 打包
    PackagedProgram program = entryPointClass == null ?
        new PackagedProgram(jarFile, classpaths, programArgs) :
    new PackagedProgram(jarFile, classpaths, entryPointClass, programArgs);
    // 设置包的SavePoint
    program.setSavepointRestoreSettings(options.
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值