問題一:為什么要導入數(shù)據(jù)到solr
因為solr所謂的索引可不是基于數(shù)據(jù)庫的索引,而將數(shù)據(jù)庫的數(shù)據(jù)導入到solr中破婆,也就是core/data
文件下涮总,并根據(jù)配置信息生成索引等滚停。有點類似于redis河劝。
問題二:怎么導入數(shù)據(jù)到solr
首先點擊core/的DataImport選項:
這里提示要在solrconfig中去做一個DataImportHandler的定義腕柜。
1.首先在數(shù)據(jù)庫中準備一張類似表做測試:
注意updateTime的類型為
timestamp
逝变,默認值為:CURRENT_TIMESTAMP
:這里是為了后面增量更新檢測服務
然后準備好以下的jar包放到
core/lib
下,lib需要自己創(chuàng)建第一個是數(shù)據(jù)庫的驅動歼狼,后面兩個在根目錄的下的dist:
進入core/config【
solr-7.4.0\server\solr\helloSolr\conf
】創(chuàng)建一個數(shù)據(jù)源配置data-config.xml文件:
<dataConfig>
<dataSource
driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/solr_base?serverTimezone=GMT%2B8&useSSL=false"
user="root"
password="123456"/>
<document>
<entity name="product" pk="pid" query="SELECT pid,name,catalog_name,price,description,picture FROM products"
deltaQuery="select pid from products where updateTime >'${dih.last_index_time}'"
deletedPkQuery="select pid from products where isdelete=1"
deltaImportQuery="select pid,name,catalog_name,price,description,picture from products where pid='${dih.delta.pid}'">
<field column="pid" name="id" />
<field column="name" name="product_name" />
<field column="catalog_name" name="product_catalog_name" />
<field column="price" name="product_price" />
<field column="description" name="product_description" />
<field column="picture" name="product_picture" />
</entity>
</document>
</dataConfig>
說明:
1.我是最新的mysql版本比較新因此需要寫成:com.mysql.cj.jdbc.Driver
,Mysql 5.x直接寫成com.mysql.jdbc.Driver
,并且Url
也不用加?serverTimezone=GMT%2B8&useSSL=false"
,貌似新版的mysql才有時區(qū)問題嚎朽。
2.entity下的pk指向數(shù)據(jù)庫的表id名稱,比如我的id設計名稱為pid柬帕,則必須要在pk中指定一下哟忍。
3.entity下query為導入到solr中的屬性值,這個與下面field中的column是一一對應的關系陷寝,類似于mybtis自定義sql文件時的ORM映射關系锅很。
4.deltaQuery為增量數(shù)據(jù)的檢測sql語句,寫法就是: select 你的表id from 你的表 where updateTime >'${dih.last_index_time}'
凤跑,其中updateTime為前面設計默認值為CURRENT_TIMESTAMP
的字段爆安,該字段下,若對數(shù)據(jù)行任意列內容進行了修改仔引,則對應的updateTime自動修改為當前系統(tǒng)時間扔仓,都是固定的寫法褐奥,只能返回一個id,注意${dih.last_index_time}為固定寫法翘簇,會對應到data-import.propertis下的一個屬性值撬码,注意不要忘記表達式外面的單引號。
5.deletedPkQuery為刪除內容的增量sql版保,表字段中isdelete為1代表已經(jīng)刪除呜笑,其實就是一種偽刪除手段。同樣的pid為你具體表的一個id彻犁,都是固定的寫法叫胁,只能返回一個id。
6.增量修改數(shù)據(jù)的查詢sql語句:
select 返回的字段1,字段2袖裕,字段n from 查詢的表 where 你的表id名字='${dih.delta.你的表id名字}'
注意這里的表id名字要與你pk指向的id一致曹抬。
7.<field column="pid" name="id" />
表示映射關系,這里的映射是與manged-schma.xml中的映射關系
進入core/conf【solr-7.4.0\server\solr\helloSolr\conf
】急鳄,打開manged-schma.xml谤民,在結尾處添加一下內容:
<!--配置從數(shù)據(jù)庫導入到sorl中的數(shù)據(jù)的字段內容,所以每次要從數(shù)據(jù)庫導入什么就需要配置什么-->
<field name="product_name" type="text_ik" indexed="true" stored="true"/>
<field name="product_price" type="pfloat" indexed="true" stored="true"/>
<field name="product_description" type="text_ik" indexed="true" stored="false"/>
<field name="product_picture" type="string" indexed="false" stored="true"/>
<field name="product_catalog_name" type="string" indexed="true" stored="true"/>
<field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true" />
<copyField source="product_name" dest="product_keywords" />
<copyField source="product_description" dest="product_keywords" />
Tips:這里的name與data-config.xml
下的field中的name是一致的對應關系疾宏。copyField為復制域张足,也就是共用的意思,當搜索某個商品時候坎藐,會在product_name
和product_description
域中都去尋找为牍,也就是你淘寶買東西,不知道名字岩馍,你就搜用途描述也能找到該商品碉咆。type為text_ik的代表要用之前配置的中文分詞器去分詞處理,index代表索引蛀恩,store代表保存疫铜。type的各種寫法可以參照改文件中的具體定義,大約在207行左右的位置:
<fieldType name="string" class="solr.StrField" sortMissingLast="true" docValues="true" />
<fieldType name="strings" class="solr.StrField" sortMissingLast="true" multiValued="true" docValues="true" />
<!-- boolean type: "true" or "false" -->
<fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>
<fieldType name="booleans" class="solr.BoolField" sortMissingLast="true" multiValued="true"/>
<!--
Numeric field types that index values using KD-trees.
Point fields don't support FieldCache, so they must have docValues="true" if needed for sorting, faceting, functions, etc.
-->
<fieldType name="pint" class="solr.IntPointField" docValues="true"/>
<fieldType name="pfloat" class="solr.FloatPointField" docValues="true"/>
<fieldType name="plong" class="solr.LongPointField" docValues="true"/>
<fieldType name="pdouble" class="solr.DoublePointField" docValues="true"/>
<fieldType name="pints" class="solr.IntPointField" docValues="true" multiValued="true"/>
<fieldType name="pfloats" class="solr.FloatPointField" docValues="true" multiValued="true"/>
<fieldType name="plongs" class="solr.LongPointField" docValues="true" multiValued="true"/>
<fieldType name="pdoubles" class="solr.DoublePointField" docValues="true" multiValued="true"/>
<fieldType name="random" class="solr.RandomSortField" indexed="true"/>
這個type和數(shù)據(jù)庫中對應字段的類型保持一個對應關系双谆,如varchar對應string,日期的對應pdate等等壳咕。
最后進入core/conf【solr-7.4.0\server\solr\helloSolr\conf
】,打開solrconfig.xml
,添加如下內容:
<requestHandler name="/dataimport"
class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
這里也就看出為什么之前需要導入三個jar包到lib下顽馋。data-config.xml一定保證在同級目錄下:
最后重新啟動solr谓厘,導入數(shù)據(jù):
點擊execute即可。
然后點擊query寸谜,測試搜索數(shù)據(jù):
可以看到數(shù)據(jù)都查詢了出來竟稳。
Tips:如果第一次導入后查詢不到數(shù)據(jù),可以將數(shù)據(jù)庫某個記錄修改一下,再重新導入
問題三:如何實現(xiàn)增量更新數(shù)據(jù)住练?
對于這個問題首先只需要理解和掌握兩個東西
- 之前配置過的data-config.xml文件
<dataConfig>
<dataSource
driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/solr_base?serverTimezone=GMT%2B8&useSSL=false"
user="root"
password="123456"/>
<document>
<entity name="product" pk="pid" query="SELECT pid,name,catalog_name,price,description,picture FROM products"
deltaQuery="select pid from products where updateTime >'${dih.last_index_time}'"
deletedPkQuery="select pid from products where isdelete=1"
deltaImportQuery="select pid,name,catalog_name,price,description,picture from products where pid='${dih.delta.pid}'">
<field column="pid" name="id" />
<field column="name" name="product_name" />
<field column="catalog_name" name="product_catalog_name" />
<field column="price" name="product_price" />
<field column="description" name="product_description" />
<field column="picture" name="product_picture" />
</entity>
</document>
</dataConfig>
前面已經(jīng)提到過query地啰、deltaQuery、deletedPkQuery讲逛、deltaImportQuery
這四個的作用要分清楚亏吝。
首先是增量更新索引的原理:首先是在數(shù)據(jù)庫表中設計的一個默認值為當前系統(tǒng)時間的updateTime
字段,這個字段在每次修改記錄時候盏混,會自動把值設置為當前系統(tǒng)時間(修改那行記錄蔚鸥,對于那行的update就自動修改),因此對于增量操作來說:只需要掃描出solr中緩存的時間${dih.last_index_time}
<updateTime
則為需要增量更新的記錄许赃。
而solr中是這樣的順序:1.掃描到updateTime>dih.last_index_time的id(deltaQuery
)2.通過這個id去增量索引(deltaImportQuery
)
對于刪除的原理:其實就是掃描出isdelete=1的記錄后止喷,在solr中對應的數(shù)據(jù)記錄給移除掉。
全量更新原理:就是query
這個sql執(zhí)行一次的結果
- solr通過http請求去控制執(zhí)行data-config.xml中的方法
打開solr的web管理主頁的dataimport菜單:
在cmd中full-import則是代表全量更新混聊,對應執(zhí)行一次Query
弹谁,打開server/logs/solr.log:
同時在solr主頁點擊Execute,然后再觀察solr.log的內容(如果沒有變化句喜,重新打開)
02:11:22.843 INFO (qtp817348612-23) [ x:helloSolr] o.a.s.h.d.DataImporter Loading DIH Configuration: data-config.xml
2018-12-04 02:11:22.850 INFO (qtp817348612-23) [ x:helloSolr] o.a.s.h.d.DataImporter Data Configuration loaded successfully
2018-12-04 02:11:22.861 INFO (qtp817348612-23) [ x:helloSolr] o.a.s.c.S.Request [helloSolr] webapp=/solr path=/dataimport params={core=helloSolr&indent=on&commit=true&name=dataimport&clean=false&wt=json&command=full-import&_=1543888704549&verbose=false} status=0 QTime=19
2018-12-04 02:11:22.873 INFO (Thread-20) [ ] o.a.s.h.d.DataImporter Starting Full Import
2018-12-04 02:11:22.877 INFO (qtp817348612-44) [ x:helloSolr] o.a.s.c.S.Request [helloSolr] webapp=/solr path=/dataimport params={indent=on&wt=json&command=status&_=1543888704549} status=0 QTime=0
2018-12-04 02:11:22.889 INFO (Thread-20) [ ] o.a.s.h.d.SimplePropertiesWriter Read dataimport.properties
2018-12-04 02:11:23.114 INFO (Thread-20) [ ] o.a.s.h.d.JdbcDataSource Creating a connection for entity product with URL: jdbc:mysql://127.0.0.1:3306/solr_base?serverTimezone=GMT%2B8&useSSL=false
2018-12-04 02:11:23.263 INFO (Thread-20) [ ] o.a.s.h.d.JdbcDataSource Time taken for getConnection(): 148
2018-12-04 02:11:23.502 INFO (Thread-20) [ ] o.a.s.h.d.DocBuilder Import completed successfully
連接了一次mysql并且打印出了發(fā)送http請求的參數(shù)预愤。
這次打開瀏覽器的開發(fā)者模式,再執(zhí)行一次Execute:
確實是執(zhí)行了一次post請求:
http://127.0.0.1:8983/solr/helloSolr/dataimport?core=helloSolr&indent=on&commit=true&name=dataimport&clean=false&wt=json&command=full-import&_=1543888704549&verbose=falsecore=helloSolr&indent=on&commit=true&name=dataimport&clean=false&wt=json&command=full-import&_=1543888704549&verbose=false
打開Postman咳胃,將提取的url植康,放進去,并在sql中新插入一條記錄:
send后:
{
"responseHeader":{
"status":0,
"QTime":25},
"initArgs":[
"defaults",[
"config","data-config.xml"]],
"command":"full-import",
"status":"idle",
"importResponse":"",
"statusMessages":{
"Total Requests made to DataSource":"1",
"Total Rows Fetched":"11",
"Total Documents Processed":"11",
"Total Documents Skipped":"0",
"Full Dump Started":"2018-12-04 02:17:19",
"":"Indexing completed. Added/Updated: 11 documents. Deleted 0 documents.",
"Committed":"2018-12-04 02:17:20",
"Time taken":"0:0:0.450"}}
最后在solr控制主頁去query一下:
新增加的記錄發(fā)現(xiàn)已經(jīng)添加成功了展懈,注意默認query顯示10條記錄销睁,超過10條要設置start和rows的值存崖。
-
增量更新
打開data-import菜單:
將Command切換成delta-import冻记,然后修改數(shù)據(jù)中任意記錄:
然后,執(zhí)行Execute:
查看solr.log:
查看network:
同樣的也是一次Post請求:
http://127.0.0.1:8983/solr/helloSolr/dataimport?core=helloSolr&indent=on&commit=true&name=dataimport&clean=false&wt=json&command=delta-import&_=1543888704549&verbose=false
這個提取到Postman中send后是一樣的結果
最終修改的記錄都得到了更新来惧。
總結
增量和全量對于我們開發(fā)者來說相當于像solr服務器發(fā)送兩次Post請求冗栗。
不過再solr.log中會發(fā)現(xiàn)一個問題:
018-12-04 02:11:23.502 INFO (Thread-20) [ ] o.a.s.h.d.DocBuilder Import completed successfully
2018-12-04 02:11:23.502 INFO (Thread-20) [ ] o.a.s.u.DirectUpdateHandler2 start commit{_version_=1618885459052593152,optimize=false,openSearcher=true,waitSearcher=true,expungeDeletes=false,softCommit=false,prepareCommit=false}
2018-12-04 02:11:23.503 INFO (Thread-20) [ ] o.a.s.u.SolrIndexWriter Calling setCommitData with IW:org.apache.solr.update.SolrIndexWriter@651aa215 commitCommandVersion:1618885459052593152
2018-12-04 02:11:23.823 INFO (qtp817348612-23) [ x:helloSolr] o.a.s.c.S.Request [helloSolr] webapp=/solr path=/dataimport params={indent=on&wt=json&command=status&_=1543888704549} status=0 QTime=1
2018-12-04 02:11:23.854 INFO (Thread-20) [ ] o.a.s.s.SolrIndexSearcher Opening [Searcher@6ec70aa[helloSolr] main]
2018-12-04 02:11:23.855 INFO (Thread-20) [ ] o.a.s.u.DirectUpdateHandler2 end_commit_flush
2018-12-04 02:11:23.855 INFO (searcherExecutor-10-thread-1) [ ] o.a.s.c.QuerySenderListener QuerySenderListener sending requests to Searcher@6ec70aa[helloSolr] main{ExitableDirectoryReader(UninvertingDirectoryReader(Uninverting(_1(7.4.0):C11/10:delGen=1) Uninverting(_4(7.4.0):C11)))}
2018-12-04 02:11:23.855 INFO (searcherExecutor-10-thread-1) [ ] o.a.s.c.QuerySenderListener QuerySenderListener done.
2018-12-04 02:11:23.861 INFO (searcherExecutor-10-thread-1) [ ] o.a.s.c.SolrCore [helloSolr] Registered new searcher Searcher@6ec70aa[helloSolr] main{ExitableDirectoryReader(UninvertingDirectoryReader(Uninverting(_1(7.4.0):C11/10:delGen=1) Uninverting(_4(7.4.0):C11)))}
2018-12-04 02:11:23.881 INFO (Thread-20
這里的日期跟實際的日期不一樣,相差了八個時區(qū)违寞。雖然再增量操作時候并沒有遇到什么問題,但還是有點別扭偶房。
修改方法:
打開solr/bin/solr.cmd,搜索UTC:
在后面+8
然后重啟服務趁曼,再查看日志里的日期:
和我電腦日期一致了。
同時
core/conf/dataimport.properties
也自動變成了東八區(qū):定時增量/全量更新索引
之前實現(xiàn)了增量/全量更新索引棕洋,其實只是發(fā)送了下面的Post請求:
增量:
http://127.0.0.1:8983/solr/helloSolr/dataimport?core=helloSolr&indent=on&commit=true&name=dataimport&clean=false&wt=json&command=delta-import&_=1543888704549&verbose=false
全量:
http://127.0.0.1:8983/solr/helloSolr/dataimport?core=helloSolr&indent=on&commit=true&name=dataimport&clean=false&wt=json&command=full-import&_=1543888704549&verbose=false
一種方式是自己寫定時task挡闰,一種方式是使用solr提供的scheduler,solr只是提供了一個監(jiān)聽器,具體的策略代碼需要自己寫摄悯。gitHup也有很多solr-dataimportScheduler赞季。使用solr提供的工具去寫的好出是:
1.可以直接集成到solr的webapp中,不用單獨起一個java項目
2.solr封裝了直接讀取dataimport.properties的對象SolrResourceLoader
奢驯,可以讓操作更加簡單申钩。
這里提供一個solr-dataimportScheduler的下載地址:
個人修改的地址:https://github.com/Siwash/rpf-solr-study/tree/master/solr-dataimportscheduler-master
原作者地址:https://github.com/magese/solr-data-import-scheduler
我修改的地方:
注釋了這一行的日志。因為SolrResourceLoader在solr7的時候這個方法是getInstancePath瘪阁,而之前是getResourcePath撒遣。如果你開發(fā)過程中遇到了如下的報錯:
2018-12-04 11:11:49.902 WARN (main) [ ] o.e.j.w.WebAppContext Failed startup of context o.e.j.w.WebAppContext@6aaceffd{/solr,file:///D:/solr_server_7.4/solr-7.4.0/server/solr-webapp/webapp/,UNAVAILABLE}{file:///D:/solr_server_7.4/solr-7.4.0/server//solr-webapp/webapp}
java.lang.NoSuchMethodError: org.apache.solr.core.SolrResourceLoader.<init>(Ljava/lang/String;)V
at org.apache.solr.handler.dataimport.scheduler.SolrDataImportProperties.loadProperties(SolrDataImportProperties.java:36) ~[?:?]
at org.apache.solr.handler.dataimport.scheduler.BaseTimerTask.reloadParams(BaseTimerTask.java:57) ~[?:?]
at org.apache.solr.handler.dataimport.scheduler.BaseTimerTask.<init>(BaseTimerTask.java:39) ~[?:?]
at org.apache.solr.handler.dataimport.scheduler.DeltaImportHTTPPostScheduler.<init>(DeltaImportHTTPPostScheduler.java:20) ~[?:?]
at org.apache.solr.handler.dataimport.scheduler.ApplicationListener.contextInitialized(ApplicationListener.java:47) ~[?:?]
at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:890) ~[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:558) ~[jetty-servlet-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:853) ~[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:370) ~[jetty-servlet-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1497) ~[jetty-webapp-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1459) ~[jetty-webapp-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:785) ~[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:287) ~[jetty-servlet-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:545) [jetty-webapp-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:46) [jetty-deploy-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:192) [jetty-deploy-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:505) [jetty-deploy-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:151) [jetty-deploy-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:180) [jetty-deploy-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.deploy.providers.WebAppProvider.fileAdded(WebAppProvider.java:453) [jetty-deploy-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64) [jetty-deploy-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:610) [jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:529) [jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.util.Scanner.scan(Scanner.java:392) [jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.util.Scanner.doStart(Scanner.java:313) [jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.deploy.providers.ScanningAppProvider.doStart(ScanningAppProvider.java:150) [jetty-deploy-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.deploy.DeploymentManager.startAppProvider(DeploymentManager.java:579) [jetty-deploy-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.deploy.DeploymentManager.doStart(DeploymentManager.java:240) [jetty-deploy-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138) [jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.server.Server.start(Server.java:419) [jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117) [jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113) [jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.server.Server.doStart(Server.java:386) [jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1569) [jetty-xml-9.4.10.v20180503.jar:9.4.10.v20180503]
at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1509) [jetty-xml-9.4.10.v20180503.jar:9.4.10.v20180503]
at java.security.AccessController.doPrivileged(Native Method) [?:1.8.0_111]
at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1508) [jetty-xml-9.4.10.v20180503.jar:9.4.10.v20180503]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_111]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_111]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_111]
at org.eclipse.jetty.start.Main.invokeMain(Main.java:220) [start.jar:9.4.10.v20180503]
at org.eclipse.jetty.start.Main.start(Main.java:486) [start.jar:9.4.10.v20180503]
at org.eclipse.jetty.start.Main.main(Main.java:77) [start.jar:9.4.10.v20180503]
2018-12-04 11:11:49.921 INFO (main) [ ] o.e.j.s.AbstractConnector Started ServerConnector@738dc9b{HTTP/1.1,[http/1.1]}{0.0.0.0:8088}
2018-12-04 11:11:49.921 INFO (main) [ ] o.e.j.s.Server Started @2805ms
NoSuchMethodError其實就是因為找不到getResourcePath這個方法,將這一行打印日志注釋后管跺,就能保證同時兼容solr4-solr7了义黎。
jar包地址:https://github.com/Siwash/rpf-solr-study/blob/master/solr-dataimportscheduler-4.x-7.x.jar
具體的應用方法:
進入solr-7.4.0\server\solr-webapp\webapp\WEB-INF
打開web.xml:
添加監(jiān)聽器:
<listener>
<listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class>
</listener>
進入solr-7.4.0\server\solr\conf,注意:conf文件夾需要手動創(chuàng)建的豁跑,新建一個dataimport.properties:
#Tue Dec 04 10:47:18 GMT+08:00 2018
last_index_time=2018-12-04 10\:47\:17
product.last_index_time=2018-12-04 10\:47\:17
#################################################
# #
# 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=你的core名字1,你的core名字2
# solr server name or IP address
# [defaults to localhost if empty]
server=你啟動服務的ip地址
# solr server port
# [defaults to 80 if empty]
port=啟動的端口號
# application name/context
# [defaults to current ServletContextListener's context (app) name]
webapp=默認web名字就是solr
# URL params [mandatory]
# remainder of URL
#剛才增量的參數(shù)
params=/dataimport?command=delta-import&commit=ture&wt=json&indent=true&entity=product&clean=false
# schedule interval
# number of minutes between two runs
# [defaults to 30 if empty]
#一分鐘執(zhí)行一次
interval=1
修改好之后廉涕,重啟solr,在solr.log中查看日志記錄:
2018-12-04 11:32:03.408 INFO (searcherExecutor-10-thread-1-processing-x:helloSolr) [ x:helloSolr] o.a.s.c.SolrCore [helloSolr] Registered new searcher Searcher@16013ab9[helloSolr] main{ExitableDirectoryReader(UninvertingDirectoryReader(Uninverting(_1(7.4.0):C11/10:delGen=1) Uninverting(_6(7.4.0):C12/2:delGen=2) Uninverting(_9(7.4.0):C2)))}
2018-12-04 11:33:01.442 INFO (Timer-0) [ ] o.a.s.h.d.s.HttpPostScheduler [helloSolr] <index update process> Process started at .............. 04.12.2018 11:33:01 441
2018-12-04 11:33:01.493 INFO (Timer-0) [ ] o.a.s.h.d.s.HttpPostScheduler [helloSolr] <index update process> Request method POST
2018-12-04 11:33:01.493 INFO (Timer-0) [ ] o.a.s.h.d.s.HttpPostScheduler [helloSolr] <index update process> Succesfully connected to server localhost
2018-12-04 11:33:01.494 INFO (Timer-0) [ ] o.a.s.h.d.s.HttpPostScheduler [helloSolr] <index update process> Using port 8983
2018-12-04 11:33:01.494 INFO (Timer-0) [ ] o.a.s.h.d.s.HttpPostScheduler [helloSolr] <index update process> Application name solr
2018-12-04 11:33:01.494 INFO (Timer-0) [ ] o.a.s.h.d.s.HttpPostScheduler [helloSolr] <index update process> URL params /dataimport?command=delta-import&commit=ture&wt=json&indent=true&entity=product&clean=false
2018-12-04 11:33:01.494 INFO (Timer-0) [ ] o.a.s.h.d.s.HttpPostScheduler [helloSolr] <index update process> Full URL http://localhost:8983/solr/helloSolr/dataimport?command=delta-import&commit=ture&wt=json&indent=true&entity=product&clean=false
2018-12-04 11:33:01.502 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.DataImporter Loading DIH Configuration: data-config.xml
2018-12-04 11:33:01.507 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.DataImporter Data Configuration loaded successfully
2018-12-04 11:33:01.517 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.DataImporter Starting Delta Import
2018-12-04 11:33:01.525 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.SimplePropertiesWriter Read dataimport.properties
2018-12-04 11:33:01.538 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.DocBuilder Starting delta collection.
2018-12-04 11:33:01.562 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.DocBuilder Running ModifiedRowKey() for Entity: product
2018-12-04 11:33:01.563 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.JdbcDataSource Creating a connection for entity product with URL: jdbc:mysql://127.0.0.1:3306/solr_base?serverTimezone=GMT%2B8&useSSL=false
2018-12-04 11:33:01.687 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.JdbcDataSource Time taken for getConnection(): 123
2018-12-04 11:33:01.701 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.DocBuilder Completed ModifiedRowKey for Entity: product rows obtained : 0
2018-12-04 11:33:01.701 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.DocBuilder Completed DeletedRowKey for Entity: product rows obtained : 0
2018-12-04 11:33:01.701 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.DocBuilder Completed parentDeltaQuery for Entity: product
2018-12-04 11:33:01.701 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.DocBuilder Delta Import completed successfully
2018-12-04 11:33:01.701 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.h.d.DocBuilder Time taken = 0:0:0.174
2018-12-04 11:33:01.701 INFO (qtp817348612-16) [ x:helloSolr] o.a.s.u.p.LogUpdateProcessorFactory [helloSolr] webapp=/solr path=/dataimport params={indent=true&commit=ture&clean=false&wt=json&command=delta-import&entity=product}{} 0 204
2018-12-04 11:33:01.706 INFO (Timer-0) [ ] o.a.s.h.d.s.HttpPostScheduler [helloSolr] <index update process> Response message OK
2018-12-04 11:33:01.706 INFO (Timer-0) [ ] o.a.s.h.d.s.HttpPostScheduler [helloSolr] <index update process> Response code 200
2018-12-04 11:33:01.708 INFO (Timer-0) [ ] o.a.s.h.d.s.HttpPostScheduler [helloSolr] <index update process> Disconnected from server localhost
2018-12-04 11:33:01.708 INFO (Timer-0) [ ] o.a.s.h.d.s.HttpPostScheduler [helloSolr] <index update process> Process ended at ................ 04.12.2018 11:33:01 708
定時器已經(jīng)生效
這里的關鍵點是1.確保data-config.xml里面的sql沒寫錯艇拍,2.新建的dataimport.propertis沒寫錯狐蜕,路徑也是正確的,3.出現(xiàn)NoSuchMethodError確保注釋了getInstancePath/getResoucePath
列舉幾個遇到的異常:
1.sql異常淑倾,在solr.log中出現(xiàn)sql異常馏鹤,有的是連接異常[檢查驅動和url等參數(shù)],有的是查詢異辰慷撸【檢查sql語句寫錯沒有湃累,是否漏掉pk屬性賦值】
2.連接異常,這個是定時器中出現(xiàn)的異常碍讨,比如connect refuse等【檢查dataimport.propertis中web名字治力,連接地址,端口號和core名字等參數(shù)是否正確勃黍,可以在日志中提取url到postman進行測試】
3.NoSuchMethodError宵统,這個建議直接注釋getInstancePath/getResoucePath這一行的打印日志