flume custom interceptor

Let's take a look at the interceptor of the timestamp in the source code

public class TimestampInterceptor implements Interceptor {

  private final boolean preserveExisting;

  /**
   * Only {@link TimestampInterceptor.Builder} can build me
   */
  private TimestampInterceptor(boolean preserveExisting) {
    this.preserveExisting = preserveExisting;
  }

  @Override
  public void initialize() {
    // no-op
  }

  /**
   * Modifies events in-place.
    *这个是对单个事件的处理
   */
  @Override
  public Event intercept(Event event) {
//获得事件中的所有头信息,都是以key-value的形式存储
    Map<String, String> headers = event.getHeaders();
    if (preserveExisting && headers.containsKey(TIMESTAMP)) {
      // we must preserve the existing timestamp
    } else {
      long now = System.currentTimeMillis();
    //把时间戳放到头信息
      headers.put(TIMESTAMP, Long.toString(now));
    }
    return event;
  }

  /**
   * Delegates to {@link #intercept(Event)} in a loop.
   * @param events
    *这是对多个事件的处理
   * @return
   */
  @Override
  public List<Event> intercept(List<Event> events) {
    for (Event event : events) {
      intercept(event);
    }
    return events;
  }

  @Override
  public void close() {
    // no-op
  }

  /**
   * Builder which builds new instances of the TimestampInterceptor.
    *这个方法是必须实现的,这个接口Interceptor中注释明确必须实现
   */
  public static class Builder implements Interceptor.Builder {

    private boolean preserveExisting = PRESERVE_DFLT;

    @Override
    public Interceptor build() {
      return new TimestampInterceptor(preserveExisting);
    }

    @Override
    public void configure(Context context) {
      preserveExisting = context.getBoolean(PRESERVE, PRESERVE_DFLT);
    }

  }

  public static class Constants {
    public static String TIMESTAMP = "timestamp";
    public static String PRESERVE = "preserveExisting";
    public static boolean PRESERVE_DFLT = false;
  }

}

You can write your own filters, especially for log4j log processing. For example, if your log generates a file every hour, but I want to store it by day, you can implement it in this form. Put the generated package Put it under /app/apache-flume-1.7.0-bin/plugins.d/interceptor/lib. If /plugins.d does not exist, you can create this directory yourself. Add: xxx.sources to the configuration file. r1.interceptors.i1.type = com.hc360.interceptor.DateTimeSuffixInterceptor$Builder is fine. Now I want to record some of the things I do, stick to it, stick to it. . . . . . . . . . . . . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325441297&siteId=291194637