solr批量生成索引踩坑

solr提供了一種批量生成索引的方式,各種文檔中都有提到。由于有這個需求,所以筆者開始了艱辛的踩坑過程。

Lucene版本問題

其實Lucene版本問題也是始發(fā)因素,之前使用hbase-indexer去批量創(chuàng)建索引,hbase-indexer使用的solr客戶端版本是solr-6.4.1,筆者的solr版本是solr-6.3.0,沒有任何問題。但是后來使用了HDP,而HDP自帶的solr版本solr-5.5.2,在進行索引合并操作時,出現(xiàn)了一個lucene版本問題:

18/02/11 16:38:06 ERROR mr.GoLive: Error sending live merge command
java.util.concurrent.ExecutionException: org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://10.1.236.66:8886/solr: Could not load codec 'Lucene62'.  Did you forget to add lucene-backward-codecs.jar?
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
    at com.ngdata.hbaseindexer.mr.GoLive.goLive(GoLive.java:130)
    at com.ngdata.hbaseindexer.mr.HBaseMapReduceIndexerTool.runIndexingPipeline(HBaseMapReduceIndexerTool.java:541)
    at com.ngdata.hbaseindexer.mr.HBaseMapReduceIndexerTool.run(HBaseMapReduceIndexerTool.java:241)
    at com.ngdata.hbaseindexer.mr.HBaseMapReduceIndexerTool.run(HBaseMapReduceIndexerTool.java:120)
    at com.ngdata.hbaseindexer.mr.HBaseMapReduceIndexerTool.run(HBaseMapReduceIndexerTool.java:110)
    at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:76)
    at com.ngdata.hbaseindexer.mr.HBaseMapReduceIndexerTool.main(HBaseMapReduceIndexerTool.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.hadoop.util.RunJar.run(RunJar.java:233)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:148)
Caused by: org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://10.1.236.66:8886/solr: Could not load codec 'Lucene62'.  Did you forget to add lucene-backward-codecs.jar?
    at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:593)
    at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:262)
    at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:251)
    at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:149)
    at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:166)
    at com.ngdata.hbaseindexer.mr.GoLive$1.call(GoLive.java:100)
    at com.ngdata.hbaseindexer.mr.GoLive$1.call(GoLive.java:89)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
18/02/11 16:38:06 INFO mr.GoLive: Live merging of index shards into Solr cluster took 0.894 secs

這個錯誤提示去查看在solr中是否有lucene-backward-codecs.jar,而solr中對應的jar包版本為:lucene-backward-codecs-5.5.2.jar;

然后猜想應該可以通過某個可以設置lucene版本,尋尋覓覓了很久后找到了一個唯一配置lucene版本的地方,solrconfig.xml文件中包含如下:

 <luceneMatchVersion>5.5.2</luceneMatchVersion>

但是,修改之后發(fā)現(xiàn)并沒有什么用處;進一步查看solr源碼,發(fā)現(xiàn)了原因:

  • solr在接收到合并索引的請求后解析lucene索引過程中發(fā)現(xiàn)版本是Lucene62,而其內(nèi)部查找對應版本的codec時找不到一致的版本
  • codec查找版本是通過訪查看SPI 實現(xiàn),而實現(xiàn)配置如下:
    luceneCodec配置

    由此可見solr并不支持多版本的索引合并,所以放棄了使用hbase-indexer作為統(tǒng)一的程序生成索引并向solr合并索引的方案。轉(zhuǎn)而通過自己開發(fā)程序讀取csv文件生成索引。

solr的mapreduce包不支持csv文件

solr自己提供了批量生成索引的類org.apache.solr.hadoop.MapReduceIndexerTool。但是筆者發(fā)現(xiàn)默認的Mapper類是org.apache.solr.hadoop.morphline.MorphlineMapper,它是用來解析單獨的文本文件的,明顯不滿足需求。并且直接傳遞mapper類給MapReduceIndexerTool的方法也行不通(需要傳遞各種參數(shù)),所以只能自定義了一個Mapper,并同時重新實現(xiàn)MapReduceIndexerTool.java。

MapReduceIndexerTool對solr的客戶端代碼有依賴

本來以為對于不同版本的solr,只需要改動pom.xml文件中對solr的依賴就可以解決codec不同版本的問題,但是MapReduceIndexerTool中使用的solrj版本的內(nèi)容不一致,所以不可避免的需要兩套程序來做兩個solr版本的批量索引生成。

找不到solr config文件夾

