最近在看尚硅谷的电信客服项目,视频里用的是hbase1.x版本,而我用的是hbase2.4,在向hbase添加协处理器时出现了问题。
hbase2.x弃用了BaseRegionObserver,在百度后我使用了RegionObserver,但是把项目打包运行后发现协处理器并没有成功运行,也没有报错。 在查阅文档后发现还需要重写RegionCoprocessor下的getRegionObserver方法,重新打包后运行成功
package com.ct.comsumer.corprocessor;
import com.ct.common.bean.BaseDao;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Append;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.coprocessor.RegionObserver;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.wal.WALEdit;
import org.apache.hadoop.hbase.client.Put;
import com.ct.common.constant.Names;
import java.io.IOException;
import java.util.Optional;
public class InsertCalleeCoprocessor implements RegionObserver, RegionCoprocessor {
rows IOException
@Override
public void postPut(ObserverContext<RegionCoprocessorEnvironment> c, Put put, WALEdit edit) throws IOException {
}
@Override
public Optional<RegionObserver> getRegionObserver() {
return Optional.of((RegionObserver) this);
}
}
添加协处理器
Admin admin = getAdmin();
TableName tableName = TableName.valueOf(name);
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
if (coprocessorClass != null && !"".equals(coprocessorClass)){
tableDescriptorBuilder.setCoprocessor(coprocessorClass);
}
TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
}
|