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

相关文章

网页解析 lxml 库--实战

lxml库使用流程 lxml 是 Python 的第三方解析库,完全使用 Python 语言编写,它对 XPath表达式提供了良好的支 持,因此能够了高效地解析 HTML/XML 文档。本节讲解如何通过 lxml 库解析 HTML 文档。 pip install lxml lxm| 库提供了一个 etree 模块,该模块专门用来解析 HTML/XML 文档,下面来介绍一下 lxml 库

性能分析之MySQL索引实战案例

文章目录 一、前言二、准备三、MySQL索引优化四、MySQL 索引知识回顾五、总结 一、前言 在上一讲性能工具之 JProfiler 简单登录案例分析实战中已经发现SQL没有建立索引问题,本文将一起从代码层去分析为什么没有建立索引? 开源ERP项目地址:https://gitee.com/jishenghua/JSH_ERP 二、准备 打开IDEA找到登录请求资源路径位置

Andrej Karpathy最新采访:认知核心模型10亿参数就够了,AI会打破教育不公的僵局

夕小瑶科技说 原创  作者 | 海野 AI圈子的红人,AI大神Andrej Karpathy,曾是OpenAI联合创始人之一,特斯拉AI总监。上一次的动态是官宣创办一家名为 Eureka Labs 的人工智能+教育公司 ,宣布将长期致力于AI原生教育。 近日,Andrej Karpathy接受了No Priors(投资博客)的采访,与硅谷知名投资人 Sara Guo 和 Elad G

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件: 2.调用utility bar api的方式有两种: 方法一,通过lwc调用: import {LightningElement,api ,wire } from 'lwc';import { publish, MessageContext } from 'lightning/messageService';import Ca

4B参数秒杀GPT-3.5:MiniCPM 3.0惊艳登场!

​ 面壁智能 在 AI 的世界里,总有那么几个时刻让人惊叹不已。面壁智能推出的 MiniCPM 3.0,这个仅有4B参数的"小钢炮",正在以惊人的实力挑战着 GPT-3.5 这个曾经的AI巨人。 MiniCPM 3.0 MiniCPM 3.0 MiniCPM 3.0 目前的主要功能有: 长上下文功能:原生支持 32k 上下文长度,性能完美。我们引入了

Java ArrayList扩容机制 (源码解读)

结论:初始长度为10,若所需长度小于1.5倍原长度,则按照1.5倍扩容。若不够用则按照所需长度扩容。 一. 明确类内部重要变量含义         1:数组默认长度         2:这是一个共享的空数组实例,用于明确创建长度为0时的ArrayList ,比如通过 new ArrayList<>(0),ArrayList 内部的数组 elementData 会指向这个 EMPTY_EL

如何在Visual Studio中调试.NET源码

今天偶然在看别人代码时,发现在他的代码里使用了Any判断List<T>是否为空。 我一般的做法是先判断是否为null,再判断Count。 看了一下Count的源码如下: 1 [__DynamicallyInvokable]2 public int Count3 {4 [__DynamicallyInvokable]5 get