Hbase二级索引+CDH+Lily

版权声明:原创文章,欢迎转载,转载请注明出处 https://blog.csdn.net/zhangshenghang/article/details/82911084

1.更改表结构,允许复制

已存在的表
disable 'tableName'
alter 'tableName',{NAME =>'fn', REPLICATION_SCOPE =>1}
enable 'tableName'
不存在的表
create ‘table‘,{NAME =>‘cf‘, REPLICATION_SCOPE =>1}
#其中1表示开启replication功能,0表示不开启,默认为0

2.创建相应的SolrCloud集合
接下来在安装有Solr的机器上运行
这里得路径和用户名都可以自己定义

# 生成实体配置文件:
 solrctl instancedir --generate /opt/hbase-indexer/index1
 # index1 意思代表用户

此时/opt/hbase-indexer/index1目录下会有个conf文件夹,我们修改下面得schema.xml文件.
在最下面新加一个字段

<field name="HBase_Indexer_Test_cf1_name" type="string" indexed="true" stored="true"/>
属性解析:
name:这里的name是自定义,但是后面要使用到,要和后面的Morphline.conf文件中的outputField属性对应。
type:字段类型
indexed:是否建立索引
stored:是否存储

注意:这里name字段它对应了我们后续需要修改Morphline.conf文件中的outputField属性。因此可以看成是hbase中需要创建索引的值。因此我们建议将其与表名和列族结合,格式建议如下:

HBase_Indexer_ZDTable_fn_name
Hbase_indexer_表名_列簇_列名

再修改solrconfig.xml,找到下面的配置将false改为true,这个是硬提交,会影响性能

    <autoCommit>
       <maxTime>${solr.autoCommit.maxTime:60000}</maxTime>
       <openSearcher>true</openSearcher>
     </autoCommit>

3.创建 collection实例并将配置文件上传到 zookeeper:

solrctl instancedir --create index1 /opt/hbase-indexer/index1

4.上传到 zookeeper之后,其他节点就可以从zookeeper下载配置文件。接下来创建 collection:

solrctl collection --create index1

如果希望将数据分散到各个节点进行存储和检索,则需要创建多个shard,需要使用如下命令

    solrctl collection --create bqjr -s 7-r 3-m 21

其中-s表示设置Shard数为7,-r表示设置的replica数为3,-m表示最大shards数目(7*3)

5.创建 Lily HBase Indexer 配置

[root@test119 index1]# cat morphline-hbase-mapper.xml 
<?xml version="1.0"?>
<!-- table:需要索引的HBase表名称-->
<!-- mapper:用来实现和读取指定的Morphline配置文件类,固定为MorphlineResultToSolrMapper-->
<indexer table="zh_ams_ns:zhongda_custom_task_cp" mapper="com.ngdata.hbaseindexer.morphline.MorphlineResultToSolrMapper" read-row="never" >
<!--param中的name参数用来指定当前配置为morphlineFile文件 -->
<!--value用来指定morphlines.conf文件的路径,绝对或者相对路径用来指定本地路径,如果是使用Cloudera Manager来管理morphlines.conf就直接写入值morphlines.conf"-->
    <param name="morphlineFile" value="morphlines.conf"/>
<!--value="ZDTableMap",这里test3Map是自定义,接下来要使用。其他的mapper,param name等属性默认即可-->
    <param name="morphlineId" value="ZDTableMap"/>
</indexer>

6.配置Morphline
通过CM页面进入到Key-Value Store Indexer的配置页面,里面有一个Morphlines文件。修改它:

修改完后重启Key-Value Store服务

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


morphlines : [
{
#与morphline-hbase-mapper中value相同
id : ZDTableMap
importCommands : ["org.kitesdk.**", "com.ngdata.**"]

commands : [                    
  {
    extractHBaseCells {
      mappings : [
        {
          inputColumn : "fn:name"
          outputField : "HBase_Indexer_ZDTable_fn_name" 
          type : string 
          source : value
        }
      ]
    }
  }


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

注:

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

7.注册 Lily HBase Indexer Configuration 和 Lily HBase Indexer Service

hbase-indexer add-indexer \
--name ZDindexer \
--indexer-conf /opt/hbase-indexer/index1/morphline-hbase-mapper.xml --connection-param solr.zk=test110:2181,test115:2181,test119:2181/solr \
--connection-param solr.collection=index1 \
--zookeeper test110:2181,test115:2181,test119:2181

运行hbase-indexer list-indexers查看添加成功,
此时新增数据已经可以通过solr查询
8.批量同步索引
在运行命令的目录下必须有morphlines.conf文件,执行

find / |grep morphlines.conf$

一般我们选择最新的那个process
cd 进入目录

hadoop --config /etc/hadoop/conf \
jar /opt/cloudera/parcels/CDH/lib/hbase-solr/tools/hbase-indexer-mr-1.5-cdh5.14.0-job.jar  \
--conf /etc/hbase/conf/hbase-site.xml \
--hbase-indexer-file /opt/hbase-indexer/index1/morphline-hbase-mapper.xml \
--zk-host test110:2181,test115:2181,test119:2181/solr \
--collection index1 \
--reducers 0 \
--go-live

猜你喜欢

转载自blog.csdn.net/zhangshenghang/article/details/82911084
今日推荐