实现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配置如下:
1
<requestHandler name="/dataimport"
2
class="org.apache.solr.handler.dataimport.DataImportHandler">
3
<lst name="defaults">
4
<str name="config">data-config.xml</str>
5
</lst>
6
</requestHandler>
上面这个是针对全量索引的,主要是配置data-config.xml文件,示例如下所示:
01
<dataConfig>
02
<dataSource name="jdbc" driver="com.mysql.jdbc.Driver"
03
url="jdbc:mysql://172.0.8.249:5606/marketing_db_saved?zeroDateTimeBehavior=convertToNull"
04
user="developer" password="[email protected]"/>
05
<document name="mkt_data">
06
<entity name="marketing_data" pk="id"
07
query="select * from marketing_data limit ${dataimporter.request.length} offset ${dataimporter.request.offset}"
08
transformer="RegexTransformer">
09
<field column="id" name="id" />
10
<field column="domain" name="domain" />
11
<field column="alex_rank" name="alex_rank" />
12
<field column="server_port" name="server_port" />
13
<field column="cert_validity_notBefore" name="cert_validity_notBefore" />
14
<field column="cert_validity_notAfter" />
15
<field column="cert_validity_notAfter_yyyyMMdd" regex="(.*?)\s+.*" name="cert_validity_notAfter_yyyyMMdd" sourceColName="cert_validity_notAfter"/>
16
<field column="cert_issuer_brand" name="cert_issuer_brand" />
17
<field column="cert_validation" name="cert_validation" />
18
<field column="cert_isMultiDomain" name="cert_isMultiDomain" />
19
<field column="cert_issuer_brand_isXRelated" name="cert_issuer_brand_isXRelated" />
20
<field column="cert_isWildcard" name="cert_isWildcard" />
21
<field column="cert_notAfter" name="cert_notAfter" />
22
<field column="special_ssl" name="special_ssl" />
23
<field column="competitor_logo" name="competitor_logo" />
24
<field column="segment" name="segment" />
25
</entity>
26
</document>
27
</dataConfig>
上面主要是通过内置变量 “${dataimporter.request.length}”和 “${dataimporter.request.offset}”来设置一个批次索引的数据表记录数,请求的URL示例如下:
1
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配置如下:
01
<requestHandler name="/deltaimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
02
<lst name="defaults">
03
<str name="config">delta-data-config.xml</str>
04
</lst>
05
</requestHandler>
06
上面定义请求的接口为“deltaimport”,对应的配置文件delta- data-config.xml内容如下所示:
07
<dataConfig>
08
<dataSource name="jdbc" driver="com.mysql.jdbc.Driver"
09
url="jdbc:mysql://172.0.8.249:5606/marketing_db_saved?zeroDateTimeBehavior=convertToNull"
10
user="developer" password="[email protected]"/>
11
<document name="mkt_data">
12
<entity name="marketing_data" pk="id"
13
query="select * from marketing_data"
14
deltaImportQuery="select * from marketing_data where id='${dih.delta.id}'"
15
deltaQuery="select id from marketing_data where updated_at > '${dih.last_index_time}'"
16
transformer="RegexTransformer">
17
<field column="id" name="id" />
18
<field column="domain" name="domain" />
19
<field column="alex_rank" name="alex_rank" />
20
<field column="server_port" name="server_port" />
21
<field column="cert_validity_notBefore" name="cert_validity_notBefore" />
22
<field column="cert_validity_notAfter" />
23
<field column="cert_validity_notAfter_yyyyMMdd" regex="(.*?)\s+.*" name="cert_validity_notAfter_yyyyMMdd" sourceColName="cert_validity_notAfter"/>
24
<field column="cert_issuer_brand" name="cert_issuer_brand" />
25
<field column="cert_validation" name="cert_validation" />
26
<field column="cert_isMultiDomain" name="cert_isMultiDomain" />
27
<field column="cert_issuer_brand_isXRelated" name="cert_issuer_brand_isXRelated" />
28
<field column="cert_isWildcard" name="cert_isWildcard" />
29
<field column="cert_notAfter" name="cert_notAfter" />
30
<field column="special_ssl" name="special_ssl" />
31
<field column="competitor_logo" name="competitor_logo" />
32
<field column="segment" name="segment" />
33
</entity>
34
</document>
35
</dataConfig>
上面主要是通过内置变量“${dih.delta.id}”和 “${dih.last_index_time}”来记录本次索引的id和最后索引时间。这里,会保存在deltaimport.properties文件中,示例如下:
1
#Sat Feb 16 17:48:49 CST 2013
2
last_index_time=2013-02-16 17\:48\:48
3
marketing_data.last_index_time=2013-02-16 17\:48\:48
请求增量索引的接口为“deltaimport”,可以写一个定时脚本,例如每隔一天执行一次 (一天索引一次,只索引数据表中更新的记录),请求URL示例如下:
1
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
还有更灵活的索引方式,以后再讲。
参考链接
http://wiki.apache.org/solr/DataImportHandler#Using_delta-import_command
http://www.myexception.cn/other/669522.html
转载:http://shiyanjun.cn/archives/444.html