基于Solr DIH实现MySQL表数据全量索引和增量索引
实现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
3
4
data-config.xml
5
6
上面这个是针对全量索引的,主要是配置data-config.xml文件,示例如下所示:
01
02
05
06
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
上面主要是通过内置变量 “${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范围为的记录进行索引,因为数据表可能达到千万记录数,而且线上有业务在操作数据库,所以要选择分批进行索引。
增量索引
solrconfig.xml配置如下:
01
02
03
delta-data-config.xml
04
05
06
上面定义请求的接口为“deltaimport”,对应的配置文件delta- data-config.xml内容如下所示:
07
08
11
12
16
transformer="RegexTransformer">
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
上面主要是通过内置变量“${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
页:
[1]