1 协处理器
协处理器分为 观察者 Observer 和 终端 Endpoint
1.1 观察者 Observer(类似传统数据库中的触发器 Trigger)
1.1.1 Coprocessor 下的接口
详细信息直接看接口上的注释吧!!!
1.1.2 Java API 的使用
继承对应的抽象类就行了,每个抽象类有不同的功能,见名知意,不解释 、、、
public class TestAddColumnBaseRegionObserver extends BaseRegionObserver{
@Override
public void prePut(ObserverContext<RegionCoprocessorEnvironment> e, Put put, WALEdit edit, Durability durability) throws IOException {
putUUIDColumnIfMissing(e, put, edit, durability);
}
private void putUUIDColumnIfMissing(ObserverContext<RegionCoprocessorEnvironment> e, Put put, WALEdit edit, Durability durability) {
try {
if(!put.has(Bytes.toBytes("cf1"), Bytes.toBytes("uuid"))) {
String uuid = MD5Util.encrypt(put.toString());
put.add(Bytes.toBytes("cf1"), Bytes.toBytes("uuid"), Bytes.toBytes(uuid));
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
1.1.3 打成 itdog8-yting-hbase-coprocessor-observer-1.0.0.jar 并上传到 hdfs(hbase shell 方式加载 coprocessor 要用到)
bash-4.1$ hadoop fs -put /usr/yting/hbase/coprocessor/test/itdog8-yting-hbase-coprocessor-observer-1.0.0.jar /usr/itdog8/yting/hbase/coprocessor/test/
1.1.4 hbase shell 方式加载 Coprocessor
hbase(main):017:0> alter ‘itdog8:hbase_coprocessor_test_1‘, ‘coprocessor‘=>‘hdfs:///usr/yting/hbase/coprocessor/test/itdog8-yting-hbase-coprocessor-observer-1.0.0.jar|com.itdog8.study.cloud.hbase.coprocessor.observer.TestAddColumnBaseRegionObserver|1001|arg1=1,arg2=2‘
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.1490 seconds
1.1.5 测试
hbase(main):025:0> scan ‘itdog8:hbase_coprocessor_test_1‘
ROW COLUMN+CELL
0 row(s) in 0.0130 seconds
hbase(main):026:0> put ‘itdog8:hbase_coprocessor_test_1‘, ‘row-zhagnsan‘, ‘cf1:name‘, ‘zhangsan‘
0 row(s) in 0.0120 seconds
hbase(main):027:0> scan ‘itdog8:hbase_coprocessor_test_1‘
ROW COLUMN+CELL
row-zhagnsan column=cf1:name, timestamp=1438919899641, value=zhangsan
row-zhagnsan column=cf1:uuid, timestamp=1438919899641, value=2a66e9ed97e9ded4c21d1ef555cfa69e
1 row(s) in 0.0110 seconds
hbase(main):028:0>
可以发现 HBase-Coprocessor-Observer(触发器)成功触发并执行了(还是叫触发器习惯点,也更容易理解)
我们put数据的时候,没有uuid的时候他put了uuid进去了
OK
1.1.6 hbase shell 方式卸载 Coprocessor
alter ‘itdog8:hbase_coprocessor_test_1‘, METHOD => ‘table_att_unset‘, NAME => ‘coprocessor$1‘
1.2 终端 Endpoint(类似传统数据库中的 存储过程)