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

2023-12-03 16:12

本文主要是介绍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/450050

相关文章

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

Java通过反射获取方法参数名的方式小结

《Java通过反射获取方法参数名的方式小结》这篇文章主要为大家详细介绍了Java如何通过反射获取方法参数名的方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、前言2、解决方式方式2.1: 添加编译参数配置 -parameters方式2.2: 使用Spring的内部工具类 -

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php