Key-Value Store Indexer(Lily HBase Indexer) 小型采坑
环境:
Cloudera Express 5.12.1
JDK 1.8.0_92
CentOS 7
步骤1:数据导入到Hbase中(非正题,跳过)
hbase中表为allDoc,两个Family:fulltext,fileInfo
fulltext中就一列:fulltext
fileInfo中有如下几列serialNumber,verdictType,hashCode,fileName
步骤2:生成实体配置文件(我这里用的root账户)
solrctl instancedir --create /root/config/
步骤3:配置/root/config/conf/schema.xml,增加分词器
<fields></fields>下增加字段,其中id为hbase的row_key,_version_字段必须有,否则报错,我这里只加了fulltext和serialnumber,这里的是solr中的schema
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="serialnumber" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="fulltext" type="text_ch" indexed="true" stored="true" required="true" multiValued="false" /> <field name="_version_" type="long" indexed="true" stored="true"/>
增加中文分词器,下载对应版本的分词器:
下载地址:https://repository.cloudera.com/artifactory/cdh-releases-rcs/org/apache/lucene/lucene-analyzers-smartcn/
我用的是:https://repository.cloudera.com/artifactory/cdh-releases-rcs/org/apache/lucene/lucene-analyzers-smartcn/4.10.3-cdh5.12.1/lucene-analyzers-smartcn-4.10.3-cdh5.12.1.jar
下载之后记得放到目录下/opt/cloudera/parcels/CDH-5.12.1-1.cdh5.12.1.p0.3/lib/solr/webapps/solr/WEB-INF/lib/lucene-analyzers-smartcn-4.10.3-cdh5.12.1.jar
然后schema.xml加入text_ch
<fieldType name="text_ch" class="solr.TextField" positionIncrementGap="100"> <analyzer type="index"> <tokenizer class="solr.SmartChineseSentenceTokenizerFactory"/> <filter class="solr.SmartChineseWordTokenFilterFactory"/> </analyzer> </fieldType>
步骤4:根据实体配置文件在Solr中建立collection
solrctl instancedir --create hbase-collection /root/config/ solrctl collection --create hbase-collection
步骤5:创建 Lily HBase Indexer 配置,下面两个文件我都放到了root文件夹下
新建一个XML文件morphline-hbase-mapper.xml,内容如下:(其中allDoc是hbase中的表名称)
<?xml version="1.0"?> <indexer table="allDoc" mapper="com.ngdata.hbaseindexer.morphline.MorphlineResultToSolrMapper"> <param name="morphlineFile" value="morphlines.conf"/> </indexer>
创建morphlines.conf,内容如下:(这里是hbase到solr的映射)
morphlines : [ { id : morphline importCommands : [ "org.kitesdk.**", "com.ngdata.**" ] commands : [ { extractHBaseCells { mappings : [ { inputColumn : "fulltext:fulltext" outputField : "fulltext" type : string source : value }, { inputColumn : "fileInfo:serialNumber" outputField : "serialnumber" type : string source : value } ] } } { logDebug { format : "output record: {}", args : [ "@{}" ] } } ] } ]
步骤6: 注册 Lily HBase Indexer配置
hbase-indexer delete-indexer -n docIndex hbase-indexer add-indexer -n docIndex -c /root/morphline-hbase-mapper.xml -cp solr.zk=gs-server-5002:2181,gs-server-5001:2181,gs-server-5000:2181/solr -cp solr.collection=hbase-collection8 -z gs-server-5002:2181,gs-server-5001:2181,gs-server-5000:2181 hbase-indexer list-indexers #查看是否正常运行
步骤7: 批量导入:
Shell脚本:(lucene-analyzers-smartcn-4.10.3-cdh5.12.1.jar 在root目录中也有一份,这个脚本也放到了root目录中)
COLLECTION='hbase-collection' ZK='gs-server-5002:2181,gs-server-5001:2181,gs-server-5000:2181' echo 'Delete previous docs...' solrctl collection --deletedocs $COLLECTION echo 'Lily HBase MapReduce indexing...' config="/etc/hadoop/conf.cloudera.yarn" parcel="/opt/cloudera/parcels/CDH" jar="$parcel/lib/hbase-solr/tools/hbase-indexer-mr-*-job.jar" hbase_conf="/etc/hbase/conf/hbase-site.xml" opts="'mapred.child.java.opts=-Xmx1024m'" log4j="$parcel/share/doc/search*/examples/solr-nrt/log4j.properties" zk="$ZK/solr" libjars="lucene-analyzers-smartcn-4.10.3-cdh5.12.1.jar" export HADOOP_OPTS="-Djava.security.auth.login.config=conf/jaas.conf" hadoop --config $config jar $jar --conf $hbase_conf --libjars $libjars -D $opts --log4j $log4j --hbase-indexer-file morphline-hbase-mapper.xml --verbose --go-live --zk-host $zk --collection $COLLECTION
ZK为Zookeeper集群地址,修改COLLECTION为之前生成的,生效基本都保持不变morphline-hbase-mapper.xml是步骤5配置生成的xml文件
总体来说不算太难,但是有很多坑。。
因为批量导入采用的是MapReduce,经常出现OOM,或者exit Code 154,Map 阶段154这个忽略了,因为重试之后几乎都可以通过(调整容器内存可以直接避免这个问题),在调整Map Reduce的内存过程中花了好多时间,另外还有就是编码问题,出现SolrServerException: org.apache.solr.client.solrj.SolrServerException: java.lang.RuntimeException: [was class java.io.CharConversionException] Invalid UTF-8 character 0xffff at char #18928, byte #47990)
这个问题暂时未解决,查阅资料说可以用如下方法解决,正在尝试(数据导入估计得2-3天,慢慢等等看了):
public static String stripNonCharCodepoints(String input) { StringBuilder retval = new StringBuilder(); char ch; for (int i = 0; i < input.length(); i++) { ch = input.charAt(i); // Strip all non-characters // http://unicode.org/cldr/utility/list-unicodeset.jsp?a=[:Noncharacter_Code_Point=True:] // and non-printable control characters except tabulator, new line and // carriage return if (ch % 0x10000 != 0xffff && // 0xffff - 0x10ffff range step 0x10000 ch % 0x10000 != 0xfffe && // 0xfffe - 0x10fffe range (ch <= 0xfdd0 || ch >= 0xfdef) && // 0xfdd0 - 0xfdef (ch > 0x1F || ch == 0x9 || ch == 0xa || ch == 0xd)) { retval.append(ch); } } return retval.toString(); }
资料参考:
http://www.aboutyun.com/thread-24447-1-1.html
http://fbinbin.iteye.com/blog/2357294
https://blog.csdn.net/d6619309/article/details/51304135?locationNum=1&fps=1
https://github.com/apache/nutch/blob/master/src/plugin/indexer-solr/src/java/org/apache/nutch/indexwriter/solr/SolrUtils.java#L76
https://blog.csdn.net/vegetable_bird_001/article/details/53420131