(二)solr定时任务配置,定时更新数据库增量到solr索引库
继续上篇手动将数据库中的数据导入到了solr中之后,(→传送←)
数据库数据有更改我们不可能及时去同步,所以需要定时检测数据库,将更改、新增的数据同步到solr中。
数据库请查看上一篇的表结构
当数据每次改动或者增加时都会根据当前时间戳更新,在进行同步的时候只需要查找找到 updata_time > last_index_time 的数据进行同步即可,不用同步所有数据
下载Jar包
需要下载一个 jar包,本来打算赚点积分的,算了免费送了
百度云:https://pan.baidu.com/s/1boeOhwciXugyeWiyB4xrVw 密码:acbu
修改配置
将下载好的 apache-solr-dataimportscheduler.jar 放到 ..\apache-solr\apache-tomcat-8.5.34\webapps\solr\WEB-INF\lib\ 下
修改tomcat的 ../webapps/solr/WEB-INF/web.xml, 在servlet节点前面增加监听配置
<listener>
<listener-class>
org.apache.solr.handler.dataimport.scheduler.ApplicationListener
</listener-class>
</listener>
data-config.xml 和上一篇博客是一样的
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/contrade" user="root" password="root" />
<document name="article">
<entity name="article" pk="id"
query="select id,title,author,type from article"
deltaImportQuery="select * from article where id ='${dih.delta.id}'"
deltaQuery="select id from article where update_time > '${dataimporter.last_index_time}'">
<!-- query:查询数据库表符合记录数据 --->
<!-- deltaQuery:增量索引查询主键ID --->
<!-- deltaImportQuery:增量索引查询导入数据 --->
<field name="id" column="id"/>
<field name="title" column="title"/>
<field name="author" column="author"/>
<field name="type" column="type"/>
<field name="update_time" column="update_time"/>
</entity>
</document>
</dataConfig>
修改 \apache-solr\solr_home\solr_core\conf\dataimport.properties
#################################################
# #
# dataimport scheduler properties #
# 数据端口调度程序 #
# #
#################################################
# tosync or not to sync
# 1- active; anything else - inactive
syncEnabled=1
# which cores to schedule
# ina multi-core environment you can decide which cores you want syncronized
# leave empty or comment it out if using single-core deployment
# 修改成你所使用的core,我这里是我自定义的core:solr_core
syncCores=solr_core
# solr server name or IP address
# [defaults to localhost if empty]
#这个一般都是localhost不会变
server=localhost
# solr server port
# [defaults to 80 if empty]
# 安装solr的tomcat端口,如果你使用的是默认的端口,就不用改了,否则改成自己的端口就好了
port=8080
# application name/context
# [defaults to current ServletContextListener's context (app) name]
# 这里默认不改(\tomcat\webapp\ 下的solr目录名)
webapp=solr
# URL params [mandatory]
# remainder of URL
# 这里改成下面的形式,solr同步数据时请求的链接
params=/dataimport?command=delta-import&clean=false&commit=true
# schedule interval
# number of minutes between two runs
# [defaults to 30 if empty]
#这里是设置定时任务的,单位是分钟,也就是多长时间你检测一次数据同步,根据项目需求修改
# 开始测试的时候为了方便看到效果,时间可以设置短一点
interval=1
# 重做索引的时间间隔,单位分钟,默认7200,即5天;
# 为空,为0,或者注释掉:表示永不重做索引
reBuildIndexInterval=1
# 重做索引的参数
reBuildIndexParams=/select?qt=/dataimport&command=full-import&clean=true&commit=true
# 重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;
# 两种格式:2012-04-11 03:10:00 或者 03:10:00,后一种会自动补全日期部分为服务启动时的日期
reBuildIndexBeginTime=03:10:00
温馨提示:启动时可能会报错 \solr_home\conf\dataimport.properties 找不到,在solr_core同级位置新建一个\conf\dataimport.properties 就可以了。
重启Tomcat
测试
在数据库手动更改
等待一段时间 执行 Execute Query,就可看到修改的数据已经同步到Solr中