Flume拦截器

相信大家对Flume都已经很了解,Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集、聚合和传输的系统。其架构多为Source、Channel、Sink的结构和模型,如图所示。




640?wx_fmt=png




Flume拦截器(Interceptor)是设置在source和channel之间,数据接收到Source后,当数据进入到Channel之前,拦截器都可以对这些数据做事件拦截,根据不同的拦截器对事件进行不同的操作。


拦截器是实现org.apache.flume.interceptor.Interceptor类接口。拦截器可以根据拦截器开发者选择的任何标准修改甚至删除事件。Flume支持拦截器的链接。通过在配置中指定拦截器构建器类名列表,可以实现此目的。拦截器在源配置中指定为以空格分隔的列表。指定拦截器的顺序是它们被调用的顺序。一个拦截器返回的事件列表将传递给链中的下一个拦截器。拦截器可以修改或删除事件。如果拦截器需要删除事件,它就不会在它返回的列表中返回该事件。如果要删除所有事件,则只返回一个空列表。拦截器是命名组件,下面是它们如何通过配置创建的示例:

a1.sources  =  r1 	
a1.sinks  =  k1 	
a1.channels  =  c1 	
a1.sources.r1.interceptors  =  i1 i2 	
a1.sources.r1.interceptors.i1.type  =  org.apache.flume.interceptor.HostInterceptor $ Builder 	
a1。sources.r1.interceptors.i1.preserveExisting  =  false 	
a1.sources.r1.interceptors.i1.hostHeader  =  hostname 	
a1.sources.r1.interceptors.i2.type  =  org.apache.flume.interceptor.TimestampInterceptor $ Builder 	
a1.sinks .k1.filePrefix  =  FlumeData。%{CollectorHost}。%Y-%m-%d 	
a1.sinks.k1.channel  =  c1
 
 

请注意,拦截器构建器将传递给类型config参数。拦截器本身是可配置的,可以传递配置值,就像传递给任何其他可配置组件一样。在上面的示例中,事件首先传递给HostInterceptor,然后HostInterceptor返回的事件传递给TimestampInterceptor。您可以指定完全限定的类名(FQCN)或别名时间戳。如果您有多个收集器写入相同的HDFS路径,那么您也可以使用HostInterceptor。


下边就给大家举例一些常见的拦截器:


时间戳拦截器


#定义拦截器,为文件最后添加时间戳	
a4.sources.r1.interceptors = i1	
a4.sources.r1.interceptors.i1.type = org.apache.flume.interceptor.TimestampInterceptor$Builder


主机名拦截器


#拦截器	
a1.sources.r1.interceptors = i1	
a1.sources.r1.interceptors.i1.type = host	

	
#参数为true时用IP192.168.1.111,参数为false时用主机名,默认为true	
a1.sources.r1.interceptors.i1.useIP = false	
a1.sources.r1.interceptors.i1.hostHeader = agentHost


UUID拦截器


a1.sources.r1.interceptors = i1	
#type的参数不能写成uuid,得写具体,否则找不到类	
a1.sources.r1.interceptors.i1.type = org.apache.flume.sink.solr.morphline.UUIDInterceptor$Builder	
#如果UUID头已经存在,它应该保存	
a1.sources.r1.interceptors.i1.preserveExisting = true	
a1.sources.r1.interceptors.i1.prefix = UUID_


正则过滤拦截器


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.sources.r1.interceptors = i1	
a1.sources.r1.interceptors.i1.type = search_replace	

	
#遇到数字改成itstar,A123会替换为Aitstar	
a1.sources.r1.interceptors.i1.searchPattern = [0-9]+	
a1.sources.r1.interceptors.i1.replaceString = itstar	
a1.sources.r1.interceptors.i1.charset = UTF-8


正则抽取拦截器


a1.sources.r1.interceptors = i1	
a1.sources.r1.interceptors.i1.type = regex_extractor	
a1.sources.r1.interceptors.i1.regex = hostname is (.*?) ip is (.*)	
a1.sources.r1.interceptors.i1.serializers = s1 s2	
a1.sources.r1.interceptors.i1.serializers.s1.name = cookieid	
a1.sources.r1.interceptors.i1.serializers.s2.name = ip


640?wx_fmt=png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值