solr 批量創(chuàng)建索引的過程,是通過mapper把數(shù)據(jù)生成solr doc,而SolrReducer.java也只是把solr doc序列化,而真正的生成索引是在org.apache.solr.hadoop.SolrOutputFormat輸出文件的過程中,生成一個內(nèi)置的EmbeddedSolrServer建立索引,但是在生成EmbeddedSolrServer過程中,發(fā)現(xiàn)了solr config文件找不到。原因出在org.apache.solr.hadoop.SolrRecordWriter中:

  public static EmbeddedSolrServer createEmbeddedSolrServer(Path solrHomeDir, FileSystem fs, Path outputShardDir)
      throws IOException {

    ...
       SolrCore core = container.create("core1", ImmutableMap.of(CoreDescriptor.CORE_DATADIR, dataDirStr));
    ...
 
  }

這里的create方法會自動去dataDirStr/core1下尋找solr collection的配置文件,而去zookeeper拉取的配置文件是放在dataDirStr下的,所以無法找到。懷疑作者在測試過程中使用的是寫死的solrHomeDir,而這里存儲著對應的core1,因此修改代碼如下,問題解決。

  public static EmbeddedSolrServer createEmbeddedSolrServer(Path solrHomeDir, FileSystem fs, Path outputShardDir)
      throws IOException {

    ...
       SolrCore core = container.create("core1", Paths.get(solrHomeDir.toString()), ImmutableMap.of(CoreDescriptor.CORE_DATADIR, dataDirStr));
    ...
  }

??:以上是solr-5.5.2 版本的代碼,對于solr-6.3.0同樣的問題也存在,只不過報錯不一致

TreeMerge過程LockFactory問題

生成索引的過程中如果包含TreeMerge過程(第一次reduce時shard個數(shù)少于reduce個數(shù),需要經(jīng)過第二次的索引合并工作),會引發(fā)鎖競爭的問題??梢孕薷膐rg.apache.solr.hadoop.TreeMergeOutputFormat類中directoty生成方式來解決問題。

          Directory mergedIndex = new HdfsDirectory(workDir, NoLockFactory.INSTANCE, context.getConfiguration(), HdfsDirectory.DEFAULT_BUFFER_SIZE);
//        Directory mergedIndex = new HdfsDirectory(workDir, context.getConfiguration());
    

生成的索引未合并

如下圖所示,對同一份數(shù)據(jù)進行4次批量索引操作,最終在hdfs上顯示的索引文件如下,可以發(fā)現(xiàn)未進行合并索引操作。而進行檢索時會檢索出同樣ID的四條數(shù)據(jù)。
??因此,批量索引操作適合增量索引或者全量索引,但是不適合批量跟新索引。

