分为两种类型
- observer 观察者相当于触发器
- Endpoint终端相当于存储过程
下面的观察者实现查询之前替换掉行键为Jack的KeyValue
import java.io.IOException; import java.util.List; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver; import org.apache.hadoop.hbase.coprocessor.ObserverContext; import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; import org.apache.hadoop.hbase.util.Bytes; public class RegionObserverTest extends BaseRegionObserver { private static byte[] fixed_rowkey = "Jack".getBytes(); @Override public void postGet(ObserverContext<RegionCoprocessorEnvironment> c, Get get, List<KeyValue> result) throws IOException { if (Bytes.equals(get.getRow(), fixed_rowkey)) { KeyValue kv = new KeyValue(get.getRow(), Bytes.toBytes("time"), Bytes.toBytes("time"), Bytes.toBytes(System .currentTimeMillis())); result.add(kv); } } }
加载协处理器命令
disable ‘students‘ alter ‘students‘ ,‘coprocessor‘=>‘hdfs://ns1/coprocessor.jar|com.hbase.RegionObserverTest||‘ enable ‘students‘
时间: 2024-11-01 09:19:20