flume拦截器的使用

interceptors 拦截器

​ 可以拦截数据源 source 给数据源添加数据 header信息 为了后续的数据的更加方便的使用

默认拦截器有:

1)Timestamp Interceptor

​ 在数据源上添加时间戳
​ headers:{timestamp=1554707017331}
​ key: timestamp
​ value:当前系统的时间戳

2)host interceptor

​ 拦截数据源 每一个event 在每一条数据的header中添加 hostname| ip
​ key: host
​ value : 当前主机的 hostname | ip

例子

指定当前agent a1的 sources sinks  channels 的别名
a1.sources = r1
a1.sinks = k1
a1.channels = c1

agent的数据源的
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

指定agent的sink的
a1.sinks.k1.type = logger

指定agent的通道
a1.channels.c1.type = memory

指定拦截器
指定拦截器的别名
a1.sources.r1.interceptors = i1

指定拦截期的类型
a1.sources.r1.interceptors.i1.type = host

绑定agent的  r1   c1   k1 
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1




数据:
Event: { headers:{host=192.168.191.203} body: 68 65 6C 6C 6F 20 74 6F 6D 0D hello tom. }
使用:
%{host}

3)Static Interceptor

​ 静态拦截器 拦截每一个event数据 手动定义拦截器的key value 手动在header中添加 需要添加的k v 便于后面的数据的分类使用
案例:

指定当前agent a1的 sources sinks  channels 的别名
a1.sources = r1
a1.sinks = k1
a1.channels = c1

agent的数据源的
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

定义拦截器
定义拦截器的别名
a1.sources.r1.interceptors = i1

定义拦截器的类型的
a1.sources.r1.interceptors.i1.type = static

手动指定拦截器的 key值
a1.sources.r1.interceptors.i1.key = class

手动指定拦截器的value值
a1.sources.r1.interceptors.i1.value = two

指定agent的sink的
a1.sinks.k1.type = logger

指定agent的通道
a1.channels.c1.type = memory

绑定agent的  r1   c1   k1 
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1




headers:{class=bd1811}

4)多个拦截器联合使用:

指定当前agent a1的 sources sinks  channels 的别名

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

agent的数据源的

a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

定义拦截器

定义拦截器的别名

a1.sources.r1.interceptors = i1 i2

i1定义拦截器的类型的

a1.sources.r1.interceptors.i1.type = static

i1手动指定拦截器的 key值

a1.sources.r1.interceptors.i1.key = class

i1手动指定拦截器的value值

a1.sources.r1.interceptors.i1.value = bd1811

指定i2对应的拦截器

a1.sources.r1.interceptors.i2.type = timestamp

指定agent的sink的

a1.sinks.k1.type = logger

指定agent的通道

a1.channels.c1.type = memory

绑定agent的  r1   c1   k1 

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




自定义拦截器

请见下文

自定义flume拦截器-实现了多种功能

下面是一个简单的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、付费专栏及课程。

余额充值