Flink ParameterTool fromArgs源码分析

本文深入解析了Apache Flink中的SocketWindowWordCount示例程序,阐述了如何通过socket接收数据并进行实时词频统计,包括参数解析、数据流处理、窗口聚合等关键步骤。

一、源码路径

java/org/apache/flink/streaming/examples/socket/SocketWindowWordCount.java

二、源码

/*
 * 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.streaming.examples.socket;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

/**
 * Implements a streaming windowed version of the "WordCount" program.
 *
 * <p>This program connects to a server socket and reads strings from the socket.
 * The easiest way to try this out is to open a text server (at port 12345)
 * using the <i>netcat</i> tool via
 * <pre>
 * nc -l 12345
 * </pre>
 * and run this example with the hostname and the port as arguments.
 */
@SuppressWarnings("serial")
public class SocketWindowWordCount {

   pub
`ParameterTool.fromArgs` 是 Apache Flink 提供的一个工具方法,用于**解析命令行参数(即 main 方法中的 `args`)**。它通常用于 Flink 应用程序中,用于从命令行传入参数,例如 Kafka 的 broker 地址、输入输出路径、并行度等。 --- ## ✅ 一、`ParameterTool.fromArgs` 的作用 `ParameterTool` 是 Flink 提供的一个参数解析工具类,它支持从以下几种方式创建实例: - `fromArgs(String[] args)`:从命令行参数中解析 - `fromPropertiesFile(String filePath)`:从 properties 文件中读取 - `fromSystemProperties()`:从 JVM 的系统属性中读取 ### 📌 `fromArgs` 方法的作用: - 将命令行参数解析为键值对(key-value) - 支持标准的命令行格式,如 `--key=value` 或 `--key value` - 可以通过 `getParameter("key")` 获取参数值 --- ## ✅ 二、使用示例 ### 示例 1:基本使用 ```java public class MyFlinkJob { public static void main(String[] args) throws Exception { final ParameterTool params = ParameterTool.fromArgs(args); String input = params.get("input", "default-input-path"); String output = params.get("output", "default-output-path"); System.out.println("Input path: " + input); System.out.println("Output path: " + output); // 后续 Flink Job 的构建逻辑... } } ``` #### 命令行运行方式: ```bash flink run -c MyFlinkJob myjob.jar --input /user/input --output /user/output ``` #### 输出结果: ``` Input path: /user/input Output path: /user/output ``` --- ### 示例 2:使用 `--` 分隔符传递参数 Flink Job 支持在提交时使用 `--` 分隔符将参数传递给你的 Job: ```bash flink run -c MyFlinkJob myjob.jar --parallelism 2 -- input=/user/input output=/user/output ``` 此时,`args` 中的内容是: ```java ["--parallelism", "2", "--", "input=/user/input", "output=/user/output"] ``` 你可以这样处理: ```java String[] jobArgs = Arrays.stream(args) .dropWhile(arg -> !arg.equals("--")) .skip(1) .toArray(String[]::new); ParameterTool params = ParameterTool.fromArgs(jobArgs); ``` --- ## ✅ 三、`ParameterTool` 的常用方法 | 方法 | 说明 | |------|------| | `get(String key)` | 获取指定 key 的值,如果不存在则返回 null | | `get(String key, String defaultValue)` | 获取指定 key 的值,如果不存在则返回默认值 | | `getRequired(String key)` | 获取必须存在的 key,否则抛出异常 | | `has(String key)` | 判断是否存在该 key | | `toMap()` | 转换为 Map 形式 | | `getNumberOfParameters()` | 获取参数个数 | --- ## ✅ 四、与 `Configuration` 的集成 Flink 的 `StreamExecutionEnvironment` 支持将 `ParameterTool` 作为全局配置参数: ```java StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.getConfig().setGlobalJobParameters(params); ``` 然后你可以在 `RichFunction` 中通过 `getRuntimeContext().getExecutionConfig().getGlobalJobParameters()` 获取参数。 --- ## ✅ 五、注意事项 | 事项 | 说明 | |------|------| | 参数格式 | 支持 `--key=value` 或 `--key value` | | 参数顺序 | 参数顺序不影响解析 | | 默认值 | 推荐使用 `get(key, defaultValue)` 设置默认值 | | 多个参数 | 支持多个 key-value 对 | | 不支持嵌套结构 | `ParameterTool` 只是简单的 key-value 解析器,不支持嵌套或复杂结构 | --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值