flume 拦截器(interceptor)

摘要: 拦截器是简单的插件式组件,设置在source和channel之间。source接收到的时间,在写入channel之前,拦截器都可以进行转换或者删除这些事件。每个拦截器只处理同一个source接收到的事件。可以自定义拦截器。

flume内置了很多拦截器,并且会定期的添加一些拦截器,在这里列出一些flume内置的,经常使用的拦截器。

  1. 时间戳拦截器

    flume中一个最经常使用的拦截器 ,该拦截器的作用是将时间戳插入到flume的事件报头中。如果不使用任何拦截器,flume接受到的只有message。时间戳拦截器的配置。

参数默认值描述
type 类型名称timestamp,也可以使用类名的全路径
preserveExistingfalse如果设置为true,若事件中报头已经存在,不会替换时间戳报头的值

source连接到时间戳拦截器的配置:

a1.sources.r1.interceptors = timestamp
a1.sources.r1.interceptors.timestamp.type=timestamp
a1.sources.r1.interceptors.timestamp.preserveExisting=false

 2.  主机拦截器

        主机拦截器插入服务器的ip地址或者主机名,agent将这些内容插入到事件的报头中。时间报头中的key使用hostHeader配置,默认是host。主机拦截器的配置

参数默认值描述
type 类型名称host
hostHeaderhost事件投的key
useIPtrue如果设置为false,host键插入主机名
preserveExistingfalse如果设置为true,若事件中报头已经存在,不会替换host报头的值

source连接到主机拦截器的配置:

a1.sources.r1.interceptors = host
a1.sources.r1.interceptors.host.type=host
a1.sources.r1.interceptors.host.useIP=false
a1.sources.r1.interceptors.timestamp.preserveExisting=true

3.  静态拦截器

    静态拦截器的作用是将k/v插入到事件的报头中。配置如下

参数默认值描述
type 类型名称static
keykey事件头的key
valuevaluekey对应的value值
preserveExistingtrue如果设置为true,若事件中报头已经存在该key,不会替换value的值

source连接到静态拦截器的配置:

a1.sources.r1.interceptors = static
a1.sources.r1.interceptors.static.type=static
a1.sources.r1.interceptors.static.key=logs
a1.sources.r1.interceptors.static.value=logFlume
a1.sources.r1.interceptors.static.preserveExisting=false

4.    正则过滤拦截器

在日志采集的时候,可能有一些数据是我们不需要的,这样添加过滤拦截器,可以过滤掉不需要的日志,也可以根据需要收集满足正则条件的日志。

参数默认值描述
type 类型名称REGEX_FILTER
regex.*匹配除“\n”之外的任何个字符
excludeEventsfalse默认收集匹配到的事件。如果为true,则会删除匹配到的event,收集未匹配到的。

source连接到正则过滤拦截器的配置:

a1.sources.r1.interceptors = regex
a1.sources.r1.interceptors.regex.type=REGEX_FILTER
a1.sources.r1.interceptors.regex.regex=(rm)|(kill)
a1.sources.r1.interceptors.regex.excludeEvents=false

这样配置的拦截器就只会接收日志消息中带有rm 或者kill的日志。

  • 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、付费专栏及课程。

余额充值