Flume--Event定义及源码解析

Flume基本构成与Event结构

1. Event定义

一行文本内容被反序列化成一个event。event的最大定义为2048字节,超过会被切割放到下一个event中。

public interface Event {
 
  /**
   * Returns a map of name-value pairs describing the data stored in the body.
   */
  public Map<String, String> getHeaders();
 
  /**
   * Set the event headers
   * @param headers Map of headers to replace the current headers.
   */
  public void setHeaders(Map<String, String> headers);
 
  /**
   * Returns the raw byte array of the data contained in this event.
   */
  public byte[] getBody();
 
  /**
   * Sets the raw byte array of the data contained in this event.
   * @param body The data.
   */
  public void setBody(byte[] body);
 
}

header是一个map,body是一个字节数组,body是真正传输的数据,header传输的数据不会被sink出去。

2. Event如何产出以及如何分流

while ((line = reader.readLine()) != null) {
            synchronized (eventList) {
              sourceCounter.incrementEventReceivedCount();
              eventList.add(EventBuilder.withBody(line.getBytes(charset)));
              if(eventList.size() >= bufferCount || timeout()) {
                flushEventBatch(eventList);
              }
            }
          }

 public static Event withBody(byte[] body, Map<String, String> headers) {
    Event event = new SimpleEvent();
 
    if(body == null) {
      body = new byte[0];
    }
    event.setBody(body);
 
    if (headers != null) {
      event.setHeaders(new HashMap<String, String>(headers));
    }
 
    return event;
  }

line是真正想要的数据内容,将其转换成内容分装到event的body中,header=null。

header的话,就是在分装Event对象的时候,我们可以自定义的设置一些key-value对,这样做的目的,是为了后续的通道多路复用做准备的

在source端产出event的时候,通过header去区别对待不同的event,然后在sink端的时候,我们就可以通过header中的key来将不同的event输出到对应的sink下游去,这样就将event分流出去了,

但是这里有一个前提:不建议通过对event的body解析来设置header,因为flume就是一个水槽,水槽是不会在中间对水进行加工的,要加工,等水流出去了再加工

    a1.sources.r1.interceptors = i1
    a1.sources.r1.interceptors.i1.type = host
    a1.sources.r1.interceptors.i1.hostHeader = hostname

如上,host是你自定义的一个拦截器,hostHeader都是自定义的key,这样你就在event产出的时候,给各个event定义了不同的header,然后再通过多路复用通道的模式进行分流

a1.sources.r1.selector.type = multiplexing
a1.sources.r1.selector.header = state
a1.sources.r1.selector.mapping.CZ = c1
a1.sources.r1.selector.mapping.US = c2 c3
a1.sources.r1.selector.default = c4

这样你就可以根据event的header中的key将其放入不同的channel中,紧接着,再通过配置多个sink去不同的channel取出event,将其分流到不同的输出端

每个sink配置的通道区别开就行了

你可能感兴趣的:(Flume)