hbase提供了类似于触发器的组件observer,类似于存储过程的endpoint。
hbase中的observer分别三类,MasterObserver、RegionObserver、WALObserevr
使用java代码也可以达到相同的效果,但是服务器端的observer相对是比较高效的
一、自定义obserevr
1.1继承BaseMasterObserver,重写其相应的方法
public class MyObserver extends BaseMasterObserver{ private Logger logger = LoggerFactory.getLogger(MyObserver.class); @Override public void preCreateTable(ObserverContext<MasterCoprocessorEnvironment> ctx, HTableDescriptor desc, HRegionInfo[] regions) throws IOException { logger.info("**************create table******************"); } @Override public void preDeleteTable(ObserverContext<MasterCoprocessorEnvironment> ctx, TableName tableName) throws IOException { logger.info("**************delete table******************"); } }
1.2打成jar包放到hbase的lib目录下
1.3修改hbase-site.xml文件
这是hbase-default.xml文件中的配置
<property> <name>hbase.coprocessor.master.classes</name> <value></value> </property>
修改为:
<property> <name>hbase.coprocessor.master.classes</name> <value>com.lanyun.hadoop2.MyObserver</value> </property>
1.4重启hbase集群
1.5创建一张表,并且观察日志的变化
创建语句:
create 'tt1','f1'
日志的输出:
2016-07-21 00:10:19,602 INFO [FifoRpcScheduler.handler1-thread-1] hadoop2.MyObserver: **************create table****************** 2016-07-21 00:10:19,602 INFO [FifoRpcScheduler.handler1-thread-1] master.HMaster: Client=root//192.168.163.129 create 'tt1', {NAME => 'f1', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}