前言
这应该是我入职之后的第一次更新,上周太忙了,培训完回寝室就是10点,然后这个solr自动更新花了我好长时间,因为网上都是tomcat+solr的形式,而我是docker启动solr,遇到了很多坑,终于今天算是完成了想要的效果,不枉我昨晚3点起来搞出了一个解决方案。
修改data-config.xml
主要是为了增加如下语句
deltaQuery="select id from datainfo where solrtime > '${dataimporter.last_index_time}'"
deltaImportQuery="select * from datainfo where id = '${dataimporter.delta.id}'">
其中solrtime是我在数据库表中新增的一项,用于记录与solr中的最后更新时间来做对比,而因为solr采用utc时间,时间比我本地时间要慢8小时,而我docker里面尝试了很多种修改时间的方法,服务器时间改了,docker容器时间改了,solr的dataimport的最后更新时间依旧是慢8小时。。。没办法,在java后台处将solrtime的时间减去8小时解决。。。
cd /var/lib/docker/volumes/2efbbdaab276537f691ee9d5fc6be2782220ff150437112394885fdfa7341f3a/_data
/data/myDATACore/conf/
vim data-config.xml
password="123456" encoding="UTF-8" />
deltaQuery="select id from datainfo where solrtime > '${dataimporter.last_index_time}'"
deltaImportQuery="select * from datainfo where id = '${dataimporter.delta.id}'">
修改managed-schema
值得注意的是如果需要使用solr对于时间的操作,type需要填写pdate,而非date,这里的utctime是用于时间范围查询使用,solrtime用于增量插入时与最后更新时间比对使用。
一个大坑
本想使用采用solr的自动更新方式来完成定时增量更新,结果花了我好长时间,始终有问题,在此记录过程,以后回头再看看。
导入jar包
跟之前配置solr一样,将我们自动定时更新所需要的jar放入服务器中,然后复制到docker容器内
docker cp apache-solr-dataimportscheduler.jar mySolr:/opt/solr/server/solr-webapp/webapp/WEB-INF/
lib/
进入docker容器
docker exec -it -u root mySolr /bin/bash
配置dataimport.properties
找到位置并创建文件
cd server/solr/
创建conf文件夹
mkdir conf
进入conf并创建dataimport.properties
touch dataimport.properties
哦,第一次在docker容器中先要使用vim需要先安装,此时进入容器必须使用 -u root以管理员进入,否则没有安装权限,且密码不明。
apt-get update
apt-get install vim
修改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=core #改成自己的
# solr server name or IP address
# [defaults to localhost if empty]
server=localhost
# solr server port
# [defaults to 80 if empty]
port=8080
# application name/context
# [defaults to current ServletContextListener's context (app) name]
webapp=solr
# URL params [mandatory]
# remainder of URL
params=/core/dataimport?command=delta-import&clean=false&commit=true
# schedule interval
# number of minutes between two runs
# [defaults to 30 if empty]
interval=10
修改web.xml配置监听
我就是在这里翻车了
org.apache.solr.handler.dataimport.scheduler.ApplicationListener
翻车
全部配置完毕之后重启solr
再次进入
致命。。。。搞了好久,不知道是哪的的问题,网上的案例都没有基于docker做的,也不知道哪出了毛病,只得将web.xml的监听注释掉。
思考
其实看下dataimportscheduler的工作原理也可以大致明白,就是通过一系列拼接,请求到了solr对应core的deltaImport,那我们自己在centos上写一个定时的http请求,去请求solr的增量更新地址,也能达到同样的效果。
最终方案
crontab -e
*/1 * * * * /usr/bin/curl -G -d "command=delta-import&clean=false&commit=true" "http://ip:port/solr/myDATACore/dataim
port"
ip和端口以及core改成自己的即可。这样就能过一分钟请求一次,完成增量更新。
当然,也可以采用这种方式来完成定时全量更新
crontab -e
0 3 * * * curl http://ip:port/solr/myDATACore/dataimport?command=full-import
这个例子是每天3点执行一次全量更新
至于crontab -e创建新任务,就跟vim操作一样。
至此,我想要的效果终于完成。
吐槽
额。。。充满妥协的方案,如果最后效果还可以,总感觉不太爽,不过至少比没做出来强。