需求
给定一个日志文件,把日志中DEBUG、INFO、WARN、ERROR日志按照不同文件夹、日志日期进行区分。输出到HDFS中
例:2016-07-08 09:33:52 INFO 日志,需要放在hdfs://flume/2016-07-08/INFO文件夹下。
解决办法
1.
使用Spooling Directory Source 读取日志文件
Spooling Directory Source 可以监测配置的目录下新增的文件,并将文件中的数据读取出来。
2.
使用Regex Filtering Interceptor 拦截器(a1.sources.r1.interceptors.i1.type =regex_filter)从每行日志中抽取时间和日志类型信息
使用RegexExtractorInterceptorMillisSerializer将抽取出的时间信息根据指明的格式转为时间戳,写入到header中,key为timestamp,将类型信息写入到header中,key为type
3.
hdfs sink从header中取出时间戳和日志类型信息,将日志输出到对应的hdfs目录
关键问题
flume读取日志是按行读取,无法进行多行读取,当出现如下日志时将无法读到日志的正确时间与类型信息,所以我们需要有一种可以多行读取日志信息的办法,这里采用自定义拦截器的方法
2016-07-28 15:49:05-[Analysis-WC] ERROR [http-8080-2] ControllerProxy.afterMethod(43) | java.lang.NullPointerException
java.lang.NullPointerException
at com.ap.alt.system.web.LoginMgrController.getSumMXYJTC(LoginMgrController.java:304)
at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
自定义拦截器
只修改public List intercept(List events)方法,该方法对读取到的每行event,调用public Event intercept(Event event)方法进行相关操作
不修改public Event intercept(Event event)方法,该方法完成指定的正则匹配,并使用serializer添加匹配的内容到header等工作
对event列表进行遍历,对每个event进行正则匹配
若匹配成功说明该event是单行日志或多行日志的开始行,可以调用public Event intercept(Event event)从中提取出时间和类型信息,添加到该event的header中
若匹配失败说明该event属于上一个event的日志内容,应将它的body和上一个event的body连接到一起,并在返回event列表时舍弃这一event
关键代码
public List<Event> intercept(List<Event> events) {
List<Event> intercepted = Lists.newArrayListWithCapacity(events.size());
int addnum=0;//记录上一个正确匹配的event在队列中的位置,以便下一event有和它连接的需要
for(int i=0;i<events.size();i++){
Event interceptedEvent=null;
Matcher matcher=regex.matcher(
new String(events.get(i).getBody(), Charsets.UTF_8));
if(matcher.find()){
interceptedEvent = intercept(events.get(i));
intercepted.add(interceptedEvent);
addnum=i;//更新正确匹配的event在队列中的位置
}else{
String body=new String(intercepted.get(addnum).getBody(), Charsets.UTF_8)+"\n"+new String(events.get(i).getBody(), Charsets.UTF_8);
intercepted.get(addnum).setBody(body.getBytes());
}
}
return intercepted;
}
jar包放到flume的lib文件夹下,在conf文件中需要使用的type后填写包名类名
完整代码+测试文件–>自定义拦截器
conf文件
#Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = spoolDir
a1.sources.r1.spoolDir=/tmp/logs
a1.sources.r1.channels = c1
a1.sources.r1.interceptors = i1 i2
#匹配时间并转换为时间戳到header中
a1.sources.r1.interceptors.i1.type = org.my.flume.myinterceptor.multinterceptor$Builder
a1.sources.r1.interceptors.i1.regex = (((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29))
a1.sources.r1.interceptors.i1.serializers = s1
a1.sources.r1.interceptors.i1.serializers.s1.type = org.apache.flume.interceptor.RegexExtractorInterceptorMillisSerializer
a1.sources.r1.interceptors.i1.serializers.s1.name = timestamp
a1.sources.r1.interceptors.i1.serializers.s1.pattern = yyyy-MM-dd
# 匹配日志类型到header中
a1.sources.r1.interceptors.i2.type = org.my.flume.myinterceptor.multinterceptor$Builder
a1.sources.r1.interceptors.i2.regex = (\INFO|\DEBUG|\WARN|\ERROR)
a1.sources.r1.interceptors.i2.serializers = s2
a1.sources.r1.interceptors.i2.serializers.s2.name = type
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path =/flume/%Y-%m-%d/%{type}
a1.sinks.k1.hdfs.fileType=DataStream
#######下面是配置输出日志文件按大小分割,超过1M自动滚动到下一文件
a1.sinks.k1.hdfs.rollSize=1024000
a1.sinks.k1.hdfs.rollInterval=0
a1.sinks.k1.hdfs.rollCount=0
a1.sinks.k1.hdfs.idleTimeout=0
a1.sinks.k1.hdfs.minBlockReplicas=1
# Use a channel which buffers events inmemory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
注意:Flume使用自定义拦截器jar包,要在type值后加$Builder,否则会报InstantiationException错误
遇见的问题:使用Flume官方文档的正则表达式出现问题,不能识别\d或\d,效果为查找d这个字母,同理\XXX会查找XXX,暂未找到解决办法,所以在这里使用了较复杂的日期验证正则表达式
启动flume
# flume-ng agent -c conf -f conf/my.conf -n a1 -Dflume.root.logger=INFO,console