HBase协处理器同步二级索引到Solr(续)
二、解决思路
三、代码
3.1 读取config文件内容
3.2 封装SolrServer的获取方式
3.3 编写提交数据到Solr的代码
3.4 拦截HBase的Put和Delete操作信息
四、 使用
一、 已知的问题和不足
在上一个版本中,实现了使用HBase的协处理器将HBase的二级索引同步到Solr中,但是仍旧有几个缺陷:
- 写入Solr的Collection是写死在代码里面,且是唯一的。如果我们有一张表的数据希望将不同的字段同步到Solr中该如何做呢?
- 目前所有配置相关信息都是写死到了代码中的,是否可以添加外部配置文件。
- 原来的方法是每次都需要编译新的Jar文件单独运行,能否将所有的同步使用一段通用的代码完成?
二、解决思路
针对上面的三个主要问题,我们一一解决
- 通常一张表会对应多个SolrCollection以及不同的Column。我们可以使用
Map[表名->List[(Collection1,List[Columns]),(Collection2,List[Columns])...]]这样的类型,根据表名获取所有的Collection和Column。 - 通过Typesafe Config读取外部配置文件,达到所有信息可配的目的。
- 所有的数据都只有Put和Delete,只要我们拦截到具体的消息之后判断当前的表名,然后根据问题一中的Collection和Column即可写入对应的SolrServer。在协处理器中获取表名的是
e.getEnvironment().getRegion().getTableDesc().getTableName().getNameAsString()其中e是ObserverContext;
三、代码
3.1 读取config文件内容
使用typesafe的config组件读取morphlines.conf文件,将内容转换为 Map<String,List<HBaseIndexerMappin>>。具体代码如下
public class ConfigManager {private static SourceConfig sourceConfig = new SourceConfig();public static Config config;static {sourceConfig.setConfigFiles("morphlines.conf");config = sourceConfig.getConfig();}public static Map<String,List<HBaseIndexerMappin>> getHBaseIndexerMappin(){Map<String,List<HBaseIndexerMappin>> mappin = new HashMap<String, List<HBaseIndexerMappin>>();Config mappinConf = config.getConfig("Mappin");List<String> tables = mappinConf.getStringList("HBaseTables");for (String table :tables){List<Config> confList = (List<Config>) mappinConf.getConfigList(table);List<HBaseIndexerMappin> maps = new LinkedList<HBaseIndexerMappin>();for(Config tmp :confList){HBaseIndexerMappin map = new HBaseIndexerMappin();map.solrConnetion = tmp.getString("SolrCollection");map.columns = tmp.getStringList("Columns");maps.add(map);}mappin.put(table,maps);}return mappin;}}
3.2 封装SolrServer的获取方式
因为目前我使用的环境是Solr和HBase公用的同一套Zookeeper,因此我们完全可以借助HBase的Zookeeper信息。HBase的协处理器是运行在HBase的环境中的,自然可以通过HBase的Configuration获取当前的Zookeeper节点和端口,然后轻松的获取到Solr的地址。
public class SolrServerManager implements LogManager {static Configuration conf = HBaseConfiguration.create();public static String ZKHost = conf.get("hbase.zookeeper.quorum","bqdpm1,bqdpm2,bqdps2");public static String ZKPort = conf.get("hbase.zookeeper.property.clientPort","2181");public static String SolrUrl = ZKHost + ":" + ZKPort + "/" + "solr";public static int zkClientTimeout = 1800000;// 心跳public static int zkConnectTimeout = 1800000;// 连接时间public static CloudSolrServer create(String defaultCollection){log.info("Create SolrCloudeServer .This collection is " + defaultCollection);CloudSolrServer solrServer = new CloudSolrServer(SolrUrl);solrServer.setDefaultCollection(defaultCollection);solrServer.setZkClientTimeout(zkClientTimeout);solrServer.setZkConnectTimeout(zkConnectTimeout);return solrServer;}}
3.3 编写提交数据到Solr的代码
理想状态下,我们时时刻刻都需要提交数据到Solr中,但是事实上我们数据写入的时间是比较分散的,可能集中再每一天的某几个时间点。因此我们必须保证在高并发下能达到一定数据量自动提交,在低并发的情况下能隔一段时间写入一次。只有两种机制并存的情况下才能保证数据能即时写入。
public class SolrCommitTimer extends TimerTask implements LogManager {public Map<String,List<SolrInputDocument>> putCache = new HashMap<String, List<SolrInputDocument>>();//Collection名字->更新(插入)操作缓存public Map<String,List<String>> deleteCache = new HashMap<String, List<String>>();//Collection名字->删除操作缓存Map<String,CloudSolrServer> solrServers = new HashMap<String, CloudSolrServer>();//Collection名字->SolrServersint maxCache = ConfigManager.config.getInt("MaxCommitSize");// 任何时候,保证只能有一个线程在提交索引,并清空集合final static Semaphore semp = new Semaphore(1);//添加Collection和SolrServerpublic void addCollecttion(String collection,CloudSolrServer server){this.solrServers.put(collection,server);}//往Solr添加(更新)数据public UpdateResponse put(CloudSolrServer server,SolrInputDocument doc) throws IOException, SolrServerException {server.add(doc);return server.commit(false, false);}//往Solr添加(更新)数据public UpdateResponse put(CloudSolrServer server,List<SolrInputDocument> docs) throws IOException, SolrServerException {server.add(docs);return server.commit(false, false);}//根据ID删除Solr数据public UpdateResponse delete(CloudSolrServer server,String rowkey) throws IOException, SolrServerException {server.deleteById(rowkey);return server.commit(false, false);}//根据ID删除Solr数据public UpdateResponse delete(CloudSolrServer server,List<String> rowkeys) throws IOException, SolrServerException {server.deleteById(rowkeys);return server.commit(false, false);}//将doc添加到缓存public void addPutDocToCache(String collection, SolrInputDocument doc) throws IOException, SolrServerException, InterruptedException {semp.acquire();log.debug("addPutDocToCache:" + "collection=" + collection + "data=" + doc.toString());if(!putCache.containsKey(collection)){List<SolrInputDocument> docs = new LinkedList<SolrInputDocument>();docs.add(doc);putCache.put(collection,docs);}else {List<SolrInputDocument> cache = putCache.get(collection);cache.add(doc);if (cache.size() >= maxCache) {try {this.put(solrServers.get(collection), cache);} finally {putCache.get(collection).clear();}}}semp.release();//释放信号量}//添加删除操作到缓存public void addDeleteIdCache(String collection,String rowkey) throws IOException, SolrServerException, InterruptedException {semp.acquire();log.debug("addDeleteIdCache:" + "collection=" + collection + "rowkey=" + rowkey);if(!deleteCache.containsKey(collection)){List<String> rowkeys = new LinkedList<String>();rowkeys.add(rowkey);deleteCache.put(collection,rowkeys);}else{List<String> cache = deleteCache.get(collection);cache.add(rowkey);if (cache.size() >= maxCache) {try{this.delete(solrServers.get(collection),cache);}finally {putCache.get(collection).clear();}}}semp.release();//释放信号量}@Overridepublic void run() {try {semp.acquire();log.debug("开始插入....");Set<String> collections = solrServers.keySet();for(String collection:collections){if(putCache.containsKey(collection) && (!putCache.get(collection).isEmpty()) ){this.put(solrServers.get(collection),putCache.get(collection));putCache.get(collection).clear();}if(deleteCache.containsKey(collection) && (!deleteCache.get(collection).isEmpty())){this.delete(solrServers.get(collection),deleteCache.get(collection));deleteCache.get(collection).clear();}}} catch (InterruptedException e) {e.printStackTrace();} catch (Exception e) {log.error("Commit putCache to Solr error!Because :" + e.getMessage());}finally {semp.release();//释放信号量}}}
3.4 拦截HBase的Put和Delete操作信息
在每个prePut和preDelete中拦截操作信息,记录表名、列名、值。将这些信息根据表名和Collection名进行分类写入缓存。
public class HBaseIndexerToSolrObserver extends BaseRegionObserver implements LogManager{Map<String,List<HBaseIndexerMappin>> mappins = ConfigManager.getHBaseIndexerMappin();Timer timer = new Timer();int maxCommitTime = ConfigManager.config.getInt("MaxCommitTime"); //最大提交时间,sSolrCommitTimer solrCommit = new SolrCommitTimer();public HBaseIndexerToSolrObserver(){log.info("Initialization HBaseIndexerToSolrObserver ...");for(Map.Entry<String,List<HBaseIndexerMappin>> entry : mappins.entrySet() ){List<HBaseIndexerMappin> solrmappin = entry.getValue();for(HBaseIndexerMappin map:solrmappin){String collection = map.solrConnetion;//获取Collection名字log.info("Create Solr Server connection .The collection is " + collection);CloudSolrServer solrserver = SolrServerManager.create(collection);//根据Collection初始化SolrServer连接solrCommit.addCollecttion(collection,solrserver);}}timer.schedule(solrCommit, 10 * 1000L, maxCommitTime * 1000L);}@Overridepublic void postPut(ObserverContext<RegionCoprocessorEnvironment> e,Put put, WALEdit edit, Durability durability) throws IOException {String table = e.getEnvironment().getRegion().getTableDesc().getTableName().getNameAsString();//获取表名String rowkey= Bytes.toString(put.getRow());//获取主键SolrInputDocument doc = new SolrInputDocument();List<HBaseIndexerMappin> mappin = mappins.get(table);for(HBaseIndexerMappin mapp : mappin){for(String column : mapp.columns){String[] tmp = column.split(":");String cf = tmp[0];String cq = tmp[1];if(put.has(Bytes.toBytes(cf),Bytes.toBytes(cq))){Cell cell = put.get(Bytes.toBytes(cf),Bytes.toBytes(cq)).get(0);//获取制定列的数据Map<String, String > operation = new HashMap<String,String>();operation.put("set",Bytes.toString(CellUtil.cloneValue(cell)));doc.setField(cq,operation);//使用原子更新的方式将HBase二级索引写入Solr}}doc.addField("id",rowkey);try {solrCommit.addPutDocToCache(mapp.solrConnetion,doc);//添加doc到缓存} catch (SolrServerException e1) {e1.printStackTrace();} catch (InterruptedException e1) {e1.printStackTrace();}}}@Overridepublic void postDelete(ObserverContext<RegionCoprocessorEnvironment> e,Delete delete,WALEdit edit,Durability durability) throws IOException{String table = e.getEnvironment().getRegion().getTableDesc().getTableName().getNameAsString();String rowkey= Bytes.toString(delete.getRow());List<HBaseIndexerMappin> mappin = mappins.get(table);for(HBaseIndexerMappin mapp : mappin){try {solrCommit.addDeleteIdCache(mapp.solrConnetion,rowkey);//添加删除操作到缓存} catch (SolrServerException e1) {e1.printStackTrace();} catch (InterruptedException e1) {e1.printStackTrace();}}}}
四、 使用
首先需要添加morphlines.conf文件。里面包含了需要同步数据到Solr的HBase表名、对应的Solr Collection的名字、要同步的列、多久提交一次、最大批次容量的相关信息。具体配置如下:
#最大提交时间(单位:秒)MaxCommitTime = 30#最大批次容量MaxCommitSize = 10000Mappin {HBaseTables: ["HBASE_OBSERVER_TEST"] #需要同步的HBase表名"HBASE_OBSERVER_TEST": [{SolrCollection: "bqjr" #Solr Collection名字Columns: ["cf1:test_age", #需要同步的列,格式<列族:列>"cf1:test_name"]},]}
该配置文件默认放在各个节点的/etc/hbase/conf/下。如果你希望将配置文件路径修改为其他路径,请修改com.bqjr.bigdata.HBaseObserver.comm.config.SourceConfig类中的configHome路径。
然后将代码打包,上传到HDFS中,将协处理器添加到对应的表中。
#先禁用这张表disable 'HBASE_OBSERVER_TEST'#为这张表添加协处理器,设置的参数具体为: jar文件路径|类名|优先级(SYSTEM或者USER)alter 'HBASE_OBSERVER_TEST','coprocessor'=>'hdfs://hostname:8020/ext_lib/HBaseObserver-1.0.0.jar|com.bqjr.bigdata.HBaseObserver.server.HBaseIndexerToSolrObserver||'#启用这张表enable 'HBASE_OBSERVER_TEST'#删除某个协处理器,"$<bumber>"后面跟的ID号与desc里面的ID号相同alter 'HBASE_OBSERVER_TEST',METHOD=>'table_att_unset',NAME => 'coprocessor$1'
如果需要新增一张表同步到Solr。只需要修改morphlines.conf文件,分发倒各个节点。然后将协处理器添加到HBase表中,这样就不用再次修改代码了。
HBase协处理器同步二级索引到Solr(续)的更多相关文章
- HBase协处理器同步二级索引到Solr
一. 背景二. 什么是HBase的协处理器三. HBase协处理器同步数据到Solr四. 添加协处理器五. 测试六. 协处理器动态加载 一. 背景 在实际生产中,HBase往往不能满足多维度分析,我们 ...
- Hbase(三) hbase协处理器与二级索引
一.协处理器—Coprocessor 1. 起源Hbase 作为列族数据库最经常被人诟病的特性包括:无法轻易建立“二级索引”,难以执 行求和.计数.排序等操作.比如,在旧版本的(<0.92)Hb ...
- HBase 协处理器实现二级索引
HBase在0.92之后引入了coprocessors,提供了一系列的钩子,让我们能够轻易实现访问控制和二级索引的特性.下面简单介绍下两种coprocessors,第一种是Observers,它实际类 ...
- Lily HBase Indexer同步HBase二级索引到Solr丢失数据的问题分析
一.问题描述二.分析步骤2.1 查看日志2.2 修改Solr的硬提交2.3 寻求StackOverFlow帮助2.4 修改了read-row="never"后,丢失部分字段2.5 ...
- 通过phoenix在hbase上创建二级索引,Secondary Indexing
环境描述: 操作系统版本:CentOS release 6.5 (Final) 内核版本:2.6.32-431.el6.x86_64 phoenix版本:phoenix-4.10.0 hbase版本: ...
- CDH版本Hbase二级索引方案Solr key value index
概述 在Hbase中,表的RowKey 按照字典排序, Region按照RowKey设置split point进行shard,通过这种方式实现的全局.分布式索引. 成为了其成功的最大的砝码. 然而单一 ...
- HBase Region级别二级索引
我们会经常谈及二级索引,这是对全表数据进行另外一种方式的组织存储,是针对table级别的.如果要为HBase上的表实现一个强一致性的二级索引,那么就无法逃避分布式事务,而这一直是用户最期待的功能. 而 ...
- hbase基于solr配置二级索引
一.概述 Hbase适用于大表的存储,通过单一的RowKey查询虽然能快速查询,但是对于复杂查询,尤其分页.查询总数等,实现方案浪费计算资源,所以可以针对hbase数据创建二级索引(Hbase Sec ...
- HBase的二级索引
使用HBase存储中国好声音数据的案例,业务描述如下: 为了能高效的查询到我们需要的数据,我们在RowKey的设计上下了不少功夫,因为过滤RowKey或者根据RowKey查询数据的效率是最高的,我们的 ...
随机推荐
- AVPlayerViewController视频播放器
前言 iOS8之后系统自带使用AVPlayerViewController播放视频 AVPlayerViewController AVPlayerViewController和导航控制器差不多,需要将 ...
- SPOJ GSS1 Can you answer these queries I ——线段树
[题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...
- 常州模拟赛d3t1 神在夏至祭降下了神谕
题目描述 我们村子在过去的400年中,断绝与下界的接触,过着自给自足的生活. 夏至祭是一场迎接祖灵于夏季归来,同时祈求丰收的庆典. 村里的男人会在广场上演出夏之军和冬之军的战争.夏之军会打倒冬之军的大 ...
- gcc/g++ 编译时出现:“对’xxxx’未定义的引用,collect2: error: ld returned 1 exit status” 的错误
出现的问题: 在使用 make 编译实现一个程序时,出现了下面的错误.查看程序源文件所在的目录时发现程序已经完成了编译,并生成了 list_repo.o 的文件,说明是在程序链接生成可执行文件时发生了 ...
- 在 Linux 实例上自动安装并运行 VNC Server
原文网址:https://help.aliyun.com/knowledge_detail/41181.html?spm=5176.8208715.110.11.4c184ae8mlC7Yy 您可以使 ...
- 装B技能GET起来!Apple Pay你会用了吗?
科技圈儿有一个自带光环的品牌 它每次一有任何动静 不用宣传 也不用刻意营销 消息还是能传天下 2月18日 你敢说你的朋友圈儿没有被下面这个词儿刷屏? Apple Pay 这不,我就跟着凑凑热闹,开个小 ...
- hzwer与逆序对
codevs——4163 hzwer与逆序对 貌似这个题和上个题是一样的((⊙o⊙)…) 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目 ...
- Java集合——遍历集合元素并修改
Java集合——遍历集合元素并修改 摘要:本文主要总结了遍历集合的方式,以及在遍历时修改集合要注意的问题. 遍历Collection 对List和Set的遍历,有四种方式,下面以ArrayList为例 ...
- 使用datatables实现列宽设置、水平滚动条、显示某列部分内容
示例 1.//使用 columnDefs 给列设置宽度 $('#example').DataTable( { "columnDefs": [ //给第一列指定宽度为表格整个宽度的2 ...
- 【Android小项目】找不同,改编自"寻找房祖名"的一款开源小应用。
近期在微信朋友圈"寻找房祖名"和"万里寻刀"这类小游戏比較火.我试着写了一个android版本号的,里面全是一系列的形近字,实现原理非常easy:用一个Grid ...