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

flink版本: flink-1.11.2

提取主类代码位置: org.apache.flink.api.java.utils.MultipleParameterTool#fromArgs

代码逻辑比较简单,此处不再赘述,在此记录方便后续使用

完整代码:

/** 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.api.java.utils;import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.api.java.Utils;
import org.apache.flink.util.Preconditions;import org.apache.commons.lang3.math.NumberUtils;import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;/*** This class provides simple utility methods for reading and parsing program arguments from* different sources. Multiple values parameter in args could be supported. For example, --multi* multiValue1 --multi multiValue2. If {@link MultipleParameterTool} object is used for* GlobalJobParameters, the last one of multiple values will be used. Navigate to {@link #toMap()}* for more information.*/
@PublicEvolving
public class MultipleParameterTool extends AbstractParameterTool {private static final long serialVersionUID = 1L;// ------------------ Constructors ------------------------/*** Returns {@link MultipleParameterTool} for the given arguments. The arguments are keys* followed by values. Keys have to start with '-' or '--'** <p><strong>Example arguments:</strong> --key1 value1 --key2 value2 -key3 value3 --multi* multiValue1 --multi multiValue2** @param args Input array arguments* @return A {@link MultipleParameterTool}*/public static MultipleParameterTool fromArgs(String[] args) {final Map<String, Collection<String>> map = new HashMap<>(args.length / 2);int i = 0;while (i < args.length) {final String key = Utils.getKeyFromArgs(args, i);i += 1; // try to find the valuemap.putIfAbsent(key, new ArrayList<>());if (i >= args.length) {map.get(key).add(NO_VALUE_KEY);} else if (NumberUtils.isNumber(args[i])) {map.get(key).add(args[i]);i += 1;} else if (args[i].startsWith("--") || args[i].startsWith("-")) {// the argument cannot be a negative number because we checked earlier// -> the next argument is a parameter namemap.get(key).add(NO_VALUE_KEY);} else {map.get(key).add(args[i]);i += 1;}}return fromMultiMap(map);}/*** Returns {@link MultipleParameterTool} for the given multi map.** @param multiMap A map of arguments. Key is String and value is a Collection.* @return A {@link MultipleParameterTool}*/public static MultipleParameterTool fromMultiMap(Map<String, Collection<String>> multiMap) {Preconditions.checkNotNull(multiMap, "Unable to initialize from empty map");return new MultipleParameterTool(multiMap);}// ------------------ ParameterUtil  ------------------------protected final Map<String, Collection<String>> data;private MultipleParameterTool(Map<String, Collection<String>> data) {this.data = Collections.unmodifiableMap(new HashMap<>(data));this.defaultData = new ConcurrentHashMap<>(data.size());this.unrequestedParameters =Collections.newSetFromMap(new ConcurrentHashMap<>(data.size()));unrequestedParameters.addAll(data.keySet());}@Overridepublic boolean equals(Object o) {if (this == o) {return true;}if (o == null || getClass() != o.getClass()) {return false;}MultipleParameterTool that = (MultipleParameterTool) o;return Objects.equals(data, that.data)&& Objects.equals(defaultData, that.defaultData)&& Objects.equals(unrequestedParameters, that.unrequestedParameters);}@Overridepublic int hashCode() {return Objects.hash(data, defaultData, unrequestedParameters);}// ------------------ Get data from the util ----------------/** Returns number of parameters in {@link ParameterTool}. */@Overridepublic int getNumberOfParameters() {return data.size();}/*** Returns the String value for the given key. The value should only have one item. Use {@link* #getMultiParameter(String)} instead if want to get multiple values parameter. If the key does* not exist it will return null.*/@Overridepublic String get(String key) {addToDefaults(key, null);unrequestedParameters.remove(key);if (!data.containsKey(key)) {return null;}Preconditions.checkState(data.get(key).size() == 1, "Key %s should has only one value.", key);return (String) data.get(key).toArray()[0];}/** Check if value is set. */@Overridepublic boolean has(String value) {addToDefaults(value, null);unrequestedParameters.remove(value);return data.containsKey(value);}/*** Returns the Collection of String values for the given key. If the key does not exist it will* return null.*/public Collection<String> getMultiParameter(String key) {addToDefaults(key, null);unrequestedParameters.remove(key);return data.getOrDefault(key, null);}/*** Returns the Collection of String values for the given key. If the key does not exist it will* throw a {@link RuntimeException}.*/public Collection<String> getMultiParameterRequired(String key) {addToDefaults(key, null);Collection<String> value = getMultiParameter(key);if (value == null) {throw new RuntimeException("No data for required key '" + key + "'");}return value;}// ------------------------- Export to different targets -------------------------/*** Return MultiMap of all the parameters processed by {@link MultipleParameterTool}.** @return MultiMap of the {@link MultipleParameterTool}. Key is String and Value is a*     Collection of String.*/public Map<String, Collection<String>> toMultiMap() {return data;}@Overrideprotected Object clone() throws CloneNotSupportedException {return new MultipleParameterTool(this.data);}// ------------------------- Interaction with other ParameterUtils -------------------------/*** Merges two {@link MultipleParameterTool}.** @param other Other {@link MultipleParameterTool} object* @return The Merged {@link MultipleParameterTool}*/public MultipleParameterTool mergeWith(MultipleParameterTool other) {final Map<String, Collection<String>> resultData =new HashMap<>(data.size() + other.data.size());resultData.putAll(data);other.data.forEach((key, value) -> {resultData.putIfAbsent(key, new ArrayList<>());resultData.get(key).addAll(value);});final MultipleParameterTool ret = new MultipleParameterTool(resultData);final HashSet<String> requestedParametersLeft = new HashSet<>(data.keySet());requestedParametersLeft.removeAll(unrequestedParameters);final HashSet<String> requestedParametersRight = new HashSet<>(other.data.keySet());requestedParametersRight.removeAll(other.unrequestedParameters);ret.unrequestedParameters.removeAll(requestedParametersLeft);ret.unrequestedParameters.removeAll(requestedParametersRight);return ret;}// ------------------------- ExecutionConfig.UserConfig interface -------------------------@Overridepublic Map<String, String> toMap() {return getFlatMapOfData(data);}/*** Get the flat map of the multiple map data. If the key have multiple values, only the last one* will be used. This is also the current behavior when multiple parameters is specified for* {@link ParameterTool}.** @param data multiple map of data.* @return flat map of data.*/private static Map<String, String> getFlatMapOfData(Map<String, Collection<String>> data) {return data.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,e -> {if (e.getValue().size() > 0) {return (String)e.getValue().toArray()[e.getValue().size() - 1];} else {return NO_VALUE_KEY;}}));}// ------------------------- Serialization ---------------------------------------------private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {in.defaultReadObject();defaultData = new ConcurrentHashMap<>(data.size());unrequestedParameters = Collections.newSetFromMap(new ConcurrentHashMap<>(data.size()));}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/652532.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

浅聊 DNS 和 host

我们先来了解一下访问一个网站的基本流程 我们访问一个网站&#xff0c;自然就是访问网站的服务器&#xff0c;但是访问一个网站的服务器&#xff0c;自然要知道它的地址&#xff0c;服务器的地址就是一串数字&#xff0c;如 也就是我们说的 ip 地址&#xff0c;输入 i…

Redis的数据类型

目录 string 1.编码方式 2.应用场景 3.常用命令 hash 1.编码方式 2.应用场景 3.常用命令 list 1.编码方式 2.应用场景 3.常用命令 set 1.编码方式 2.应用场景 3.常用命令 zset 1.编码方式 2.应用场景 3.常用命令 如何理解Redis的编码方式 embs…

MySQL设计开发使用规范

数据库设计 库名 【强制】库的名称格式: 业务系统名称、业务系统名称子系统名。如: aimall , aimall_op【强制】创建数据库时必须显式指定字符集,并且字符集只能是是utf8或者utf8mb4【建议】库的名称必须控制在20个字符以内【强制】单实例表个数必须控制在2000个以内【强制】…

以太网交换基础VLAN原理与配置

目录 7.以太网交换基础 7.1.以太网协议 7.2.以太网帧介绍 7.3.以太网交换机 7.4.同网段数据通信全过程 8.VLAN原理与配置 8.1.VLAN的基本概念 8.2.VLAN的应用 7.以太网交换基础 7.1.以太网协议 以太网是当今现有局域网(Local Area Network,LAN)采用的最通用的通信协议…

Linux进程间通信方法和代码示例

Linux进程间通信&#xff08;IPC, Inter-Process Communication&#xff09;包括了多种不同的技术&#xff0c;例如管道&#xff08;pipe&#xff09;、信号&#xff08;signal&#xff09;、共享内存&#xff08;shared memory&#xff09;、消息队列&#xff08;message queu…

SpringBoot自定义全局异常处理器

文章目录 一、介绍二、实现1. 定义全局异常处理器2. 自定义异常类 三、使用四、疑问 一、介绍 Springboot框架提供两个注解帮助我们十分方便实现全局异常处理器以及自定义异常。 ControllerAdvice 或 RestControllerAdvice&#xff08;推荐&#xff09;ExceptionHandler 二、…

Python第 1 课 Python 介绍与安装

文章目录 第 1 课 Python 介绍与安装1.Python介绍1.1 面向对象概述1.2 Python 概述1.3 Python 特点 2.查看Python3.pyCharm 安装方法3.1 下载 pyCharm3.2 打开 pyCharm3.3 汉化 pyCharm3.4 pyCharm 的基本介绍和基本使用方法 第 1 课 Python 介绍与安装 1.Python介绍 1.1 面向…

Python爬虫库推荐

很多人学Python&#xff0c;都是从爬虫开始的&#xff0c;毕竟网上类似的资源很丰富&#xff0c;开源项目也非常多。 Python学习网络爬虫主要分3个大的版块&#xff1a; 抓取 &#xff0c; 分析 &#xff0c; 存储 当我们在浏览器中输入一个url后回车&#xff0c;后台会发生什…

消息中间件之八股面试回答篇:三、RabbitMQ如何解决消息堆积问题(100万条消息堆积)+RabbitMQ高可用性和强一致性机制+回答模板

RabbitMQ中的消息堆积问题 当生产者发送消息的速度超过了消费者处理消息的速度&#xff0c;就会导致队列中的消息堆积&#xff0c;直到队列存储消息达到上限。之后发送的消息就会成为死信&#xff0c;可能会被丢弃&#xff0c;这就是消息堆积问题。 解决消息堆积有三种种思路…

网络工程师必学知识:2、数据链路层-II型以太帧的封装

1.概述&#xff1a; 针对于链路层&#xff0c;华为官网IP报文格式大全里面包含了很多。如下图&#xff1a; 今天主要分析Ethernet II以太帧。 2.Frame Format&#xff1a; 12Byte&#xff08;inter frame gap&#xff09;|7B(同步码)|1B(定界符)|6B(DMAC)|6B(SMAC)|2B(Type)…

【Demo】基于CharacterController组件的角色控制

项目介绍 项目名称&#xff1a;Demo1 项目版本&#xff1a;1.0 游戏引擎&#xff1a;Unity2020.3.26f1c1 IDE&#xff1a;Visual Studio Code 关键词&#xff1a;Unity3D&#xff0c;CharacterController组件&#xff0c;角色控制&#xff0c;自定义按键&#xff0c;Scrip…

Rider 打开Unity项目 Project 全部显示 load failed

电脑自动更新&#xff0c;导致系统重启&#xff0c;第二天Rider打开Unity 工程&#xff0c;没有任何代码提示&#xff0c;字符串查找也失效。 现象&#xff1a; 1.所有的Project均显示laod failed。点击load failed。右侧信息显示Can not start process 2.选中解决方案进行Bui…

解决PyCharm的Terminal终端conda环境默认为base无法切换的问题

问题描述 在使用PyCharm的Terminal终端时&#xff0c;打开的默认环境为base。 在使用切换命令时&#xff0c;依旧无法解决。 解决方法 1、输入以下命令以查看conda的配置信息&#xff1a; conda config --show2、在输出中找到 auto_activate_base 的行&#xff0c;发现被…

IMX6ULL驱动学习——通过总线设备驱动模型点亮野火开发板小灯【参考韦东山老师教程】

参考&#xff1a;【IMX6ULL驱动开发学习】11.驱动设计之面向对象_分层思想&#xff08;学习设备树过渡部分&#xff09;-CSDN博客 韦东山课程&#xff1a;LED模板驱动程序的改造_总线设备驱动模型 我使用的开发板&#xff1a;野火imx6ull pro 欢迎大家一起讨论学习 实现了总线设…

uniapp 实现路由拦截,权限或者登录控制

背景&#xff1a; 项目需要判断token&#xff0c;即是否登录&#xff0c;登录之后权限 参考uni-app官方&#xff1a; 为了兼容其他端的跳转权限控制&#xff0c;uni-app并没有用vue router路由&#xff0c;而是内部实现一个类似此功能的钩子&#xff1a;拦截器&#xff0c;由…

数字图像处理(实践篇)三十三 OpenCV-Python从立体图像创建深度图实践

目录 一 方案 二 实践 双眼视觉是指人类使用两只眼睛同时观察同一场景,通过左右眼的视差来感知深度。左眼和右眼的视差是由于它们在空间中的位置不同而产生的,这种差异可以被大脑解读为物体的距离和深度。为了从立体图像构建深度图,找到两个图像之间的视差,可以初始化并创…

Java强训day7(选择题编程题)

选择题 public class Test01{private static int x 100;public static void main(String[] args) {Test01 hs1 new Test01();hs1.x;Test01 hs2 new Test01();hs2.x;hs1new Test01();hs1.x;Test01.x--;System.out.println("x"x);} }public class Test01{private …

倒排索引的构建与查询

倒排索引是信息检索的重要技术&#xff0c;本文将基于中文短信数据&#xff08;数据集可在本文所附资源处下载或点击此处链接下载&#xff09;&#xff0c;编程构建倒排索引并实现布尔查询。 1. 功能设计 用户输入查询&#xff0c;按下回车键&#xff0c;如果该查询作为单独的…

【Kafka】主题Topic详解

目录 主题的管理创建主题查看主题修改主题删除主题 增加分区分区副本的分配必要参数配置KafkaAdminClient应用功能操作示例 主题的管理 使用kafka-topics.sh脚本。 下面是使用脚本的一些选项 选项说明–config <String: namevalue>为创建的或修改的主题指定配置信息。…

Mybatis-Plus入门

Mybatis-Plus入门 MyBatis-Plus 官网&#xff1a;https://mp.baomidou.com/ 1、简介 MyBatis-Plus (简称 MP) 是一个 MyBatis 的增强工具&#xff0c;在 MyBatis 的基础上只做增强不做改变&#xff0c;为简化开发、 提高效率而生。 https://github.com/baomidou/mybatis-p…