欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

手机协处理器java,HBase1.x实战:协处理器Java开发实例--ObserverCoprocessor

发布时间:2023/12/20 java 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 手机协处理器java,HBase1.x实战:协处理器Java开发实例--ObserverCoprocessor 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

概述:

HBase日常开发中可能需要根据查询条件对固定表部分列建立二级索引存到另外一张表,或要求在插入A表的同时,需要同步部分列到B表中,我们就可以通过协处理实现这个需求:

实例:

A表:

表名:student

主键:rowID

列族:info,

列:info:name,info:age,info:score,info:adress,info:phoneNumber

B表:

表名:index_student_table

主键:name(来自A表)

列族:info,

列:info:age,info:score

RegionObserver协处理器类PutIndexObserver,向A表插入数据的同时,协处理器向B表中name为主键,age,score为列插入数据。

代码实例:

package com.unicom.ljs.hbase125.coprocessor;import java.io.IOException;import java.util.List;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.CellUtil;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Durability;import org.apache.hadoop.hbase.client.HTableInterface;import org.apache.hadoop.hbase.client.Put;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.regionserver.wal.WALEdit;import org.apache.hadoop.hbase.util.Bytes;/*** @author: Created By lujisen* @company ChinaUnicom Software JiNan* @date: 2020-02-02 13:28* @version: v1.0* @description: com.unicom.ljs.hbase125.coprocessor*//*协处理器,获取put数据,向index_student_table表同步*/public class PutIndexObserver extends BaseRegionObserver{@Overridepublic void postPut(ObserverContext env,Put put, WALEdit edit, Durability durability) throws IOException {/*获取协处理器需要插入数据的索引表*/HTableInterface table = env.getEnvironment().getTable(TableName.valueOf("index_student_table"));// 获取值List cellList1 = put.get(Bytes.toBytes("info"), Bytes.toBytes("name"));List cellList2 = put.get(Bytes.toBytes("info"), Bytes.toBytes("age"));List cellList3 = put.get(Bytes.toBytes("info"), Bytes.toBytes("score"));// 以name作为rowkey age,score作为作为info列族下的列名插入索引表for (Cell cell1 : cellList1) {// 列info:name的值作为二级索引表的rowkeyPut indexPut = new Put(CellUtil.cloneValue(cell1));for (Cell cell2 : cellList2) {indexPut.add(Bytes.toBytes("info"), Bytes.toBytes("age"), CellUtil.cloneValue(cell2));}for (Cell cell3 : cellList3) {indexPut.add(Bytes.toBytes("info"), Bytes.toBytes("score"), CellUtil.cloneValue(cell3));}// 插入索引表数据table.put(indexPut);}// 关闭表table.close();}}

数据插入主函数代码:

package com.unicom.ljs.hbase125.coprocessor;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Connection;import org.apache.hadoop.hbase.client.ConnectionFactory;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.util.Bytes;import java.util.ArrayList;import java.util.List;/*** @author: Created By lujisen* @company ChinaUnicom Software JiNan* @date: 2020-02-02 15:07* @version: v1.0* @description: com.unicom.ljs.hbase125.coprocessor*/public class StudentCoprocessor{public static Configuration conf =null;public static Connection conn =null;public static final String tableName="student";public static final int insertCount=1;public static void main(String[] args) throws Exception{conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum","salver158.hadoop.unicom,salver31.hadoop.unicom,salver32.hadoop.unicom");conf.set("hbase.zookeeper.property.clientPort", "2181");conf.set("zookeeper.znode.parent", "/hbase-unsecure");conn = ConnectionFactory.createConnection(conf);// 获取表HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));List insertPutList=new ArrayList();for(int i=0;i

验证步骤:

1.将代码打jar包,上传到hdfs的/tmp目录下

hadoop fs -put studentCoprocessor.jar /tmp/

2.登录hbase shell控制台,新建表student,添加协处理器,依次执行:

create 'student','info'disable 'student'alter'student',METHOD =>'table_att','coprocessor' =>'hdfs://10.124.165.98:8020/tmp/studentCoprocessor.jar|com.unicom.ljs.hbase125.coprocessor.PutIndexObserver|100'enable'student'

3.新建需要同步的数据表index_student_table

create'index_student_table','info'

4.执行“数据插入主函数”,查看表数据进行验证:

5.表student和表index_student_table都已经插入了数据,验证完成。

总结

以上是生活随笔为你收集整理的手机协处理器java,HBase1.x实战:协处理器Java开发实例--ObserverCoprocessor的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。