drwxr-xr-x   - infra-solr hdfs          0 2018-01-31 10:49 /user/infra-solr/mrsolr/core_node5
drwxr-xr-x   - infra-solr hdfs          0 2018-01-31 10:49 /user/infra-solr/mrsolr/core_node5/data
drwxr-xr-x   - infra-solr hdfs          0 2018-02-03 21:29 /user/infra-solr/mrsolr/core_node5/data/index
-rwxr-xr-x   3 infra-solr hdfs        100 2018-02-03 21:15 /user/infra-solr/mrsolr/core_node5/data/index/_7.fdt
-rwxr-xr-x   3 infra-solr hdfs         83 2018-02-03 21:15 /user/infra-solr/mrsolr/core_node5/data/index/_7.fdx
-rwxr-xr-x   3 infra-solr hdfs        244 2018-02-03 21:15 /user/infra-solr/mrsolr/core_node5/data/index/_7.fnm
-rwxr-xr-x   3 infra-solr hdfs        489 2018-02-03 21:15 /user/infra-solr/mrsolr/core_node5/data/index/_7.si
-rwxr-xr-x   3 infra-solr hdfs        110 2018-02-03 21:15 /user/infra-solr/mrsolr/core_node5/data/index/_7_Lucene50_0.doc
-rwxr-xr-x   3 infra-solr hdfs        178 2018-02-03 21:15 /user/infra-solr/mrsolr/core_node5/data/index/_7_Lucene50_0.tim
-rwxr-xr-x   3 infra-solr hdfs        102 2018-02-03 21:15 /user/infra-solr/mrsolr/core_node5/data/index/_7_Lucene50_0.tip
-rwxr-xr-x   3 infra-solr hdfs         73 2018-02-03 21:15 /user/infra-solr/mrsolr/core_node5/data/index/_7_Lucene54_0.dvd
-rwxr-xr-x   3 infra-solr hdfs        118 2018-02-03 21:15 /user/infra-solr/mrsolr/core_node5/data/index/_7_Lucene54_0.dvm
-rwxr-xr-x   3 infra-solr hdfs        100 2018-02-03 21:20 /user/infra-solr/mrsolr/core_node5/data/index/_8.fdt
-rwxr-xr-x   3 infra-solr hdfs         83 2018-02-03 21:20 /user/infra-solr/mrsolr/core_node5/data/index/_8.fdx
-rwxr-xr-x   3 infra-solr hdfs        496 2018-02-03 21:20 /user/infra-solr/mrsolr/core_node5/data/index/_8.fnm
-rwxr-xr-x   3 infra-solr hdfs        489 2018-02-03 21:20 /user/infra-solr/mrsolr/core_node5/data/index/_8.si
-rwxr-xr-x   3 infra-solr hdfs        110 2018-02-03 21:20 /user/infra-solr/mrsolr/core_node5/data/index/_8_Lucene50_0.doc
-rwxr-xr-x   3 infra-solr hdfs        244 2018-02-03 21:20 /user/infra-solr/mrsolr/core_node5/data/index/_8_Lucene50_0.tim
-rwxr-xr-x   3 infra-solr hdfs        148 2018-02-03 21:20 /user/infra-solr/mrsolr/core_node5/data/index/_8_Lucene50_0.tip
-rwxr-xr-x   3 infra-solr hdfs         82 2018-02-03 21:20 /user/infra-solr/mrsolr/core_node5/data/index/_8_Lucene54_0.dvd
-rwxr-xr-x   3 infra-solr hdfs        179 2018-02-03 21:20 /user/infra-solr/mrsolr/core_node5/data/index/_8_Lucene54_0.dvm
-rwxr-xr-x   3 infra-solr hdfs        100 2018-02-03 21:24 /user/infra-solr/mrsolr/core_node5/data/index/_9.fdt
-rwxr-xr-x   3 infra-solr hdfs         83 2018-02-03 21:24 /user/infra-solr/mrsolr/core_node5/data/index/_9.fdx
-rwxr-xr-x   3 infra-solr hdfs        496 2018-02-03 21:24 /user/infra-solr/mrsolr/core_node5/data/index/_9.fnm
-rwxr-xr-x   3 infra-solr hdfs        489 2018-02-03 21:24 /user/infra-solr/mrsolr/core_node5/data/index/_9.si
-rwxr-xr-x   3 infra-solr hdfs        110 2018-02-03 21:24 /user/infra-solr/mrsolr/core_node5/data/index/_9_Lucene50_0.doc
-rwxr-xr-x   3 infra-solr hdfs        244 2018-02-03 21:24 /user/infra-solr/mrsolr/core_node5/data/index/_9_Lucene50_0.tim
-rwxr-xr-x   3 infra-solr hdfs        148 2018-02-03 21:24 /user/infra-solr/mrsolr/core_node5/data/index/_9_Lucene50_0.tip
-rwxr-xr-x   3 infra-solr hdfs         82 2018-02-03 21:24 /user/infra-solr/mrsolr/core_node5/data/index/_9_Lucene54_0.dvd
-rwxr-xr-x   3 infra-solr hdfs        179 2018-02-03 21:24 /user/infra-solr/mrsolr/core_node5/data/index/_9_Lucene54_0.dvm
-rwxr-xr-x   3 infra-solr hdfs        100 2018-02-03 21:29 /user/infra-solr/mrsolr/core_node5/data/index/_a.fdt
-rwxr-xr-x   3 infra-solr hdfs         83 2018-02-03 21:29 /user/infra-solr/mrsolr/core_node5/data/index/_a.fdx
-rwxr-xr-x   3 infra-solr hdfs        496 2018-02-03 21:29 /user/infra-solr/mrsolr/core_node5/data/index/_a.fnm
-rwxr-xr-x   3 infra-solr hdfs        489 2018-02-03 21:29 /user/infra-solr/mrsolr/core_node5/data/index/_a.si
-rwxr-xr-x   3 infra-solr hdfs        110 2018-02-03 21:29 /user/infra-solr/mrsolr/core_node5/data/index/_a_Lucene50_0.doc
-rwxr-xr-x   3 infra-solr hdfs        244 2018-02-03 21:29 /user/infra-solr/mrsolr/core_node5/data/index/_a_Lucene50_0.tim
-rwxr-xr-x   3 infra-solr hdfs        148 2018-02-03 21:29 /user/infra-solr/mrsolr/core_node5/data/index/_a_Lucene50_0.tip
-rwxr-xr-x   3 infra-solr hdfs         82 2018-02-03 21:29 /user/infra-solr/mrsolr/core_node5/data/index/_a_Lucene54_0.dvd
-rwxr-xr-x   3 infra-solr hdfs        179 2018-02-03 21:29 /user/infra-solr/mrsolr/core_node5/data/index/_a_Lucene54_0.dvm
-rwxr-xr-x   3 infra-solr hdfs        289 2018-02-03 21:24 /user/infra-solr/mrsolr/core_node5/data/index/segments_b
-rwxr-xr-x   3 infra-solr hdfs        351 2018-02-03 21:29 /user/infra-solr/mrsolr/core_node5/data/index/segments_c

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容