Solr7.3.1定時任務,增量索引
一.修改數據庫配置文件
1.修改配置文件指定路徑:/solrhome/core/solrconfig.xml
修改內容
<requestHandler name="/dataimport"
class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">delta-data-config.xml</str>
</lst>
</requestHandler>
2.創建配置文件:/solrhome/core/ delta-data-config.xml
添加內容:
<dataConfig>
<dataSource
type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb"
user="root"
password="root"/>
<document>
<entity name="info" pk="id"
query="select id,infodesc,sort,name,user,last_update_time from info"
deltaImportQuery="select id,infodesc,sort,name,user,last_update_time from info where id='${dih.delta.id}'"
deltaQuery="select id from info where last_update_time > '${dih.last_index_time}'"
transformer="RegexTransformer">
<field column="id" name="id"/>
<field column="infodesc" name="infodesc"/>
<field column="sort" name="sort"/>
<field column="name" name="name"/>
<field column="user" name="user"/>
<field column="last_update_time" name="last_update_time"/>
</entity>
</document>
</dataConfig>
3.定時任務指定字段
deltaQuery="select id from info where last_update_time > '${dih.last_index_time}'"
last_index_time字段為指定定時更新條件字段
4. 創建sql依賴配置文件
/solrhome/core/ conf/ dataimport.properties
二.創建定時任務配置文件
1.創建配置文件:solrhome/conf/dataimport.properties
2.添加內容:
#################################################
# #
# 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
syncCores=mysb
# solr server name or IP address
# [defaults to localhost if empty]
server=localhost
# solr server port
# [defaults to 80 if empty]
port=80
# application name/context
# [defaults to current ServletContextListener's context (app) name]
webapp=solr
# URL params [mandatory]
# remainder of URL
#增量 增量請求路徑需與頁面的請求配置相同
params=/dataimport?command=delta-import&clean=false&commit=true&optimize=false&wt=json&indent=true&entity=info&verbose=false&debug=false&name=dataimport
# schedule interval
# number of minutes between two runs
# [defaults to 30 if empty] 分鍾 默認30分鍾
interval=1
# 重做索引的時間間隔,單位分鍾,默認7200,即1天;
# 為空,為0,或者注釋掉:表示永不重做索引
reBuildIndexInterval=7200
# 重做索引的參數
reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true&optimize=true&wt=json&indent=true&entity=info&verbose=false&debug=false
# 重做索引時間間隔的計時開始時間,第一次真正執行的時間=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;
# 兩種格式:2012-04-11 03:10:00 或者 03:10:00,后一種會自動補全日期部分為服務啟動時的日期
reBuildIndexBeginTime=2012-04-11 09:00:00
*3.注意:和一中3的sql配置文件路徑不通
三.添加定時任務jar包
1.下載並添加jar包
下載solr-data-import-scheduler-1.1.2.jar或者apache-solr-dataimportscheduler-1.1.jar
Jar包添加路徑webapps/solr/WEB-INF/lib
我用的是:solr-data-import-scheduler-1.1.2.jar
2.添加任務監聽:webapps/solr/WEB-INF/web.xml
<listener>
<listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class>
</listener>
3. 定時任務配置:同二中內容
將solr-data-import-scheduler-1.1.2.jar 中 dataimport.properties 取出並根據實際情況修改,然后放到 solr_home/conf (不是solrhome/core/conf) 目錄下面,solrhome/conf文件夾沒有的話手動創建
四.啟動
1.報錯-版本錯誤
這是因為solr-data-import-scheduler-1.1.2.jar中的方法:
org.apache.solr.handler.dataimport.scheduler. SolrDataImportProperties
報錯;
修改錯誤內容:
SolrResourceLoader loader = new SolrResourceLoader();
logger.info("Instance dir = " + loader.getConfigDir());
String configDir = loader.getConfigDir();
configDir = SolrResourceLoader.normalizeDir(configDir);
2.錯誤-sql錯誤1
字段錯誤:kdkdkdk
java.lang.RuntimeException: org.apache.solr.handler.dataimport.DataImportHandlerException: Unable to execute query: select id,kdkdkdk from info where last_update_time > '' Processing Document # 1
3.錯誤-sql錯誤2
條件錯誤:dih
deltaImportQuery="select id,infodesc,sort,name,user,last_update_time from info where id='${dih.delta.id}'"
deltaQuery="select id from info where last_update_time > '${dih.last_index_time}'"
兩個sql中的dih對應/solrhome/core/conf/dataimport.properties中:
archives_bi.last_index_time=2018-06-25 10\:45\:05
dih不用修改為info,會自動對應,假如修改為info,則deltaQuery會查詢條件last_update_time > '${dih.last_index_time}'會變為last_update_time > '',導致定時更新不成功,同理deltaImportQuery也是如此。
4.錯誤-連接錯誤
數據庫jar包要和數據庫版本保持一致
5.錯誤-未知錯誤
Exception while processing: test_table document : SolrInputDocument(fields: []):org.apache.solr.handler.dataimport.DataImportHandlerException: Unable to execute query: select * from test_table limit offset Processing Document # 1
at org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow(DataImportHandlerException.java:71)
at org.apache.solr.handler.dataimport.JdbcDataSource$ResultSetIterator.<init>(JdbcDataSource.java:283)
Exception while processing: test_table document : SolrInputDocument(fields: []):org.apache.solr.handler.dataimport.DataImportHandlerException: Unable to execute query: select * from test_table limit offset Processing Document # 1
at org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow(DataImportHandlerException.java:71)
at org.apache.solr.handler.dataimport.JdbcDataSource$ResultSetIterator.<init>(JdbcDataSource.java:283)
***全量索引異常:data-config.xml中配置了批次索引,但創建索引的時候參數offset&length出現問題,我也未調通,出現此問題在apache-solr-dataimportscheduler-1.1.jar中,未在solr-data-import-scheduler-1.1.2.jar中出現此問題***
6.錯誤-其他
還有其他一些問題,例如配置文件找不到等,就不說了
五.注意
此文件只介紹了,如何添加定時任務的增量索引內容,未介紹如何配置solr服務等內容。此服務使用的是繼承Tomcat服務。
jar包下載:https://pan.baidu.com/s/1whxYyI6nGzHvEtsTCZH4pw