flink源码分析 - 命令行参数解析-CommandLineParser

2023-12-03 18:01

本文主要是介绍flink源码分析 - 命令行参数解析-CommandLineParser,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

flink版本: flink-1.11.2

调用位置:   

org.apache.flink.runtime.entrypoint.StandaloneSessionClusterEntrypoint#main

代码位置:

flink核心命令行解析器:

        org.apache.flink.runtime.entrypoint.parser.CommandLineParser 

/** Licensed to the Apache Software Foundation (ASF) under one* or more contributor license agreements.  See the NOTICE file* distributed with this work for additional information* regarding copyright ownership.  The ASF licenses this file* to you under the Apache License, Version 2.0 (the* "License"); you may not use this file except in compliance* with the License.  You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.apache.flink.runtime.entrypoint.parser;import org.apache.flink.runtime.entrypoint.FlinkParseException;import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;import javax.annotation.Nonnull;/*** Command line parser which produces a result from the given* command line arguments.*/
public class CommandLineParser<T> {@Nonnullprivate final ParserResultFactory<T> parserResultFactory;public CommandLineParser(@Nonnull ParserResultFactory<T> parserResultFactory) {// TODO_MA 注释: parserResultFactory = EntrypointClusterConfigurationParserFactorythis.parserResultFactory = parserResultFactory;}public T parse(@Nonnull String[] args) throws FlinkParseException {final DefaultParser parser = new DefaultParser();final Options options = parserResultFactory.getOptions();final CommandLine commandLine;try {/************************************************** TODO_MA 马中华 https://blog.csdn.net/zhongqi2513*  注释: 解析参数*/commandLine = parser.parse(options, args, true);} catch (ParseException e) {throw new FlinkParseException("Failed to parse the command line arguments.", e);}// TODO_MA 注释: 创建 EntrypointClusterConfiguration 返回return parserResultFactory.createResult(commandLine);}public void printHelp(@Nonnull String cmdLineSyntax) {final HelpFormatter helpFormatter = new HelpFormatter();helpFormatter.setLeftPadding(5);helpFormatter.setWidth(80);helpFormatter.printHelp(cmdLineSyntax, parserResultFactory.getOptions(), true);}
}

        其中核心方法是构造方法及parse方法。

        构造方法主要用于从外界获取parserResultFactory变量,用于后期解析;

        parse(@Nonnull String[] args) 方法用于解析参数。 其中args即为从外部命令行传入的参数。

解析过程中用到的核心对象是 

final DefaultParser parser = new DefaultParser();

该对象来源于 Apache Common Cli包,具体用法参考(内部包含官方文档地址):

使用Apache commons-cli包进行命令行参数解析的示例代码-CSDN博客

核心解析步骤是:

