电信客服项目之协处理器

协处理器在本项目中主要是用来,在向hbase中put一条数据时同时也要put一条call1和call2颠倒的数据

注意:

1、协处理器如果是配置到hbase-site.xml文件中,默认是对全部的表都进行处理

2、如果不配置到xml文件,只指定某个表,那么就只对改表有效

3、注意将consumer进行编译,打包,打包后上传到hbase的lib包下,记住记住,一定要分发这个jar包

4、一定要慎用,如果出现问题,就在hbase-site.xml中配置,见之前博客

本项目的协处理器:

public class CalleeConprocess extends BaseRegionObserver {

    private int  regions =  Integer.parseInt(PropertyUtil.getProperty("hbase.regions"));
    private String cf2 = PropertyUtil.getProperty("habse.cf2");
    /*
      本部分是实现,在put插入数据后,在插入被叫数据,虽然空间上是浪费了,但是效率提高了
     */
    @Override
    public void postPut(ObserverContext<RegionCoprocessorEnvironment> e, Put put1, WALEdit edit, Durability durability) throws IOException {

        //先获取协处理器中的表,也就是遍regionserver中的所有的表
        String tableName = e.getEnvironment().getRegion().getRegionInfo().getTable().getNameAsString();

        //获取要操作表的名称,
        String currentTable = PropertyUtil.getProperty("hbase.table.name");
        
        //!匹配两者
        if(!tableName.equals(currentTable)){
            return;
        }
        //取出上一个操作的put
        String oldRow = Bytes.toString(put1.getRow());
        String[] splits = oldRow.split("_");
        //!切割后的数据判断flag是不是0,不是0的话会死循环
        if("1".equals(splits[4])) return;

        String call1 = splits[1];
        String buildTime = splits[2];
        String call2 = splits[3];
        String flag = splits[4];
        String  duration = splits[5];
        //获取上一个put,并生成新的,要插入的put

        String newPar = HbaseUtil.getPartionKey(call2,buildTime,regions);
        String newKey  = HbaseUtil.getRowKey(newPar,call2,buildTime,call1,"1",duration);

        Put newPut = new Put(Bytes.toBytes(newKey));

        //在新的put中添加数据

        newPut.addColumn(Bytes.toBytes(cf2),Bytes.toBytes("call1"),Bytes.toBytes(call2));
        newPut.addColumn(Bytes.toBytes(cf2),Bytes.toBytes("buildTime"),Bytes.toBytes(buildTime));
        newPut.addColumn(Bytes.toBytes(cf2),Bytes.toBytes("call2"),Bytes.toBytes(call1));
        newPut.addColumn(Bytes.toBytes(cf2),Bytes.toBytes("flag"),Bytes.toBytes("1"));
        newPut.addColumn(Bytes.toBytes(cf2),Bytes.toBytes("duration"),Bytes.toBytes(duration));

        //插入数据
        Connection connection = ConnectionFactory.createConnection(Constant.hBaseConfiguration);
        Table table = connection.getTable(TableName.valueOf(tableName));

        table.put(newPut);
        //关闭资源
        table.close();
        connection.close();
    }
}

在HbaseUtil中修改

hTableDescriptor.addCoprocessor("com.xin.conprocess.CalleeConprocess");

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值