自定义拦截器不适用于apache flume

pxyaymoc  于 2021-06-04  发布在  Flume
关注(0)|答案(1)|浏览(257)

我有一个flume组件监听syslog流。我定制了一个拦截器来修改调用,但它不起作用。我做错了什么?谢谢你,安德里亚
拦截器是一个经过良好编译的jar文件,位于@flume\u home/bin目录中

拦截器类:

package com.test.flume;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.conf.Configurable;
import org.apache.flume.interceptor.Interceptor;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

public class SQLFlumeInterceptor implements Interceptor {

    private final String headerKey;

    private SQLFlumeInterceptor(Context ctx) {            
    }

    @Override
    public void initialize() {

    }

    @Override
    public Event intercept(Event event) {            
        addPreposition(event);
        return event;
    }

    private void addPreposition(Event event) {            
        System.out.println("Event processed");
        event.setBody( "Modified Event".getBytes() );
    }

    @Override
    public List<Event> intercept(List<Event> events) {
        for (Iterator<Event> iterator = events.iterator(); iterator.hasNext(); ) {

            Event next = iterator.next();
            intercept(next);

            if(next == null) {
            iterator.remove();
            }
        }
        return events;
    }

    @Override
    public void close() {

    }

    public static class CounterInterceptorBuilder implements Interceptor.Builder {

        private Context ctx;

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

        @Override
        public void configure(Context context) {
        this.ctx = context;
        }

  }

flume.config文件


# Name the components on this agent

a1.sources = r1
a1.sinks = file-sink
a1.channels = c1

# Describe/configure the source

a1.sources.r1.type = syslogtcp
a1.sources.r1.port = 41414
a1.sources.r1.host = 192.168.1.2
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = com.test.flume.SQLFlumeInterceptor$CounterInterceptorBuilder

# Describe the FILE_ROLLsink

a1.sinks.file-sink.type = FILE_ROLL
a1.sinks.file-sink.sink.directory = /opt/apache-flume-1.5.2-bin/logs/pluto.log
a1.sinks.file-sink.sink.rollInterval = 0
ai.sinks.file-sink.batchSize = 100
ai.sinks.file-sink.fileHeader = true

# 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.file-sink.channel = c1

系统将事件记录在文件中而不进行修改,这是相关的调试日志:

2015-04-27 21:39:17,625 (conf-file-poller-0) [DEBUG - org.apache.flume.conf.FlumeConfiguration$AgentConfiguration.isValid(FlumeConfiguration.java:313)] Starting validation of configuration for agent: a1, initial-configuration: AgentConfiguration[a1]
SOURCES: {r1={ parameters:{port=41414, host=192.168.1.2, interceptors=i1, interceptors.i1.type=com.test.flume.
SQLFlumeInterceptor$CounterInterceptorBuilder, channels=c1, type=syslogtcp} }}
CHANNELS: {c1={ parameters:{transactionCapacity=100, capacity=1000, type=memory} }}
SINKS: {file-sink={ parameters:{sink.rollInterval=0, type=FILE_ROLL, channel=c1, sink.directory=/opt/apache-flume-1.5.2-bin/logs/pluto.log} }}
bqf10yzr

bqf10yzr1#

请将拦截器jar文件放在@flume\u home/lib目录中,而不是放在@flume\u home/bin中。
否则Flume将不会加载jar。

相关问题