常用命令:
>命令
終止、增量、全量、重新加載DIH配置、查詢狀態,返回文檔創建數,文檔刪除數等狀態信息
http://localhost:8389/solr/book/dataimport?command=abort
http://localhost:8389/solr/book/dataimport?command=delta-import
http://localhost:8389/solr/book/dataimport?command=full-import
http://localhost:8389/solr/book/dataimport?command=reload-config
http://localhost:8389/solr/book/dataimport?command=status
>參數
command full-import/delta-import/status/reload-config/abort 全量導入 / 增量導入 / 導入狀態 / 重新加載配置文件 / 中止導入
verbose true/false 輸出詳細步驟
clean true/false 刪除原有索引
commit true/false 導入後是否提交
optimize true/false 導入後是否優化索引
debug true/false 調試模式,不會提交數據
entity 與db-data-config.xml中定義的entity對應 所要導入的entity
start 整數 調試模式用,開始的記錄數
rows 整數 調試模式用,導入的記錄數
>全量刪除:
<delete><query>*:*</query></delete><commit/>
>Entity屬性:
pk="ID" 這個很有必要,因爲其中的增量索引查詢主鍵ID時需要
dataSource="mydb" 這個引用名字是引用上面數據源的名字
name="myinfo" 這個名字必須唯一,存在多個實體時
query="select * from myinfo WHERE isdelete=0"
query查詢是指查詢出表裏所有的符合條件的數據,因爲筆者測試的有刪除業務,所以where後面有一個限定條件isdelete=0,意思爲查詢未被刪除的數據
(注意這個query查詢只對第一次全量導入有作用,對增量導入不起作用)
deltaQuery="select ID from myinfo where my_date > '${dataimporter.last_index_time}'"
deltaQuery的意思是,查詢出所有經過修改的記錄的ID可能是修改操作,添加操作,刪除操作產生的此查詢只對增量導入起作用,而且只能返回ID值)
deletedPkQuery="select ID from myinfo where isdelete=1"
此操作值查詢那些數據庫裏僞刪除的數據的ID(即isdelete標識爲1的數據)
solr通過它來刪除索引裏面對應的數據
(此查詢只對增量導入起作用,而且只能返回ID值)
deltaImportQuery="select * from myinfo where ID='${dataimporter.delta.ID}'"
此查詢是獲取以上兩步的ID,然後把其全部數據獲取,根據獲取的數據
對索引庫進行更新操作,可能是刪除,添加,修改
(此查詢只對增量導入起作用,可以返回多個字段的值,一般情況下,都是返回所有字段的列)
parentDeltaQuery是獲取父Entity的pk的SQL
>DIH具體配置:
1. solr create -c reader
2. cd solr-7.4.0\server\solr\reader\conf
3. solrconfig.xml中添加
<lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-dataimporthandler-.*\.jar" />
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">db-data-config.xml</str>
</lst>
</requestHandler>
4. 創建db-data-config.xml
<dataConfig>
<dataSource type="JdbcDataSource"
name="ds-1"
driver="org.postgresql.Driver"
url="jdbc:postgresql://x.x.x.x:5432/xxxx"
user="xxxx"
password="xxxx"/>
<document>
<entity dateSource="ds-1" name="reader" pk="dzid"
query="select dzid,xming,dzzhao,picture,optime from lt_dzheinfo"
deltaQuery="select dzid from lt_dzheinfo where optime > '${dih.last_index_time}'"
deletedPkQuery="select dzid from lt_dzheinfo where ztai != '正常'"
deltaImportQuery="select * from lt_dzheinfo where dzid = '${dih.delta.dzid}'"
>
<field column="dzid" name="id"></field>
<field column="xming" name="xming"></field>
<field column="dzzhao" name="dzzhao"></field>
<field column="picture" name="picture"></field>
<field column="optime" name="optime"></field>
</entity>
</document>
</dataConfig>
5. 定義solr創建DB所用字段(schema中缺少的),打開managed-schema文件
<field name="xming" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="dzzhao" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="picture" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="optime" type="pdate" indexed="true" stored="true" required="true" multiValued="false" />
>定時DIH配置:
1. 添加依賴包:
下載solr-dataimportscheduler.jar包放入solr-7.4.0\server\solr-webapp\webapp\WEB-INF\lib下
(文件下載:https://download.csdn.net/download/weixin_38289303/12005671)
2. 修改web.xml文件,添加監聽器:
solr-7.4.0\server\solr-webapp\webapp\WEB-INF\web.xml添加如下配置
<listener>
<listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class>
</listener>
3. 查看日誌:solr-7.4.0\server\logs\solr.log。添加指定的文件到指定目錄。
新建文件:solr-7.4.0\server\solr\conf\dataimport.properties(若沒conf目錄,自己創建)。
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
syncCores=reader
# solr server name or IP address
# [defaults to localhost if empty]
server=localhost
# solr server port
# [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
#剛纔增量的參數
params=/dataimport?command=delta-import&commit=ture&wt=json&indent=true&entity=reader&clean=false
# schedule interval
# number of minutes between two runs
# [defaults to 30 if empty]
#一分鐘執行一次
interval=1
# 重做索引的時間間隔,單位分鐘,默認7200,即5天;
# 爲空,爲0,或者註釋掉:表示永不重做索引
reBuildIndexInterval=10080
# 重做索引的參數
reBuildIndexParams=/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
4. 測試成功!