Apache Solr學習 第七篇 實現solr定時數據同步

1、準備環境

  已安裝配置好solr和mysql數據。java

    jar包:solr-dataimportscheduler-1.1.1.jar。mysql

    原理:看了下jar的大概邏輯,就是在solr啓動的時候,啓動一個定時任務,以http方式訪問solr 的dataimport接口。web

2、配置

    1.data-config.xml配置sql

<?xml version="1.0" encoding="UTF-8"?>
  <dataConfig>
   <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/moxiangbooks" user="root" password="www1928..com" batchSize="-1"/>
     <document name="books">
        <entity name="books" pk="bid" query="SELECT A.bid, A.bname, A.author, A.currPrice, A.discount, A.press, A.publishtime, A.edition, A.pageNum, A.wordNum, A.printtime, A.booksize, A.paper, A.cid, A.image_w as imageW, A.image_b as imageB, A.orderBy, price FROM books A" deltaImportQuery="SELECT A.bid, A.bname, A.author, A.currPrice, A.discount, A.press, A.publishtime, A.edition, A.pageNum, A.wordNum, A.printtime, A.booksize, A.paper, A.cid, A.image_w as imageW, A.image_b as imageB, A.orderBy, price FROM books A WHERE A.bid = '${dih.delta.bid}'" deltaQuery="select bid from books" >
            <field column="bid" name="bid" />
            <field column="bname" name="bname" />
            <field column="author" name="author" />
            <field column="price" name="price" />
            <field column="currPrice" name="currPrice" />
            <field column="discount" name="discount" />
            <field column="press" name="press" />
            <field column="publishtime" name="publishtime" />
            <field column="edition" name="edition" />
            <field column="pageNum" name="pageNum" />
            <field column="wordNum" name="wordNum" />
            <field column="printtime" name="printtime" />
            <field column="booksize" name="booksize" />
            <field column="paper" name="paper" />
            <field column="cid" name="cid" />
            <field column="imageW" name="imageW" />
            <field column="imageB" name="imageB" />
            <field column="orderBy" name="orderBy" />
        </entity>
    </document>
配置參數說明:
  • driver:數據庫驅動
  • url:數據庫訪問地址
  • query:全量更新SQL
  • deltaQuery: 增量更新SQL
  • ${dih.delta.bid}表的主鍵
2.dataimport.properties配置

      有的solr-dataimportscheduler-1.1.1.jar自帶該文件,有的沒有,若是是Tomcat啓動solr,在/solrhome下新建文件夾conf,並在conf下新建dataimport.properties,數據庫

若是是用solr自帶的jetty啓動,則在solr-6.6.6\server\solr目錄下新建conf,注意,在每個core對應的conf文件夾也有dataimport.properties,可是和咱們新建不是一個apache

文件,dataimport.properties內容以下:json

 ################################################# # # # dataimport scheduler properties # # # ################################################# # server BASIC authorization by userName and password # format:userName:password # if no server BASIC authorization,please set: # authorizationMsg= #authorizationMsg=userName:password # to sync or not to sync # 同步執行更新 syncEnabled=1 # # solr中對應得core名,可寫多個,多個時用逗號「,」分隔 # syncCores=books # solr server name or IP address # [defaults to localhost if empty] server=localhost # solr服務的端口號 # [defaults to 80 if empty] port=8983 # application name/context # [defaults to current ServletContextListener's context (app) name] webapp=solr # URL params [mandatory] # remainder of URL #增量更新對應的訪問參數,注意/dataimport?地址不一樣版本sorl了能地址名不一樣,具體可登陸solr管理後臺##查看dataimport的具體訪問ULR params=/dataimport?command=delta-import&clean=false&commit=true&optimize=false&wt=json&indent=true&verbose=false&debug=false # schedule interval # number of minutes between two runs # 定時任務執行增量更新的間隔,不能爲0.5這樣的數,默認設置爲1分鐘 interval=1 # 重作索引的時間間隔,單位分鐘,默認7200,即5天; # 爲空,爲0,或者註釋掉:表示永不重作索引,即全量更新的時間間隔 reBuildIndexInterval=7200 # 重作索引的參數,即全量更新 reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true&optimize=true&wt=json&indent=true&verbose=false&debug=false # 重作索引時間間隔的計時開始時間,第一次真正執行的時間=reBuildIndexBeginTime+reBuildIndexInterval*60*1000; # 兩種格式:2012-04-11 03:10:00 或者 03:10:00,後一種會自動補全日期部分爲服務啓動時的日期 reBuildIndexBeginTime=03:10:00
3.全量更新和增量更新
  • 全量更新:即刪除原來core下的全部索引數據,並從數據庫中從新導入
  • 增量更新:根據/core/conf/dataimport.properties記錄的時間,將查到的數據作對比,只更新有改變的索引會更新
  4.將solr-dataimportscheduler-1.1.1.jar放入到WEB-INF下的lib目錄中
  5.在WEB-INF/web.xml中配置監聽器
<listener> 
  <listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class>
</listener>

配置完成啓動項目,一分鐘之後就能夠去查看數據更新了app

3、遇到的問題

涉及問題,啓動時候報了webapp

ERROR: Solr at http://localhost:8983/solr did not come online within 30 seconds!

查看solr啓動日誌,發現測試

java.lang.NoSuchMethodError: org.apache.solr.core.SolrResourceLoader.getInstanceDir()Ljava/lang/String;
    at org.apache.solr.handler.dataimport.scheduler.SolrDataImportProperties.loadProperties(SolrDataImportProperties.java:37)
    at org.apache.solr.handler.dataimport.scheduler.BaseTimerTask.reloadParams(BaseTimerTask.java:57)
    at org.apache.solr.handler.dataimport.scheduler.BaseTimerTask.<init>(BaseTimerTask.java:39)

      像這種報錯通常都是jar包不兼容引發的,打開了jar看了看,發現有solr-core的有個類的構造方法發生了改變,因此反編譯修改了solr-dataimportscheduler-1.1.1.jar,

修改後已測試好使, 修改後的jar地址。

      連接:https://pan.baidu.com/s/15K4ZKIDjVQkgR5IoCDySpg

      提取碼:vf3u

solr涉及資源已所有分享網盤,喜歡的給文章點個贊,謝謝你們了
相關文章
相關標籤/搜索