Flume拦截器-正则过滤拦截器

1.filter.conf
#1 agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#2 source
a1.sources.r1.type = exec
a1.sources.r1.channels = c1
a1.sources.r1.command = tail -F /opt/plus
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = regex_filter
a1.sources.r1.interceptors.i1.regex = ^A.*
#如果excludeEvents设为false,表示过滤掉不是以A开头的events。如果excludeEvents设为true,则表示过滤掉以A开头的events。
a1.sources.r1.interceptors.i1.excludeEvents = true

a1.sinks.k1.type = logger

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

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
2.启动命令:
bin/flume-ng agent -c conf/ -f jobconf/filter.conf -n a1 -Dflume.root.logger=INFO,console
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Flume拦截器示例,它将日志记录在文件中并添加一个时间戳: ``` package com.example.flume.interceptor; import org.apache.flume.Context; import org.apache.flume.Event; import org.apache.flume.interceptor.Interceptor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; public class TimestampInterceptor implements Interceptor { private static final Logger logger = LoggerFactory.getLogger(TimestampInterceptor.class); private FileWriter fileWriter; private TimestampInterceptor(FileWriter fileWriter) { this.fileWriter = fileWriter; } @Override public void initialize() { // 初始化方法,可以用于读取配置文件等 } @Override public Event intercept(Event event) { // 对每个事件进行拦截处理 String body = new String(event.getBody()); String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); String log = String.format("[%s] %s", timestamp, body); try { fileWriter.write(log); fileWriter.flush(); } catch (IOException e) { logger.error("Failed to write log to file", e); } return event; } @Override public List<Event> intercept(List<Event> events) { // 对事件列表进行批量处理 for (Event event : events) { intercept(event); } return events; } @Override public void close() { // 关闭方法,可以用于释放资源等 try { fileWriter.close(); } catch (IOException e) { logger.error("Failed to close file writer", e); } } public static class Builder implements Interceptor.Builder { private FileWriter fileWriter; @Override public void configure(Context context) { // 配置方法,可以用于读取参数等 String fileName = context.getString("fileName", "flume.log"); try { File file = new File(fileName); if (!file.exists()) { file.createNewFile(); } fileWriter = new FileWriter(file, true); } catch (IOException e) { logger.error("Failed to create file writer", e); } } @Override public Interceptor build() { // 构建方法,返回一个Interceptor实例 return new TimestampInterceptor(fileWriter); } } } ``` 这个拦截器会在Flume接收到事件后将日志写入文件中,并在每行日志前添加一个时间戳。在Flume配置文件中使用这个拦截器的方法如下: ``` agent.sources = source1 agent.channels = channel1 agent.sinks = sink1 agent.sources.source1.type = ... agent.sources.source1.interceptors = interceptor1 agent.sources.source1.interceptors.interceptor1.type = com.example.flume.interceptor.TimestampInterceptor$Builder agent.sources.source1.interceptors.interceptor1.fileName = /path/to/log/file agent.channels.channel1.type = ... agent.channels.channel1.capacity = ... agent.channels.channel1.transactionCapacity = ... agent.sinks.sink1.type = ... agent.sinks.sink1.channel = channel1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值