solr 配置自動同步數據庫數據(全量,增量)
solr6.3.3
mysql-connector-java-5.1.45-bin.jar
solr 以 jetty 方式部署 (非tomcat)
* 1.將 mysql-connector-java-5.1.45-bin.jar 放到 ./dist下
* 2. 修改數據倉庫下的配置文件 ./collocation1/conf/solrconfig.xml
在大約70行左右的範圍 加入以下幾行
<lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-dataimporthandler-.*\.jar"/>
<lib dir="${solr.install.dir:../../../..}/dist/" regex="mysql-connector-java-\d.*\.jar" />
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
* 3. 再在 ./collocation1/conf 創建 data-config.xml文件, 內容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource" name="test" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/test" user="root" password="1234"/>
<document>
<entity
pk="id"
dataSource="test"
name="t_product"
query="select id,name,alias_name,old_product_id,top_product_id,type from t_product where status = '1' "
deltaQuery="select id from t_product where update_dt > '${dataimporter.last_index_time}'"
deletedPkQuery="select id from t_product where status = 10"
deltaImportQuery="select id,name,alias_name,old_product_id,top_product_id,type from t_product where id='${dataimporter.delta.id}'"
>
<field name="id" column="id" />
<field name="name" column="name" />
<field name="alias_name" column="alias_name" />
<field name="old_product_id" column="old_product_id" />
<field name="top_product_id" column="top_product_id" />
<field name="type" column="type" />
</entity>
</document>
</dataConfig>
# 同步數據語句
query="select id,name,alias_name,old_product_id,top_product_id,type from t_product where status = '1' "
# 增量同步語句 只去 id 用於 只同步被修改的 根據 ${dataimporter.last_index_time}'
# 這裏注意時間 update_dt 爲int, dataimporter.last_index_time爲 timestamp
deltaQuery="select id from t_product where FROM_UNIXTIME(update_dt , '%Y-%m-%d %H:%i:%S') > '${dataimporter.last_index_time}'"
# 拉取 增量同步需要修改的數據
deltaImportQuery="select id,name,alias_name,old_product_id,top_product_id,type from t_product where id='${dataimporter.delta.id}'"
# 同步需要刪除的數據
deletedPkQuery="select id from t_product where status = 10"
* 4. 在 ./bin 的 solr.in.sh 內 修改 solr 的默認時區爲 PRC, 大約在 60+ 行
# By default the start script uses UTC; override the timezone if needed
SOLR_TIMEZONE="Asia/Shanghai"
# 不生效的話 可以修改 ./bin/solr
vim solr
搜索 /UTC/
改爲 Asia/Shanghai
* 5. 定時腳本修復
# 在索引之前先清除索引
clean=true
# 無論做什麼操作 都提交
commit=true
# 更新那個實體對應 的 配置文件中的 實體
entity=t_product
# 無論做什麼操作 都優化
optimize=false
# 是否開啓 debug
debug=false
# 是否阻塞所有請求
synchronous=false
# 增量更新數據
http://localhost:8983/solr/collocation1/dataimport?command=delta-import&clean=false&commit=true&wt=json&indent=true&entity=t_product&verbose=false&optimize=false&debug=false&synchronous=false&id=1
# 全量跟新數據
http://localhost:8983/solr/collocation1/dataimport?command=full-import&clean=true&commit=true&wt=json&indent=true&entity=t_product&verbose=false&optimize=false&debug=false&synchronous=false
# 重載配置
http://localhost:8983/solr/collocation1/dataimport?command=reload-config
# 查看運行的統計狀態
http://localhost:8983/solr/collocation1/dataimport?command=status
# 查看配置
http://localhost:8983/solr/collocation1/dataimport?command=show-config
tips: 一定要注意改對文件(注意文件位置)
ps -aux | grep solr 可以查看 solr 啓動的各種配置項.