flume加载配置

本文介绍了一个用于解析Flume配置文件的Java类PropertiesFileConfigurationProvider。该类通过读取指定的属性文件来创建FlumeConfiguration对象,并对配置项进行验证。
PropertiesFileConfigurationProvider.java
@Override
  public FlumeConfiguration getFlumeConfiguration() {
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new FileReader(file));
      Properties properties = new Properties();
      properties.load(reader);
      return new FlumeConfiguration(toMap(properties));
    } catch (IOException ex) {
      LOGGER.error("Unable to load file:" + file
          + " (I/O failure) - Exception follows.", ex);
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException ex) {
          LOGGER.warn(
              "Unable to close file reader for file: " + file, ex);
        }
      }
    }
    return new FlumeConfiguration(new HashMap<String, String>());
  }


private Map<String, String> toMap(Properties properties) {
    Map<String, String> result = Maps.newHashMap();
    Enumeration<?> propertyNames = properties.propertyNames();
    while (propertyNames.hasMoreElements()) {
      String name = (String) propertyNames.nextElement();
      String value = properties.getProperty(name);
      result.put(name, value);
    }
    return result;
  }

  /**
   * Creates a populated Flume Configuration object.
   */
  public FlumeConfiguration(Map<String, String> properties) {
    agentConfigMap = new HashMap<String, AgentConfiguration>();
    errors = new LinkedList<FlumeConfigurationError>();
    // Construct the in-memory component hierarchy
    for(String name : properties.keySet()) {
      String value = properties.get(name);

      if (!addRawProperty(name, value)) {
        logger.warn("Configuration property ignored: " + name + " = " + value);
      }
    }
    // Now iterate thru the agentContext and create agent configs and add them
    // to agentConfigMap

    // validate and remove improperly configured components
    validateConfiguration();
  }

 private boolean addRawProperty(String name, String value) {
    // Null names and values not supported
    if (name == null || value == null) {
      errors
      .add(new FlumeConfigurationError("", "",
          FlumeConfigurationErrorType.AGENT_NAME_MISSING,
          ErrorOrWarning.ERROR));
      return false;
    }

    // Empty values are not supported
    if (value.trim().length() == 0) {
      errors
      .add(new FlumeConfigurationError(name, "",
          FlumeConfigurationErrorType.PROPERTY_VALUE_NULL,
          ErrorOrWarning.ERROR));
      return false;
    }

    // Remove leading and trailing spaces
    name = name.trim();
    value = value.trim();

    int index = name.indexOf('.');

    // All configuration keys must have a prefix defined as agent name
    if (index == -1) {
      errors
      .add(new FlumeConfigurationError(name, "",
          FlumeConfigurationErrorType.AGENT_NAME_MISSING,
          ErrorOrWarning.ERROR));
      return false;
    }

    String agentName = name.substring(0, index);

    // Agent name must be specified for all properties
    if (agentName.length() == 0) {
      errors
      .add(new FlumeConfigurationError(name, "",
          FlumeConfigurationErrorType.AGENT_NAME_MISSING,
          ErrorOrWarning.ERROR));
      return false;
    }

    String configKey = name.substring(index + 1);

    // Configuration key must be specified for every property
    if (configKey.length() == 0) {
      errors
      .add(new FlumeConfigurationError(name, "",
          FlumeConfigurationErrorType.PROPERTY_NAME_NULL,
          ErrorOrWarning.ERROR));
      return false;
    }

    AgentConfiguration aconf = agentConfigMap.get(agentName);

    if (aconf == null) {
      aconf = new AgentConfiguration(agentName, errors);
      agentConfigMap.put(agentName, aconf);
    }

    // Each configuration key must begin with one of the three prefixes:
    // sources, sinks, or channels.
    return aconf.addProperty(configKey, value);
  }

 

### Apache Flume 安装与配置方法 #### 一、Flume 简介 Apache Flume 是一种分布式、可靠且高效的系统,专门设计用于从多个源收集日志数据并将其传输到集中存储位置。它支持高可用性和可扩展性,适用于大规模的数据流管理[^3]。 目前存在两个主要版本系列:Flume OG (Original Generation, 版本 0.9.x) 和 Flume NG (Next Generation, 版本 1.x)[^5]。本文讨论的是更现代的 Flume NG 版本(如 1.9.0),其功能更为强大且广泛应用于生产环境。 --- #### 二、Flume 的安装过程 ##### 解压压缩包 下载完成后,可以使用以下命令将 `apache-flume-1.9.0-bin.tar.gz` 文件解压至目标目录 `/your/desired/path/`: ```bash tar -xzf apache-flume-1.9.0-bin.tar.gz -C /your/desired/path/ ``` 此操作会创建一个名为 `apache-flume-1.9.0-bin` 的文件夹,其中包含了 Flume 所需的所有组件和依赖项[^4]。 ##### 设置环境变量 为了方便调用 Flume 命令,在 `.bashrc` 或其他 shell 配置文件中添加如下内容: ```bash export FLUME_HOME=/your/desired/path/apache-flume-1.9.0-bin export PATH=$FLUME_HOME/bin:$PATH ``` 执行以下命令使更改生效: ```bash source ~/.bashrc ``` 验证安装是否成功可以通过运行以下命令来测试: ```bash flume-ng version ``` 如果显示了 Flume 的版本号,则说明安装完成。 --- #### 三、Flume 的基本配置 Flume 的核心在于通过配置文件定义数据流动的方式。以下是关于如何编写和加载配置文件的关键点。 ##### 配置文件结构 Flume配置文件通常命名为类似于 `flume-conf.properties`,位于 `$FLUME_HOME/conf` 目录下。该文件中的每一部分都描述了一个特定的任务或通道设置。例如: ```properties # 定义 agent 名称为 myAgent agentName.sources = source1 agentName.sinks = sink1 agentName.channels = channel1 # Source 配置 agentName.sources.source1.type = netcat agentName.sources.source1.bind = localhost agentName.sources.source1.port = 44444 # Sink 配置 agentName.sinks.sink1.type = logger # Channel 配置 agentName.channels.channel1.type = memory agentName.channels.channel1.capacity = 1000 agentName.channels.channel1.transactionCapacity = 100 # 将 Source 和 Sink 连接到同一个 Channel agentName.sources.source1.channels = channel1 agentName.sinks.sink1.channel = channel1 ``` 上述示例展示了如何配置一个简单的 NetCat 数据源 (`netcat`) 并将其写入内存通道 (`memory`) 中,最终输出到控制台 (`logger`)[^1]。 ##### 启动 Flume Agent 启动 Flume Agent 可以通过以下命令实现: ```bash flume-ng agent --conf $FLUME_HOME/conf \ --conf-file /path/to/flume-conf.properties \ --name agentName \ -Dflume.root.logger=INFO,console ``` 这一步指定了使用的配置文件路径以及对应的 Agent 名称。 --- #### 四、高级主题 对于复杂场景下的应用开发,建议查阅官方文档《Flume 1.9.0 Developer Guide》[^2],其中包括但不限于插件机制、性能优化策略等内容。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值