  commandLine = parser.parse(options, args, true);  以及

return parserResultFactory.createResult(commandLine);。

其中parser对象进行参数解析。  parserResultFactory主要为parser对象提供需要解析的命令行选项options,及通过 parserResultFactory.createResult(commandLine) 从parser的解析结果commandLine中拿到命令行选项对应值,并构造出相应结果。

ParserResultFactory的接口定义:
/** Licensed to the Apache Software Foundation (ASF) under one* or more contributor license agreements.  See the NOTICE file* distributed with this work for additional information* regarding copyright ownership.  The ASF licenses this file* to you under the Apache License, Version 2.0 (the* "License"); you may not use this file except in compliance* with the License.  You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.apache.flink.runtime.entrypoint.parser;import org.apache.flink.runtime.entrypoint.FlinkParseException;import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;import javax.annotation.Nonnull;/*** Parser result factory used by the {@link CommandLineParser}.** @param <T> type of the parsed result*/
public interface ParserResultFactory<T> {/*** Returns all relevant {@link Options} for parsing the command line* arguments.** @return Options to use for the parsing*/Options getOptions();/*** Create the result of the command line argument parsing.** @param commandLine to extract the options from* @return Result of the parsing* @throws FlinkParseException Thrown on failures while parsing command line arguments*/T createResult(@Nonnull CommandLine commandLine) throws FlinkParseException;
}

其下具体实现类如图所示:

截取两个具体实现供参考:

ClusterConfigurationParserFactory:
/** Licensed to the Apache Software Foundation (ASF) under one* or more contributor license agreements.  See the NOTICE file* distributed with this work for additional information* regarding copyright ownership.  The ASF licenses this file* to you under the Apache License, Version 2.0 (the* "License"); you may not use this file except in compliance* with the License.  You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.apache.flink.runtime.entrypoint;import org.apache.flink.runtime.entrypoint.parser.ParserResultFactory;import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;import javax.annotation.Nonnull;import java.util.Properties;import static org.apache.flink.runtime.entrypoint.parser.CommandLineOptions.CONFIG_DIR_OPTION;
import static org.apache.flink.runtime.entrypoint.parser.CommandLineOptions.DYNAMIC_PROPERTY_OPTION;/*** Parser factory which generates a {@link ClusterConfiguration} from the given* list of command line arguments.*/
public class ClusterConfigurationParserFactory implements ParserResultFactory<ClusterConfiguration> {public static Options options() {final Options options = new Options();options.addOption(CONFIG_DIR_OPTION);options.addOption(DYNAMIC_PROPERTY_OPTION);return options;}@Overridepublic Options getOptions() {return options();}@Overridepublic ClusterConfiguration createResult(@Nonnull CommandLine commandLine) {final String configDir = commandLine.getOptionValue(CONFIG_DIR_OPTION.getOpt());final Properties dynamicProperties = commandLine.getOptionProperties(DYNAMIC_PROPERTY_OPTION.getOpt());return new ClusterConfiguration(configDir, dynamicProperties, commandLine.getArgs());}
}
EntrypointClusterConfigurationParserFactory:
/** Licensed to the Apache Software Foundation (ASF) under one* or more contributor license agreements.  See the NOTICE file* distributed with this work for additional information* regarding copyright ownership.  The ASF licenses this file* to you under the Apache License, Version 2.0 (the* "License"); you may not use this file except in compliance* with the License.  You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.apache.flink.runtime.entrypoint;import org.apache.flink.runtime.entrypoint.parser.ParserResultFactory;import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;import javax.annotation.Nonnull;import java.util.Properties;import static org.apache.flink.runtime.entrypoint.parser.CommandLineOptions.CONFIG_DIR_OPTION;
import static org.apache.flink.runtime.entrypoint.parser.CommandLineOptions.DYNAMIC_PROPERTY_OPTION;
import static org.apache.flink.runtime.entrypoint.parser.CommandLineOptions.EXECUTION_MODE_OPTION;
import static org.apache.flink.runtime.entrypoint.parser.CommandLineOptions.HOST_OPTION;
import static org.apache.flink.runtime.entrypoint.parser.CommandLineOptions.REST_PORT_OPTION;/*** Parser factory for {@link EntrypointClusterConfiguration}.*/
public class EntrypointClusterConfigurationParserFactory implements ParserResultFactory<EntrypointClusterConfiguration> {@Overridepublic Options getOptions() {final Options options = new Options();options.addOption(CONFIG_DIR_OPTION);options.addOption(REST_PORT_OPTION);options.addOption(DYNAMIC_PROPERTY_OPTION);options.addOption(HOST_OPTION);options.addOption(EXECUTION_MODE_OPTION);return options;}@Overridepublic EntrypointClusterConfiguration createResult(@Nonnull CommandLine commandLine) {// TODO_MA 注释: 解析 --configDir  -cfinal String configDir = commandLine.getOptionValue(CONFIG_DIR_OPTION.getOpt());// TODO_MA 注释: 解析程序的 -Dkey-value参数final Properties dynamicProperties = commandLine.getOptionProperties(DYNAMIC_PROPERTY_OPTION.getOpt());// TODO_MA 注释: 解析 --webui-port -rfinal String restPortStr = commandLine.getOptionValue(REST_PORT_OPTION.getOpt(), "-1");final int restPort = Integer.parseInt(restPortStr);// TODO_MA 注释: 解析 --host -hfinal String hostname = commandLine.getOptionValue(HOST_OPTION.getOpt());/************************************************** TODO_MA 马中华 https://blog.csdn.net/zhongqi2513*  注释: 返回一个 EntrypointClusterConfiguration 对象*/return new EntrypointClusterConfiguration(configDir,dynamicProperties,commandLine.getArgs(),hostname,restPort);}
}

        

这篇关于flink源码分析 - 命令行参数解析-CommandLineParser的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/450337

相关文章

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

Linux内核参数配置与验证详细指南

《Linux内核参数配置与验证详细指南》在Linux系统运维和性能优化中,内核参数(sysctl)的配置至关重要,本文主要来聊聊如何配置与验证这些Linux内核参数,希望对大家有一定的帮助... 目录1. 引言2. 内核参数的作用3. 如何设置内核参数3.1 临时设置(重启失效)3.2 永久设置(重启仍生效

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Java字符串处理全解析(String、StringBuilder与StringBuffer)

《Java字符串处理全解析(String、StringBuilder与StringBuffer)》:本文主要介绍Java字符串处理全解析(String、StringBuilder与StringBu... 目录Java字符串处理全解析:String、StringBuilder与StringBuffer一、St

Spring Boot循环依赖原理、解决方案与最佳实践(全解析)

《SpringBoot循环依赖原理、解决方案与最佳实践(全解析)》循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系,:本文主要介绍SpringBoot循环依赖原理、解决方案与最... 目录一、循环依赖的本质与危害1.1 什么是循环依赖?1.2 核心危害二、Spring的三级缓存机制2.1 三

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

SpringShell命令行之交互式Shell应用开发方式

《SpringShell命令行之交互式Shell应用开发方式》本文将深入探讨SpringShell的核心特性、实现方式及应用场景,帮助开发者掌握这一强大工具,具有很好的参考价值,希望对大家有所帮助,如... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S