* 写入另一张二级索引表index_ob_table,可以提高对于特定字段的查询效率
*/
@SuppressWarnings(“deprecation”)
public class PutObserver extends BaseRegionObserver{
@Override
public void postPut(ObserverContext《RegionCoprocessorEnvironment》 e,
Put put, WALEdit edit, Durability durability) throws IOException {
// 获取二级索引表
HTableInterface table = e.getEnvironment().getTable(TableName.valueOf(“index_ob_table”));
// 获取值
List《Cell》 cellList1 = put.get(Bytes.toBytes(“info”), Bytes.toBytes(“name”));
List《Cell》 cellList2 = put.get(Bytes.toBytes(“info”), Bytes.toBytes(“score”));
// 将数据插入二级索引表
for (Cell cell1 : cellList1) {
// 列info:name的值作为二级索引表的rowkey
Put indexPut = new Put(CellUtil.cloneValue(cell1));
for (Cell cell2 : cellList2) {
// 列info:score的值作为二级索引表中列info:score的值
indexPut.add(Bytes.toBytes(“info”), Bytes.toBytes(“score”), CellUtil.cloneValue(cell2));
}
// 数据插入二级索引表
table.put(indexPut);
}
// 关闭资源
table.close();
}
}
5.2 加载Observer
// 将PutObserver类打包后上传到HDFS
$ hadoopfs -put ovserver_put.jar /input
// 启动hbase shell
$hbase shell
// 创建数据表ob_table
》 create‘ob_table’,‘info’
// 创建二级索引表ob_table
》 create‘index_ob_table’,‘info’
// 加载协处理器
》disable ‘ob_table’
》 alter‘ob_table’,METHOD =》‘table_att’,‘coprocessor’ =》‘hdfs://localhost:9000/input/observer_put.jar|com.hbase.demo.observer.PutObserver|100’
》 enable‘ob_table’
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-59353-8.html
才能具备基本的判断分析能力
一剑之仇终将报