拦截器存放目录: plugins.d/任意名/lib
拦截器需要实现Interceptor接口,并实现Interceptor.Builder 接口
package sinc.interceptors;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.Charsets;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;
public class SincInterceptor implements Interceptor {
public void close() {
}
public void initialize() {
}
//body过滤,只要包含ok的,原则上应不对body进行过滤,放在下游处理
public Event intercept(Event event) {
String body = new String(event.getBody(), Charsets.UTF_8);
System.out.println("event body:" + body);
if (body.contains("ok")) {
Map<String, String> amap = event.getHeaders();
amap.put("Author", "sinc");
event.setHeaders(amap);
event.setBody(body.getBytes());
return event;
} else {
return null;
}
}
public List<Event> intercept(List<Event> events) {
List<Event> list = new ArrayList<Event>();
for (Event event : events) {
if (intercept(event) != null) {
list.add(event);
}
}
return events;
}
public static class Builder implements Interceptor.Builder {
public void configure(Context context) {
}
public Interceptor build() {
// TODO Auto-generated method stub
return new SincInterceptor();
}
}
}