Key-Value Store Indexer(1):实现solr对hbase中列簇进行二级索引

18 篇文章 1 订阅

一、实现功能

最近,因为hbase查询速度非常慢,尤其通过模糊搜索无法满足需求。所以,希望通过在solr中建立对应列簇的二级索引,进行模糊搜索。通过查找,使用CDH的Key-Value Store Indexer组件,对hbase指定表的指定列簇做监控,实现自动增量填充至solr指定字段,做模糊搜索。

二、环境

CDH5.15.2

三、配置步骤

1.在 HBase 指定列簇上启用复制,最小单位是列簇column family
(1)启用CDH的 HBase复制功能。(默认开启)
 
(2)对于现有表

hbase shell #进入hbase

disable 'hbase_solr'
alter 'hbase_solr', {NAME => 'data_solr', REPLICATION_SCOPE => 1}
enable 'hbase_solr'

(3)如果没有表则新建

create 'hbase_solr', {NAME => 'data_solr', REPLICATION_SCOPE => 1}

2.创建SolrCloud 实例collection
当对应hbase对应的列簇变化时,solr自动在该collection建立索引!

(1)创建solr配置文件:

solrctl instancedir --generate /opt/datas/solr/hbase_solr

  (2)编辑实例的schema.xml文件,根据自己的需求确定。

vi /opt/datas/solr/hbase_solr/conf/schema.xml 

    备注:schema.xml中的域,即对应hbase中列簇下的列。所以,一定要定义好。在本例的title和subject两个域,在solr默认创建的文件schema.xml中就有,所以可以忽略。
(3)上传配置文件至zookeeper,并且命名为hbase_solr

solrctl instancedir --create hbase_solr /opt/datas/solr/hbase_solr

(4)使用上传zk配置文件hbase_solr建立collection:hbase_solr

solrctl collection --create hbase_solr -s 2 -c hbase_solr

3.创建 Lily HBase Indexer 配置

$ vi /etc/solr/conf/morphline-hbase-mapper.xml

写入:
<?xml version="1.0"?>
<indexer table="hbase_solr" mapper="com.ngdata.hbaseindexer.morphline.MorphlineResultToSolrMapper">

   <!-- The relative or absolute path on the local file system to the morphline configuration file. -->
   <!-- Use relative path "morphlines.conf" for morphlines managed by Cloudera Manager -->
   <param name="morphlineFile" value="morphlines.conf"/>

   <!-- The optional morphlineId identifies a morphline if there are multiple morphlines in morphlines.conf -->
   <!-- <param name="morphlineId" value="morphline1"/> -->

</indexer>

(1)关键点:table="hbase_solr",即为solr实例hbase_solr监控的hbase的对应表名字
(2)关键点:value="morphlines.conf",在此没有使用相对或者绝对目录,即为默认使用zk管理。

4.创建 Morphline 配置文件.
在CDH 管理界面进入Key-Value Store Indexer面板->配置->服务范围->Morphlines->Morphlines文件。将下列配置黏贴:

  SOLR_LOCATOR : {
  # Name of solr collection
  collection : hbase_solr
  
  # ZooKeeper ensemble
  zkHost : "$ZK_HOST" 
}

morphlines : [
  {
    id : morphline1
    importCommands : ["org.kitesdk.morphline.**", "com.ngdata.**"]

    commands : [                    
      {
        extractHBaseCells {
          mappings : [
            {
              inputColumn : "data_solr:title"
              outputField : "title"
              type : string 
              source : value
            }

            {
              inputColumn : "data_solr:subject"
              outputField : "subject"
             type : "byte[]"
             source : value
            }
          ]
        }
      }

      { logTrace { format : "output record: {}", args : ["@{}"] } }
    ]
  }
]

解释:

  • id:表示当前morphlines文件的ID名称。
  • importCommands:需要引入的命令包地址。
  • extractHBaseCells:该命令用来读取HBase列数据并写入到SolrInputDocument对象中,该命令必须包含零个或者多个mappings命令对象。
  • mappings:用来指定HBase列限定符的字段映射。
  • inputColumn:需要写入到solr中的HBase列字段。值包含列族和列限定符,并用‘:’分开。其中列限定符也可以使用通配符‘*’来表示,譬如可以使用data:*表示读取只要列族为data的所有hbase列数据,也可以通过data:my*来表示读取列族为data列限定符已my开头的字段值。
  • outputField:用来表示morphline读取的记录需要输出的数据字段名称,该名称必须和solr中的schema.xml文件的字段名称保持一致,否则写入不正确。
  • type:用来定义读取HBase数据的数据类型,我们知道HBase中的数据都是以byte[]的形式保存,但是所有的内容在Solr中索引为text形式,所以需要一个方法来把byte[]类型转换为实际的数据类型。type参数的值就是用来做这件事情的。现在支持的数据类型有:byte[](原封不动的拷贝hbase中的byte[]数据),int,long,string,boolean,float,double,short和bigdecimal。当然你也可以指定自定的数据类型,只需要实现com.ngdata.hbaseindexer.parse.ByteArrayValueMapper接口即可。
  • source:用来指定HBase的KeyValue那一部分作为索引输入数据,可选的有‘value’和’qualifier’,当为value的时候表示使用HBase的列值作为索引输入,当为qualifier的时候表示使用HBase的列限定符作为索引输入。

5.重启Lily HBase Indexer,并且重新分配配置文件。

6.创建Lily HBase Indexer实例
注册Lily HBase Indexer配置文件到Lily HBase Indexer Service服务中

hbase-indexer add-indexer --name hbase_solr --indexer-conf /etc/solr/conf/morphline-hbase-mapper.xml --connection-param solr.zk=zkip1:2181,zkip2:2181,zkip3:2181/solr --connection-param solr.collection=hbase_solr --zookeeper zkip1:2181,zkip2:2181,zkip3:2181
    Indexer added: hbase_solr

或者

hbase-indexer add-indexer --name hbase_solr --indexer-conf /etc/solr/conf/morphline-hbase-mapper.xml --connection-param solr.zk=zkip1,zkip2,zkip3/solr --connection-param solr.collection=hbase_solr --zookeeper zkip1:2181,zkip2:2181,zkip3:2181
        Indexer added: hbase_solr

7. hbase-indexer相关命令
(1)查询hbase-indexer实例

    /hbase-indexer list-indexers -dump
    参数说明
    list-indexers 动作命令
    -dump 添加此参数可以显示索引的配置详情

(2)删除

    ./hbase-indexer delete-indexer --name 'hbase_solr'
    参数说明
    delete-indexer 动作命令
    indexer_vip 要被删除的索引名称
    实例:
    hbase-indexer delete-indexer --name 'hbase_solr' --zookeeper 10.7.221.25:2181,10.7.221.26:2181,10.7.221.31:2181

8.测试
(1)HBase写入数据

put 'hbase_solr','2','data_solr:title','i am title'
put 'hbase_solr','2','data_solr:subject','i am subject'

put 'hbase_solr','3','data_solr:title','i am title-3'
put 'hbase_solr','3','data_solr:subject','i am subject-3'

(2)solr查询测试
在hbase插入数据后,几乎同时,可以在solr中查询

四、总结

1.参考:CDH中Lily HBase Indexer(Key-value Store Indexer)的配置使用,连接:http://suroot.cn/339.html
2.参考:Lily HBase Indexer使用整理,连接:https://blog.csdn.net/kissmelove01/article/details/45196941

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值