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了