配置Solr7.3定时增量同步数据(定时与数据库同步数据)
solr定时与数据库同步数据
-
配置准备:
-
需要(solr-dataimport-scheduler.jar)适用于7.3的jar包(有需要的可以留下邮箱)
-
dataimport.properties配置文件
-
新建数据库字段updateTime,并设置根据时间戳更新
-
dataimport.properties配置文件内容如下(需要更改已标出)
################################################# # # # dataimport scheduler properties # # # ################################################# # to sync or not to sync # 1 - active; anything else - inactive # 这里的配置不用修改 syncEnabled=1 # which cores to schedule # in a multi-core environment you can decide which cores you want syncronized # leave empty or comment it out if using single-core deployment # 修改成你所使用的core,我的是goods syncCores=goods # solr server name or IP address # [defaults to localhost if empty] # 本地的话不用改变 server=localhost # solr server port # [defaults to 80 if empty] # 配置solr的tomca端口号,默认8080 port=8080 # application name/context # [defaults to current ServletContextListener's context (app) name] # 默认不改 webapp=solr # URL params [mandatory] # remainder of URL # 默认不用修改,solr同步数据库的链接 params=/dataimport?command=full-import&wt=json # schedule interval # number of minutes between two runs # [defaults to 30 if empty] # 这里是设置定时任务的,单位是分钟,也就是多长时间你检测一次数据同步,根据项目需求修改 # 开始测试的时候为了方便看到效果,时间可以设置短一点 interval=1 # 重做索引的时间间隔,单位分钟,默认7200,即5天; # 为空,为0,或者注释掉:表示永不重做索引 reBuildIndexInterval=7200 # 重做索引的参数 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-dataimport-scheduler.jar 放到…apache-tomcat-8.0.39\webapps\solr\WEB-INF\lib下
-
修改\apache-tomcat-8.0.39\webapps\solr\WEB-INF\web.xml文件
-
添加以下代码(粘贴ApplicationListener别有空格)
<listener> <listener-class> org.apache.solr.handler.dataimport.scheduler.ApplicationListener </listener-class> </listener>
-
-
在\apache-tomcat-8.0.39\solrhome\下创建conf目录(文件夹)
- 把dataimport.properties配置文件放到创建好的conf目录中
-
进入\apache-tomcat-8.0.39\solrhome\goods\conf文件(你的core文件的conf)
-
打开并修改solr-data-config.xml文件,内容如下(需要修改成自己的)
<?xml version="1.0" encoding="UTF-8" ?> <dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/myup" user="yhl" password="123456"/> <document> <entity name="t_goods" pk="go_id" query="SELECT * FROM t_goods" deltaImportQuery="SELECT * FROM t_goods where go_id = '${dih.delta.go_id}'" deltaQuery="SELECT go_id FROM t_goods where updateTime > '${dataimporter.last_index_time}'"> > <field column="go_id" name="go_id"/> <field column="go_name" name="go_name"/> <field column="updateTime" name="updateTime"/> </entity> </document> </dataConfig>
- 修改完保存退出并打开managed-schema文件
- 添加updateTime字段与其他需要查询的字段
-
-
配置完成,启动solr,等待一分钟看效果,如果一分钟后数据同步过来就OK了