【大数据之Flume】五、Flume进阶之自定义拦截器 Interceptor

(1)需求:
  使用 Flume 采集服务器本地日志,需要按照日志类型的不同,将不同种类的日志发往不同的分析系统。

(2)分析:
  一台服务器产生的日志类型可能有很多种,不同类型的日志可能需要发送到不同的分析系统。
  此时会用到 Flume 拓扑结构中的 Multiplexing多路复用结构,Multiplexing的原理是,根据 event 中 Header 的某个 key 的值,将不同的 event 发送到不同的 Channel中,所以我们需要自定义一个 Interceptor,为不同类型的 event 的 Header 中的 key 赋予不同的值。
  以端口数据模拟日志,以是否包含”atguigu”模拟不同类型的日志,我们需要自定义 interceptor 区分数据中是否包含”atguigu”,将其分别发往不同的分析系统(Channel)。
在这里插入图片描述
步骤:
(1)创建一个 maven 项目,并引入以下依赖。

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

(2)定义 CustomInterceptor 类并实现 Interceptor 接口,并打包,将jar包放到/opt/module/flume-1.9.0/lib目录下。

package com.study.interceptor;

import org.apache.flume.Context;import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;
import java.util.ArrayList;import java.util.List; import java.util.Map;

public class TypeInterceptor implements Interceptor {
    
    

    //声明一个存放事件的集合
    private List<Event> addHeaderEvents;
    
    @Override
    public void initialize() {
    
    
        //初始化存放事件的集合
        addHeaderEvents = new ArrayList<>();
    }
    
    //单个事件拦截 @Override
    public Event intercept(Event event) {
    
    
    
        //1.获取事件中的头信息
        Map<String, String> headers = event.getHeaders();
        
        //2.获取事件中的 body 信息
        String body = new String(event.getBody());
        
        //3.根据 body 中是否有"atguigu"来决定添加怎样的头信息
        if (body.contains("atguigu")) {
    
    
            //4.添加头信息 
            headers.put("type","first");
        } else {
    
    
            //4.添加头信息
            headers.put("type","second");
        }
    
        return event;
    }
    
    //批量事件拦截 @Override
    public List<Event> intercept(List<Event> events){
    
    
    
        //1.清空集合addHeaderEvents.clear();
        
        //2.遍历 events
        for (Event event : events) {
    
    
            //3.给每一个事件添加头信息 
            addHeaderEvents.add(intercept(event));
        }
        
        //4.返回结果
        return addHeaderEvents;
    }
    
    @Override
    public void close() {
    
    
    
    }
    public static class Builder implements Interceptor.Builder {
    
    @Override
         public Interceptor build() {
    
     
            return new TypeInterceptor();
        }
            
        @Override
        public void configure(Context context) {
    
    
            
        }
    }
}

(3)编辑 flume 配置文件
  为 hadoop102 上/opt/module/flume-1.9.0/job/group4目录下的 flume1.conf 配置 1 个 netcat source,1 个 sink group(2 个 avro sink),
并配置相应的ChannelSelector 和 interceptor。

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

# Describe/configure the source 
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444 

#拦截器
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = com.study.interceptor.TypeInterceptor$Builder

#多路复用选择器
a1.sources.r1.selector.type = multiplexing
a1.sources.r1.selector.header = type
a1.sources.r1.selector.mapping.atguigu = c1
a1.sources.r1.selector.mapping.other = c2


# Describe the sink 
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop103
a1.sinks.k1.port = 4141

a1.sinks.k2.type=avro
a1.sinks.k2.hostname = hadoop104
a1.sinks.k2.port = 4242

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Use a channel which buffers events in memory
a1.channels.c2.type = memory
a1.channels.c2.capacity = 1000
a1.channels.c2.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1 c2
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c2

(4)为 hadoop103 上/opt/module/flume-1.9.0/job/group4目录下的 flume2.conf配置一个 avro source 和一个 logger sink。

a1.sources = r1
a1.sinks = k1
a1.channels = c1

a1.sources.r1.type = avro
a1.sources.r1.bind = hadoop103
a1.sources.r1.port = 4141

a1.sinks.k1.type = logger

a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

a1.sinks.k1.channel = c1
a1.sources.r1.channels = c1

(5)为 hadoop104 上/opt/module/flume-1.9.0/job/group4目录下的 flume3.conf配置一个 avro source 和一个 logger sink。

a1.sources = r1
a1.sinks = k1
a1.channels = c1

a1.sources.r1.type = avro
a1.sources.r1.bind = hadoop104
a1.sources.r1.port = 4242

a1.sinks.k1.type = logger

a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

a1.sinks.k1.channel = c1
a1.sources.r1.channels = c1

(6)分别在 hadoop103,hadoop104,hadoop102 上启动 flume 进程,注意先后顺序。

(7)在 hadoop102 使用netcat 向 localhost:44444 发送消息,观察 hadoop103 和hadoop104 打印的日志。

猜你喜欢

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