Flume 内置组件不够用?自己写一个,没你想的那么难
Flume 内置的 Source、Sink、拦截器覆盖了大部分场景,但总有例外。
我遇到过的情况:业务方发的日志格式是自定义的二进制协议,内置的 Source 根本不认识;下游存储是个自研的时序数据库,没有现成的 Sink。这时候只能自己写。
说实话,Flume 的扩展接口设计得还算清晰,继承几个类、实现几个方法就能跑起来。这篇文章把三种扩展——拦截器、Source、Sink——的写法都过一遍,代码贴全,配置也附上,你需要的时候直接抄。
开发前准备:开发环境与依赖
Flume 的自定义组件本质上就是写 Java 类,打包成 JAR 扔到 Flume 的 lib 目录下。
依赖配置
在 pom.xml 中添加 Flume 核心依赖(以 1.9.0 为例):
1 2 3 4 5 6
| <dependency> <groupId>org.apache.flume</groupId> <artifactId>flume-ng-core</artifactId> <version>1.9.0</version> <scope>provided</scope> </dependency>
|
scope 设成 provided,因为 Flume 运行时已经带了这些类,打包的时候别打进去,避免版本冲突。
核心接口
Flume 扩展的核心是实现官方定义的接口,各组件对应的接口如下:
| 组件类型 |
需实现的接口 / 继承的类 |
核心方法 |
| 拦截器 |
org.apache.flume.interceptor.Interceptor |
intercept(Event) 处理单个事件 |
| Source |
继承 AbstractSource,实现 PollableSource |
process() 产生并发送事件 |
| Sink |
继承 AbstractSink,实现 Configurable |
process() 从 Channel 消费事件 |
每个组件都要配一个 Builder 类,Flume 通过 Builder 来实例化你的组件。这块容易忘,后面代码里会看到。
实战一:自定义拦截器(Interceptor):给 Event 打个标签
拦截器的用处是:数据从 Source 出来、进 Channel 之前,你插一手,改改 Header、改改 Body,或者直接丢掉某些数据。
下面这个例子实现了一个简单的分类器:根据 Body 内容给 Event 的 Header 里加一个 type 字段,后续可以用 Multiplexing Channel Selector 按类型路由到不同的 Channel。
1.代码实现
通过实现org.apache.flume.interceptor.Interceptor来自定义自己的拦截器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| public class MyInterceptor implements Interceptor { @Override public void initialize() {
}
@Override public Event intercept(Event event) { Map<String,String> headers = event.getHeaders();
String body = new String(event.getBody());
if (body.startsWith("number:")) { headers.put("type", "number"); } else if (body.startsWith("log:")) { headers.put("type", "log"); } else { headers.put("type", "other"); } return event;
}
@Override public List<Event> intercept(List<Event> list) { for (Event event : events) { intercept(event); } return events; }
@Override public void close() {
} public static class Builder implements Interceptor.Builder{
@Override public Interceptor build() { return new MyInterceptor(); }
@Override public void configure(Context context) { } }
}
|
2. 打包与部署
3. 配置使用拦截器
在 Flume 配置文件中引用自定义拦截器,并结合 Multiplexing Channel Selector 实现按类型路由:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
agent.sources = customSource agent.channels = numChannel logChannel otherChannel agent.sinks = numSink logSink otherSink
agent.sources.customSource.type = seq
agent.sources.mySource.interceptors = myInterceptor
agent.sources.mySource.interceptors.myInterceptor.type = com.zhanghe.study.custom_flume.interceptor.MyInterceptor$Builder
agent.sources.customSource.selector.type = multiplexing
agent.sources.customSource.selector.header = type
agent.sources.customSource.selector.mapping.number = numChannel
agent.sources.customSource.selector.mapping.log = logChannel
agent.sources.customSource.selector.default = otherChannel
agent.channels.numChannel.type = memory agent.channels.logChannel.type = memory agent.channels.otherChannel.type = memory
agent.sinks.numSink.type = logger agent.sinks.logSink.type = logger agent.sinks.otherSink.type = logger
agent.sources.customSource.channels = numChannel logChannel otherChannel agent.sinks.numSink.channel = numChannel agent.sinks.logSink.channel = logChannel agent.sinks.otherSink.channel = otherChannel
|
这个组合很实用:拦截器打标签 + Channel Selector 按标签路由,一条采集链路可以分流到多个下游。
4. 验证效果
启动 Flume 后,序列生成器会产生事件,拦截器会按内容添加 type 头信息,最终不同类型的事件会路由到对应的 Channel 和 Sink,控制台会输出分类后的日志。
实战二:自定义Source:从特殊数据源拉数据
内置的 Source 不认识你的数据源,就得自己写。下面这个 Source 做的事情很简单:每秒生成一条数据,格式是 "自定义前缀: 序号"。
1. 代码实现
自定义的Source需要继承AbstractSource,实现Configurable和PollableSource接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| import org.apache.flume.*; import org.apache.flume.conf.Configurable; import org.apache.flume.source.AbstractSource; import org.apache.flume.source.PollableSource; import java.util.concurrent.atomic.AtomicInteger;
public class MySource extends AbstractSource implements PollableSource, Configurable {
private String prefix; private AtomicInteger counter = new AtomicInteger(0);
@Override public void configure(Context context) { prefix = context.getString("prefix", "custom"); }
@Override public Status process() throws EventDeliveryException { Status status = Status.READY; try { String data = prefix + ": " + counter.incrementAndGet(); Event event = EventBuilder.withBody(data.getBytes());
getChannelProcessor().processEvent(event); Thread.sleep(1000); } catch (Exception e) { status = Status.BACKOFF; if (e instanceof Error) { throw (Error) e; } } return status; }
@Override public long getBackOffSleepIncrement() { return 0; }
@Override public long getMaxBackOffSleepInterval() { return 0; } }
|
2. 配置使用自定义 Source
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
agent.sources = customSource agent.channels = memoryChannel agent.sinks = loggerSink
agent.sources.mySource.type = com.zhanghe.study.custom_flume.source.MySource
agent.sources.customSource.prefix = mydata
agent.channels.memoryChannel.type = memory agent.sinks.loggerSink.type = logger
agent.sources.customSource.channels = memoryChannel agent.sinks.loggerSink.channel = memoryChannel
|
有个坑要注意:process() 方法里如果抛出异常,Flume 不会自动重试。你得自己 catch 住,返回 Status.BACKOFF,Flume 才会稍后再次调用 process()。我刚写的时候没注意这个,Source 一报错就直接停了。
实战三:自定义Sink:往特殊目标写数据
自定义 Sink 就是把数据从 Channel 取出来,发到你想要的地方。下面这个 Sink 把数据追加写到本地文件里。
1. 代码实现
自定义的Sink需要继承AbstractSink类,实现Configurable接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import org.apache.flume.*; import org.apache.flume.conf.Configurable; import org.apache.flume.sink.AbstractSink; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter;
public class MySink extends AbstractSink implements Configurable { private static final Logger logger = LoggerFactory.getLogger(FileSink.class); private String filePath; private PrintWriter writer;
@Override public void configure(Context context) { filePath = context.getString("filePath"); if (filePath == null) { throw new IllegalArgumentException("filePath 配置不能为空!"); } }
@Override public void start() { try { writer = new PrintWriter(new FileWriter(filePath, true)); } catch (IOException e) { logger.error("初始化文件写入流失败", e); throw new FlumeException(e); } super.start(); }
@Override public Status process() throws EventDeliveryException { Status status = Status.READY; Channel channel = getChannel(); Transaction txn = channel.getTransaction();
try { txn.begin(); Event event = channel.take();
if (event != null) { String data = new String(event.getBody()); writer.println(data); writer.flush(); } else { status = Status.BACKOFF; } txn.commit(); } catch (Exception e) { txn.rollback(); status = Status.BACKOFF; if (e instanceof Error) { throw (Error) e; } } finally { txn.close(); } return status; }
@Override public void stop() { if (writer != null) { writer.close(); } super.stop(); } }
|
2. 配置使用自定义 Sink
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| agent.sources = seqSource agent.channels = memoryChannel agent.sinks = fileSink
agent.sources.seqSource.type = seq
agent.sinks.fileSink.type = com.zhanghe.study.custom_flume.sink.MySink
agent.sinks.fileSink.filePath = /tmp/flume-custom-sink.log
agent.channels.memoryChannel.type = memory
agent.sources.seqSource.channels = memoryChannel agent.sinks.fileSink.channel = memoryChannel
|