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内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

sqlite3 命令行工具使用指南

《sqlite3命令行工具使用指南》本文系统介绍sqlite3CLI的启动、数据库操作、元数据查询、数据导入导出及输出格式化命令,涵盖文件管理、备份恢复、性能统计等实用功能,并说明命令分类、SQL语... 目录一、启动与退出二、数据库与文件操作三、元数据查询四、数据操作与导入导出五、查询输出格式化六、实用功

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决