# hbase-study **Repository Path**: yeyouluo/hbase-study ## Basic Information - **Project Name**: hbase-study - **Description**: No description available - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-10 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 需求 1. 按照需求创建表 2. 联动删除好友关系 ## 实现 ### 编程 使用协处理器实现。继承`BaseRegionObserver`类,重写`preDelete()`方法。 ### 打包协处理器程序,上传到HDFS ```shell hdfs dfs -rm -f /processor/my*.jar hdfs dfs -mkdir -p /processor mv hbase-study-1.0-SNAPSHOT.jar my-delete-processor-v1.jar hdfs dfs -put ./my-delete-processor-v1.jar /processor hdfs dfs -ls /processor ``` ### 执行建表语句 在Idea中执行建表main方法,类路径为`com.yeyouluo.hbase.CreateHBaseTable` ### 加载协处理器 > 在hbase shell中执行 ```sql describe 'friend' alter 'friend',METHOD => 'table_att','Coprocessor'=>'hdfs://linux121:9000/processor/my-delete-processor-v1.jar|com.yeyouluo.hbase.MyDeleteProcessor|1001|' describe 'friend' scan 'friend' ``` 效果如下: ![](imgs/Snipaste_2020-08-10_13-07-40.png) ### 执行删除单元测试 > 在Idea中执行 执行`com.yeyouluo.hbase.HbaseClientDemo`的删除方法`deleteData()`,其完整用例如下: ```java /** * 删除一条数据 * * @throws IOException */ @Test public void deleteData() throws IOException { // 获取table对象 Table table = conn.getTable(TableName.valueOf(TABLE_NAME)); // 准备delete对象 Delete delete = new Delete(Bytes.toBytes("uid1")); delete.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid2")); // 删除数据 table.delete(delete); System.out.println("删除一条数据成功"); } ``` ### 加载协处理器 > 在hbase shell中执行 ```sql describe 'friend' alter 'friend',METHOD => 'table_att','Coprocessor'=>'hdfs://linux121:9000/processor/my-delete-processor-v1.jar|com.yeyouluo.hbase.MyDeleteProcessor|1001|' describe 'friend' scan 'friend' ``` 效果如下: ![](imgs/Snipaste_2020-08-10_13-07-54.png) 可以看到rowkey为`uid1`中少了cq为`uid2`的一列,rowkey为`uid2`也少了cq为`uid1`的一列。效果符合预期。