flume拦截器

摘要:
flume拦截器1、flume拦截器介绍拦截器是简单的插件式组件,设置在source和channel之间。source接收到的事件event,在写入channel之前,拦截器都可以进行转换或者删除这些事件。

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$Builder
preserveExisting 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$Builder
hostHeader host 事件头的key
useIP true 如果设置为false,host键插入主机名
preserveExisting false 如果设置为true,若事件中报头已经存在,不会替换时间戳报头的值

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


a1.sources.r1.interceptors=i2
a1.sources.r1.interceptors.i2.type=host
a1.sources.r1.interceptors.i2.useIP=false
a1.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$Builder
key key 事件头的key
value value key对应的value值
preserveExisting true 如果设置为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


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

参数

默认值

描述

type

REGEX_FILTER

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

regex

.*

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

excludeEvents

false

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

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

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


a1.sources.r1.interceptors=i4
a1.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 = r1
a1.sinks = k1
a1.channels = c1


# 描述和配置source组件:r1


a1.sources.r1.type = netcat
a1.sources.r1.bind = hadoop-001
a1.sources.r1.port = 44444
a1.sources.r1.interceptors=i4
a1.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 = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100


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


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


输入以下指令:


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


发送数据测试:

flume拦截器第1张

打印到控制台信息:

flume拦截器第2张

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

免责声明:文章转载自《flume拦截器》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇es使用term+filter查询(对type为text的查询注意点)Nginx使用教程(二):Nginx配置性能优化之worker配置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

钉钉微应用的免登录流程

  最近做了个钉钉企业内部微应用的项目。记录下自己的心得。   首先根据官方文档明白免登流程   免登录的流程如上。首先我们需要拿到自己企业的corpId,和corpSecret,访问企业后台https://oa.dingtalk.com/index.htm#/microApp/microAppList登录后就可以拿到(当然你得有管理员权限),拿到这两个...

bootstrap-table中时间戳转换为日期格式。

{ field: 'createdTime', title: '创建时间', formatter: function (value, row, index) { return changeDateFormat(value) } }, function changeDateFormat(cellval) { var dateVal = cellval +...

windows条件下,Ping加上时间戳,并保存到文件,适用于测试网络

在c盘下面新建文件 ping.vbs在 ping.vbs中输入代码如下: Dim args, flag, unsuccOut args="" otherout="" flag=0 If WScript.Arguments.count = 0Then WScript.Echo "Usage: cscript tping.vbs [-t] [...

HBase1.2官方文档——ACID

原文链接:http://hbase.apache.org/acid-semantics.html 关于这篇文档 Apache HBase (TM) 不是一个适用于ACID的数据库。但是它可以保证特定的属性。这篇说明列举了HBase中ACID的属性 定义 为了共同的词汇表,我们定义以下术语: Atomicity原子性 一个操作是原子的,它要么整体都...

将日期格式转换为时间戳

1. 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 /** 1. 下面是获取时间日期的方法,需要什么样的格式自己拼接起来就好了 2. 更多好用的方法可以在这查到 -> http://www.w3school.com.cn/jsref/jsref_obj_date.asp...

flume 使用手册

以下jie皆来自官网: 1:首先版本是flume 1.8 查看版本: bin/flume-ng version 2:配置与启动 https://flume.apache.org/FlumeUserGuide.html#configuration Defining the flow # list the sources, sinks and channel...