本帖最后由 pig2 于 2014-5-15 12:24 编辑
问题导读:
1、如何配置Solr 的索引?
2.Solr DIH组件的实现类是什么?
3.全量索引如何配置?
4.增量索引如何配置?
实现MySQL表数据全量索引和增量索引,基于Solr DIH组件实现起来比较简单,只需要重复使用Solr的DIH(Data Import Handler)组件,对data-config.xml进行简单的修改即可。Solr DIH组件的实现类为org.apache.solr.handler.dataimport.DataImportHandler,在Solr的solrconfig.xml中配置两个handler,配置分别说明如下。
全量索引
solrconfig.xml配置如下:
- <requestHandler name="/dataimport"
- class="org.apache.solr.handler.dataimport.DataImportHandler">
- <lst name="defaults">
- <str name="config">data-config.xml</str>
- </lst>
- </requestHandler>
复制代码
上面这个是针对全量索引的,主要是配置data-config.xml文件,示例如下所示:
- <dataConfig>
- <dataSource name="jdbc" driver="com.mysql.jdbc.Driver"
- url="jdbc:mysql://172.0.8.249:5606/marketing_db_saved?zeroDateTimeBehavior=convertToNull"
- user="developer" password="sedept@shiyanjun.cn"/>
- <document name="mkt_data">
- <entity name="marketing_data" pk="id"
- query="select * from marketing_data limit ${dataimporter.request.length} offset ${dataimporter.request.offset}"
- transformer="RegexTransformer">
- <field column="id" name="id" />
- <field column="domain" name="domain" />
- <field column="alex_rank" name="alex_rank" />
- <field column="server_port" name="server_port" />
- <field column="cert_validity_notBefore" name="cert_validity_notBefore" />
- <field column="cert_validity_notAfter" />
- <field column="cert_validity_notAfter_yyyyMMdd" regex="(.*?)\s+.*" name="cert_validity_notAfter_yyyyMMdd" sourceColName="cert_validity_notAfter"/>
- <field column="cert_issuer_brand" name="cert_issuer_brand" />
- <field column="cert_validation" name="cert_validation" />
- <field column="cert_isMultiDomain" name="cert_isMultiDomain" />
- <field column="cert_issuer_brand_isXRelated" name="cert_issuer_brand_isXRelated" />
- <field column="cert_isWildcard" name="cert_isWildcard" />
- <field column="cert_notAfter" name="cert_notAfter" />
- <field column="special_ssl" name="special_ssl" />
- <field column="competitor_logo" name="competitor_logo" />
- <field column="segment" name="segment" />
- </entity>
- </document>
- </dataConfig>
复制代码
上面主要是通过内置变量 “${dataimporter.request.length}”和 “${dataimporter.request.offset}”来设置一个批次索引的数据表记录数,请求的URL示例如下:- http://172.0.8.212:8080/search-server/core0/dataimport?command=full-import&commit=true&clean=false&offset=1000000&length=100000
复制代码
上面表示,对数据表中id范围为[10000000, 1100000]的记录进行索引,因为数据表可能达到千万记录数,而且线上有业务在操作数据库,所以要选择分批进行索引。
增量索引
solrconfig.xml配置如下:
- <requestHandler name="/deltaimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
- <lst name="defaults">
- <str name="config">delta-data-config.xml</str>
- </lst>
- </requestHandler>
- 上面定义请求的接口为“deltaimport”,对应的配置文件delta- data-config.xml内容如下所示:
- <dataConfig>
- <dataSource name="jdbc" driver="com.mysql.jdbc.Driver"
- url="jdbc:mysql://172.0.8.249:5606/marketing_db_saved?zeroDateTimeBehavior=convertToNull"
- user="developer" password="sedept@shiyanjun.cn"/>
- <document name="mkt_data">
- <entity name="marketing_data" pk="id"
- query="select * from marketing_data"
- deltaImportQuery="select * from marketing_data where id='${dih.delta.id}'"
- deltaQuery="select id from marketing_data where updated_at > '${dih.last_index_time}'"
- transformer="RegexTransformer">
- <field column="id" name="id" />
- <field column="domain" name="domain" />
- <field column="alex_rank" name="alex_rank" />
- <field column="server_port" name="server_port" />
- <field column="cert_validity_notBefore" name="cert_validity_notBefore" />
- <field column="cert_validity_notAfter" />
- <field column="cert_validity_notAfter_yyyyMMdd" regex="(.*?)\s+.*" name="cert_validity_notAfter_yyyyMMdd" sourceColName="cert_validity_notAfter"/>
- <field column="cert_issuer_brand" name="cert_issuer_brand" />
- <field column="cert_validation" name="cert_validation" />
- <field column="cert_isMultiDomain" name="cert_isMultiDomain" />
- <field column="cert_issuer_brand_isXRelated" name="cert_issuer_brand_isXRelated" />
- <field column="cert_isWildcard" name="cert_isWildcard" />
- <field column="cert_notAfter" name="cert_notAfter" />
- <field column="special_ssl" name="special_ssl" />
- <field column="competitor_logo" name="competitor_logo" />
- <field column="segment" name="segment" />
- </entity>
- </document>
- </dataConfig>
复制代码
上面主要是通过内置变量“${dih.delta.id}”和 “${dih.last_index_time}”来记录本次索引的id和最后索引时间。这里,会保存在deltaimport.properties文件中,示例如下:
- #Sat Feb 16 17:48:49 CST 2013
- last_index_time=2013-02-16 17\:48\:48
- marketing_data.last_index_time=2013-02-16 17\:48\:48
复制代码
请求增量索引的接口为“deltaimport”,可以写一个定时脚本,例如每隔一天执行一次 (一天索引一次,只索引数据表中更新的记录),请求URL示例如下:
- 172.0.8.212:8080/search-server/core0/dataimport?command=delta-import
复制代码
有关“query”,“deltaImportQuery”, “deltaQuery”的解释,引用官网说明,如下所示: The query gives the data needed to populate fields of the Solr document in full-import The deltaImportQuery gives the data needed to populate fields when running a delta-import The deltaQuery gives the primary keys of the current entity which have changes since the last index time 还有更灵活的索引方式,未完待续。
|