Flume SimpleHbaseEventSerializer 类解析

     flume官网上介绍了很多sink/source;

     这篇51cto博客有很多示例介绍;

     HBaseSink/AsyncHBaseSink我是基于这篇apche官网博客做的。

     我首先将详细介绍 SimpleHbaseEventSerializer.java 代码

     SimpleHbaseEventSerializer类中包括的函数有

  • SimpleHbaseEventSerialzer函数:这是构造函数
  • configure(ComponentConfiguration conf)函数:这个函数空白。
  • close()函数:这是个关闭函数,我估计是是否资源用的。
  • configure(Context context)函数:这个函数的主要作用是从flume的配置文件中读取信息。
  • initalize(Event event, byte[] cf):从event中读取内容,并配置HBase的column family
  • getActions():创建put变量,每个put对应着hbase的一行数据。
  • getIncrements():将hbase的自增列加1

Configure(Context context)函数

    // 读取flume配置文件中的rowPrefix,rowPrefix的默认值是default
    rowPrefix = context.getString("rowPrefix", "default");

    // 读取flume配置文件中的incrementRow,默认值是inRow
    incrementRow =
        context.getString("incrementRow", "incRow").getBytes(Charsets.UTF_8);

    // 读取flume配置文件中的suffix,默认值是uuid
    String suffix = context.getString("suffix", "uuid");

    // 读取flume配置文件中的payloadColumn,默认之是pCol。payloadColumn对应这hbase的列名
    String payloadColumn = context.getString("payloadColumn","pCol");
    
    // 读取flume配置文件中的incrementColumn,默认值是iCol
    String incColumn = context.getString("incrementColumn","iCol");
    
    if(payloadColumn != null && !payloadColumn.isEmpty()) {
      // 这几行代码是配置hbase中的rowkey前缀   
      if(suffix.equals("timestamp")){
        keyType = KeyType.TS;
      } else if (suffix.equals("random")) {
        keyType = KeyType.RANDOM;
      } else if(suffix.equals("nano")){
        keyType = KeyType.TSNANO;
      } else {
        keyType = KeyType.UUID;
      }
      plCol = payloadColumn.getBytes(Charsets.UTF_8);
    }
    if(incColumn != null && !incColumn.isEmpty()) {
      incCol = incColumn.getBytes(Charsets.UTF_8);
    }

对于Configure函数,主要需要说明的flume配置文件和代码之间的对应关系。
比如,如果你在flume的配置文件中有一行如: a1.sinks.k1.serializer.payloadColumn=colName。
那么Configure函数中的context.getString("payloadColumn", "pCol")的返回值就是colName.
同样如果你设置 a1.sinks.k1.serializer.rowPrefix=123456, 那么context.getString("rowPrefix", "default")的返回值就是123456.

initalize(Event event, byte[] cf)函数

public void initialize(Event event, byte[] cf) {
    this.payload = event.getBody();
    this.cf = cf;
  }
这个函数代码简单,cf表示hbase中的column family; event是flume的一个事件,是flume数据流中的一个data object。如果flume的source是文本文件的话,文件中的每一行就会产生一个flume event。

getActions()函数

  public List<Row> getActions() throws FlumeException {
    List<Row> actions = new LinkedList<Row>();
    if(plCol != null){
      byte[] rowKey;
      try {
        // 配置rowkey,具体靠参考SimpleRowKeyGenerator类
        if (keyType == KeyType.TS) {
          rowKey = SimpleRowKeyGenerator.getTimestampKey(rowPrefix);
        } else if(keyType == KeyType.RANDOM) {
          rowKey = SimpleRowKeyGenerator.getRandomKey(rowPrefix);
        } else if(keyType == KeyType.TSNANO) {
          rowKey = SimpleRowKeyGenerator.getNanoTimestampKey(rowPrefix);
        } else {
          rowKey = SimpleRowKeyGenerator.getUUIDKey(rowPrefix);
        }
 
        // 创建rowkey的一个put
        Put put = new Put(rowKey);
        
        // 在put中添加一列数据。columnfamily是cf,colunname是plCol,value是payload。
        // plCol是payloadColumn的byte形式。而payloadColumn初始化于Configure函数,来自于flume的配置文件
        // payload初始化于initalize函数,来自于event
        put.add(cf, plCol, payload);
        
        actions.add(put);
      } catch (Exception e){
        throw new FlumeException("Could not get row key!", e);
      }

    }
    return actions;
  }
getActions函数,它生成一个put实例,put最后插入到hbase中。需要注意的是put实例中所有的数据来源。
plCol来自于payloadColumn, payloadColumn来自于flume的配置文件;cf也是来自于flume配置文件;payload来自于event。
plCol对应hbase中的colum, cf对应hbase中的columnfamily,payload对应hbase中的value。

getIncrement()函数

    public List<Increment> getIncrements(){
      List<Increment> incs = new LinkedList<Increment>();
      if(incCol != null) {
        Increment inc = new Increment(incrementRow);
        inc.addColumn(cf, incCol, 1);
        incs.add(inc);
      }
      return incs;
    }
该函数作用是在hbase增添一个自增序列。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值