栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Flume进阶二——自定义Interceptor

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flume进阶二——自定义Interceptor

一:自定义Interceptor

        在实际的开发中,一台服务器产生的日志类型可能有很多种,不同类型的日志可能需要 发送到不同的分析系统。此时会用到 Flume 拓扑结构中的 Multiplexing 结构,Multiplexing 的原理是,根据 event 中 Header 的某个 key 的值,将不同的 event 发送到不同的 Channel 中,所以我们需要自定义一个 Interceptor,为不同类型的 event 的 Header 中的 key 赋予不同的值

1.1 案例:            使用 Flume 采集服务器本地日志,需要按照日志类型的不同,将不同种类的日志发往不 同的分析系统。                 在该案例中,我们以端口数据模拟日志,是否包含start区分不同日志类型,我们需要自定义 interceptor 区分,将其分别发往不同的分析系统 (Channel),即hadoop112接受全部日志,然后包含start的日志放松到hadoop113,包含event的日志发送到hadoop114                   1.1.1实现步骤: 1.创建一个 maven 项目,并引入以下依赖。

 org.apache.flume
 flume-ng-core
 1.7.0
2.定义 MyInterceptor 类并实现 Interceptor 接口。
package com.atguigu.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 MyInterceptor implements Interceptor {
    
    //存放拦截器处理后的list
    private  List addHeaderEvents ;
    
    
    @Override
    public void initialize() {
        addHeaderEvents = new ArrayList<>();
    }

    
    @Override
    public Event intercept(Event event) {
        //获取header和body
        Map headers = event.getHeaders();
        byte[] bbody = event.getBody();
        String body = new String(bbody);
//        flume配置文件对应
//        a1.sources.r1.selector.header = type
//        a1.sources.r1.selector.mapping.start = c1
//        a1.sources.r1.selector.mapping.event = c2
        if (body.contains("start")){
            headers.put("type","start");
        }else {
            headers.put("type","event");
        }
        
        return event;
    }

    
    @Override
    public List intercept(List list) {
        //清空前一批次集合
        addHeaderEvents.clear();
        for (Event event : list) {
            addHeaderEvents.add(intercept(event));
        }
        return addHeaderEvents;
    }

    
    @Override
    public void close() {

    }

    
    public static class Builder implements Interceptor.Builder{

        @Override
        public Interceptor build() {
            return new MyInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }
}

3.把上面的java打包放入hadoop112的flume的lib目录下面

4.编辑 flume 配置文件         为 hadoop112 上的 Flume1 配置 1 个 netcat source,1 个 sink group(2 个 avro sink), 并配置相应的 ChannelSelector 和 interceptor。

        

# vim flume1.conf,添加如下内容

# 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 = exec
a1.sources.r1.command = tail -F /opt/data/test.log
a1.sources.r1.shell = /bin/bash -c
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = com.atguigu.interceptor.MyInterceptor$Bulider
a1.sources.r1.selector.type = multiplexing
a1.sources.r1.selector.header = type
a1.sources.r1.selector.mapping.start = c1
a1.sources.r1.selector.mapping.event = c2
# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop113
a1.sinks.k1.port = 4141
a1.sinks.k2.type=avro
a1.sinks.k2.hostname = hadoop114
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
为 hadoop103 上的 Flume2 配置一个 avro source 和一个 logger sink。
# vim flume2.conf,添加如下内容

a1.sources = r1
a1.sinks = k1
a1.channels = c1
a1.sources.r1.type = avro
a1.sources.r1.bind = hadoop113
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
为 hadoop114 上的 Flume3 配置一个 avro source 和一个 logger sink。
# vim flume3.conf,添加如下内容

a1.sources = r1
a1.sinks = k1
a1.channels = c1
a1.sources.r1.type = avro
a1.sources.r1.bind = hadoop114
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

5.启动

[atguigu@hadoop113 flume-1.7.0]$ bin/flume-ng agent -n a1 -c conf/ -f job/flume2.conf -Dflume.root.logger=INFO,console

[atguigu@hadoop114 flume-1.7.0]$ bin/flume-ng agent -n a1 -c conf/ -f job/flume3.conf -Dflume.root.logger=INFO,console

[atguigu@hadoop112 flume-1.7.0]$ bin/flume-ng agent -n a1 -c conf/ -f job/flume1.conf

在hadoop112的/opt/data/test.log端口输入原数据

hadoop113

 hadoop114

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/343423.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号