【大数据之Flume】六、Flume进阶之自定义Source

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

  自定义source的接口:https://flume.apache.org/releases/content/1.11.0/FlumeDeveloperGuide.html
  自定义MySource 需要继承 AbstractSource 类并实现 Configurable 和 PollableSource 接口。

  实现的方法:
    getBackOffSleepIncrement() //backoff 步长
    getMaxBackOffSleepInterval()//backoff 最长时间
    configure(Context context)//初始化 context(读取配置文件内容)
    process()//获取数据封装成 event 并写入 channel,这个方法将被循环调用

  适用于:读取MySQL数据或者其它文件系统。

(2)需求:
  使用 flume 接收数据,并给每条数据添加前缀,输出到控制台,使得前缀后缀可从 flume 配置文件中配置。
在这里插入图片描述
(3)分析:
在这里插入图片描述
步骤:
(1)创建一个 maven 项目,并引入以下pom依赖。

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

(2)自定义MySource ,继承 AbstractSource 类并实现 Configurable 和 PollableSource 接口,并打包,将jar包放到/opt/module/flume-1.9.0/lib目录下。

package com.study.source;

import org.apache.flume.Context;
import org.apache.flume.Event;
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;

public class MySource extends AbstractSource implements Configurable, PollableSource {
    
    

    private String perfix;
    private String subfix;
    private Long delay;

    //初始化 context(读取配置文件内容)
    @Override
    public void configure(Context context) {
    
    
        perfix = context.getString("per","per-");
        subfix = context.getString("sub");
        delay = context.getLong("delay",2000L);
    }

    //获取数据封装成 event 并写入 channel,这个方法将被循环调用。
    @Override
    public Status process() throws EventDeliveryException {
    
    

        //循环封装事件
        try {
    
    
            for (int i = 0; i < 5; i++) {
    
    
                //创建事件
                Event e = new SimpleEvent();
                //创建事件头信息
                HashMap<String, String> header = new HashMap<>();
                //给事件设置头信息
                e.setHeaders(header);
                //给事件设置内容
                e.setBody((perfix+"test: "+i+subfix).getBytes());
                //将事件写入 channel
                getChannelProcessor().processEvent(e);
            }

            Thread.sleep(delay);
            return Status.READY;
        } catch (Exception exception) {
    
    
            exception.printStackTrace();
            return Status.BACKOFF;
        }

    }

    //backoff 步长
    @Override
    public long getBackOffSleepIncrement() {
    
    
        return 0;
    }

    //backoff 最长时间
    @Override
    public long getMaxBackOffSleepInterval() {
    
    
        return 0;
    }

}

(3)在/opt/module/flume-1.9.0/job下创建文件夹group5,在该文件夹下创建配置文件flume-mysource-logger.conf。

# Name the components on this agent 
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = com.study.source.MySource
a1.sources.r1.delay = 1000
a1.sources.r1.per = per
a1.sources.r1.sub = sub

# 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)开启任务

bin/flume-ng agent -c conf/ -n a1 -f job/group5/flume-mysource-logger.conf -Dflume.root.logger=INFO,console

(5)结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_18625571/article/details/131792971