Flume开发 -- 自定义Source

一、自定义 Source 介绍

Source 是负责接收数据到 Flume Agent 的组件。Source 组件可以处理各种类型、各种格式的日志数据,包括 avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy。官方提供的 source 类型已经很多,但是有时候并不能满足实际开发当中的需求,此时我们就需要根据实际需求自定义某些 source。

官方也提供了自定义 source 的接口:
https://flume.apache.org/FlumeDeveloperGuide.html#source
根据官方说明自定义 MySource 需要继承 AbstractSource 类并实现 Configurable 和 PollableSource 接口。

实现相应方法:
getBackOffSleepIncrement() //暂不用
getMaxBackOffSleepInterval() //暂不用
configure(Context context) //初始化 context(读取配置文件内容)
process() //获取数据封装成 event 并写入 channel,这个方法将被循环调用。

使用场景:读取 MySQL 数据或者其他文件系统。

二、需求

使用 flume 接收数据,并给每条数据添加前缀,输出到控制台。前缀可以从 flume 配置文件中配置。
在这里插入图片描述

三、分析

在这里插入图片描述

四、案例实操

4.1 环境配置

创建 maven 项目,添加 pom 依赖。

<dependencies>     
	<dependency>         	
		<groupId>org.apache.flume</groupId>
		<artifactId>flume-ng-core</artifactId>         	
		<version>1.7.0</version> 
	</dependency> 
</dependencies>

4.2 编写代码

package customersource;

import org.apache.flume.Context;
import org.apache.flume.EventDeliveryException;
import org.apache.flume.PollableSource;
import org.apache.flume.conf.Configurable;
import org.apache.flume.event.SimpleEvent;
import org.apache.flume.source.AbstractSource;

import java.util.HashMap;

/**
 * @description: 自定义 Source
 * @author: hyr
 * @time: 2020/3/4 8:27
 */
public class MySource extends AbstractSource implements Configurable, PollableSource {
    // 定义配置文件将来要读取的字段
    private Long delay;
    private String field;

    // 初始化配置信息
    @Override
    public void configure(Context context) {
        // 无默认值
        delay = context.getLong("delay");
        // 有默认值
        field = context.getString("field", "Hello!");
    }

    /*
        1、接收数据(for 循环造数据)
        2、封装为事件
        3、将事件传给 channel
     */
    @Override
    public Status process() throws EventDeliveryException {
        try {
            // 创建事件头信息
            HashMap<String, String> headerMap = new HashMap<>();

            // 创建事件
            SimpleEvent event = new SimpleEvent();
            // 循环封装事件
            for (int i = 0; i < 5; i++) {
                // 在源码中,SimpleEvent 的构造函数中,初始化了一个 HashMap。
                // 若我们需要传入头信息,需要自己封装一个 HashMap。
                event.setHeaders(headerMap);

                // 给事件设置内容
                // 要传入一个字节数组
                event.setBody((field + i).getBytes());

                // 将事件写入 channel
                getChannelProcessor().processEvent(event);

                // 设置休眠时间
                Thread.sleep(delay);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Status.BACKOFF;
        }

        // 返回状态
        return Status.READY;
    }

    @Override
    public long getBackOffSleepIncrement() {
        return 0;
    }

    @Override
    public long getMaxBackOffSleepInterval() {
        return 0;
    }
}

4.3 打包

用 maven 将代码打成 jar 包,上传到服务器的 flume 的 lib 目录下。

4.4 编写 mySource.conf

# Name the components on this agent 
a1.sources = r1 
a1.sinks = k1 
a1.channels = c1 
 
# Describe/configure the source 
a1.sources.r1.type = customersource.MySource
a1.sources.r1.delay = 1000 
# a1.sources.r1.field = helloworld
 
# Describe the sink 
a1.sinks.k1.type = logger 
 
# Use a channel which buffers events in memory 
a1.channels.c1.type = memory 
a1.channels.c1.capacity = 1000 
a1.channels.c1.transactionCapacity = 100 
 
# Bind the source and sink to the channel 
a1.sources.r1.channels = c1 
a1.sinks.k1.channel = c1 

4.5 开启 Flume

[test@hadoop151 flume]$ bin/flume-ng agent --conf conf/ --name a1 --conf-file job/mySource.conf -Dflume.root.logger=INFO,console

4.6 结果展示

1、使用默认前缀
在这里插入图片描述

2、使用自定义前缀
将配置文件中,下面这一行的注释删掉即可。

# a1.sources.r1.field = helloworld

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值