Flume中的事件event源码分析和自定义拦截器interceptor

Flume中的Event
在Flume中一行文本内容会被反序列化成一个event【序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据】,event的最大定义为2048字节,超过,则会切割,剩下的会被放到下一个event中,默认编码是UTF-8,这都是统一的。

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);
 
}

由此可知,一个Event中的header是一个Map<String, String>,而body是一个字节数组byte[]。但是我们实际使用中真正传输的只有body中的数据,而header传输的数据是不会被sink出去的。

那么,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 = 
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值