Flume 拦截器(interceptor)详解

flume 拦截器(interceptor)1、flume拦截器介绍拦截器是简单的插件式组件,设置在source和channel之间。source接收到的事件event,在写入channel之前,拦截器都可以进行转换或者删除这些事件。每个拦截器只处理同一个source接收到的事件。可以自定义拦截器。2、flume内置的拦截器

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

参数

默认值

描述

type

timestamp

类型名称timestamp,也可以使用类名的全路径org.apache.flume.interceptor.TimestampInterceptor$Builder

preserveExisting

false

如果设置为true,若事件中报头已经存在,不会替换时间戳报头的值

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

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


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


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

参数

默认值

描述

type

host

类型名称host,也可以使用类名的全路径org.apache.flume.interceptor.HostInterceptor$Builder

hostHeader

host

事件头的key

useIP

true

如果设置为false,host键插入主机名

preserveExisting

false

如果设置为true,若事件中报头已经存在,不会替换时间戳报头的值

参数 默认值 描述type host 类型名称host,也可以使用类名的全路径org.apache.flume.interceptor.HostInterceptor$BuilderhostHeader host 事件头的keyuseIP true 如果设置为false,host键插入主机名preserveExisting false 如果设置为true,若事件中报头已经存在,不会替换时间戳报头的值

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


a1.sources.r1.interceptors=i2a1.sources.r1.interceptors.i2.type=hosta1.sources.r1.interceptors.i2.useIP=falsea1.sources.r1.interceptors.i2.preserveExisting=false


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

参数

默认值

描述

type

static

类型名称static,也可以使用类全路径名称org.apache.flume.interceptor.StaticInterceptor$Builder

key

key

事件头的key

value

value

key对应的value值

preserveExisting

true

如果设置为true,若事件中报头已经存在该key,不会替换value的值

参数 默认值 描述type static 类型名称static,也可以使用类全路径名称org.apache.flume.interceptor.StaticInterceptor$Builderkey key 事件头的keyvalue value key对应的value值preserveExisting true 如果设置为true,若事件中报头已经存在该key,不会替换value的值

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


a1.sources.r1.interceptors = statica1.sources.r1.interceptors.static.type=statica1.sources.r1.interceptors.static.key=logsa1.sources.r1.interceptors.static.value=logFlumea1.sources.r1.interceptors.static.preserveExisting=false


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

参数

默认值

描述

type

REGEX_FILTER

类型名称REGEX_FILTER,也可以使用类全路径名称org.apache.flume.interceptor.RegexFilteringInterceptor$Builder

regex

.*

匹配除“\n”之外的任何个字符

excludeEvents

false

默认收集匹配到的事件。如果为true,则会删除匹配到的event,收集未匹配到的

参数 默认值 描述type REGEX_FILTER 类型名称REGEX_FILTER,也可以使用类全路径名称org.apache.flume.interceptor.RegexFilteringInterceptor$Builderregex .* 匹配除“\n”之外的任何个字符excludeEvents false 默认收集匹配到的事件。如果为true,则会删除匹配到的event,收集未匹配到的

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


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


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

测试案例:test_regex.conf# 定义这个agent中各组件的名字


a1.sources = r1a1.sinks = k1a1.channels = c1


# 描述和配置source组件:r1


a1.sources.r1.type = netcata1.sources.r1.bind = hadoop-001a1.sources.r1.port = 44444a1.sources.r1.interceptors=i4a1.sources.r1.interceptors.i4.type=REGEX_FILTER


#保留内容中出现hadoop或者是spark的字符串的记录


a1.sources.r1.interceptors.i4.regex=(hadoop)|(spark)a1.sources.r1.interceptors.i4.excludeEvents=false


# 描述和配置sink组件:k1


a1.sinks.k1.type = logger


# 描述和配置channel组件,此处使用是内存缓存的方式


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


# 描述和配置source channel sink之间的连接关系


a1.sources.r1.channels = c1a1.sinks.k1.channel = c1


输入以下指令:


bin/flume-ng agent --conf conf --conf-file conf/test_regex.conf --name a1 -Dflume.root.logger=INFO,console


发送数据测试:

打印到控制台信息:

 

只接受到存在hadoop或者spark的记录,验证成功!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值