Solr学习之路-4定时增量导入

in Java with 0 comment

Solr学习之路-3Data Import Handler最主要的就是做了个全量导入,那之后如何做索引呢?一直全量导入岂不是浪费了许多资源?做个定时的增量导入就可以一劳永逸了。继续在Tomcat的solr web项目做实验。

Web定时

1、web.xml 添加监听器

定时机制要随应用启动后自动启动。就需要添加监听器:

<!– add solr listener–>
  <listener>
     <listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class>
  </listener>

2、配置增量导入参数

我们需要配置增量导入的相关参数,比如:周期,是否启动增量导入、访问URL等等。

#################################################
#                                               #
#       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=game,resource
syncCores=code1
#  solr server name or IP address
#  [defaults to localhost if empty]
server=localhost
#  solr server port
#  [defaults to 80 if empty]
port=7080
#  application name/context
#  [defaults to current ServletContextListener’s context (app) name]
webapp=solr
#  URL params [mandatory]
#  remainder of URL
#  增量更新的请求参数
params=/dataimport?command=delta-import&clean=false&commit=true
#  schedule interval
#  number of minutes between two runs
#  [defaults to 30 if empty]
#  配置的是1 min 一次
interval=1
#  重做索引的实现已经被后面的代码版本阉割了
#  重做索引的时间间隔,单位分钟,默认7200,即5天;
#  为空,为0,或者注释掉:表示永不重做索引
reBuildIndexInterval=7200
#  重做索引的参数
reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true
#  重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;
#  两种格式:2016-10-10 02:10:00 或者  03:10:00,后一种会自动补全日期部分为服务启动时的日期
reBuildIndexBeginTime=02:10:00

启动Tomcat,竟然报错:java.lang.ClassNotFoundException:org.apache.solr.handler.dataimport.scheduler.ApplicationListener

提示找不到类,原来相关代码没有引入,找一下apache-solr-dataimportscheduler.jar或者自己编译一下源码。我选择了jar包:https://github.com/mbonaci/solr-data-import-scheduler 下载下来放在【WEB-INF/lib】包后,启动Tomcat又报错:java.lang.NoSuchMethodError:org.apache.solr.core.SolrResourceLoader

提示找不到方法,只好把jar包反编译重新编码,后来打开jar包看后,字节码和源码都一并提供了,完全没有必要,之后发现,原来下载的jar包引用了低版本的solr-core包中SolrResourceLoader中的getInstanceDir()对象方法,而高版本弃用了这方法。结果报错了。

换成其他对象方法:getInstancePath(),如图:

getInstancePath

又顺便把代码里访问URL的POST请求方式改成GET方式了。重新编译启动,又是不顺利,提示报错:

java.io.FileNotFoundException: D:\tomcat8.0.24\webapps\solr\solrhome\conf\dataimport.properties (系统找不到指定的路径。)

原来配置文件放错了,把配置好的文件dataimport.properties放到【solrhome\conf】目录下,继续,发现可以启动了,但没有任何反应。。。

我把周期设成了1分钟,没道理啊。进入UI管理界面,手动增量导入了下,有报错:

ORA-01861: literal does not match format string

把db-config配置文件中的最后索引时间转出date型就可以了:to_date('${dataimporter.last_index_time}','yyyy-mm-dd hh24:mi:ss')

人生,无处不在的坑。。。

再看下是不是listener监听器的启动问题,把他放到filter配置之后和servlet之前,然而并没有什么用,真是一波三折啊,查看了许多前辈的文章,没有什么很大的区别啊,实在懵逼了。只好再检查下配置文件dataimport.properties,发现设置周期使能参数syncEnabled=1值后面多了些空格。。。崩溃

把所有参数检查了一遍,重启后,终于胜利了。

solr_delta-import

定时增量这是基于程序应用的,也可以基于系统的。在Linux下使用crontabcurl做个脚本程序或者在Windows下做个计划任务,甚至是浏览器定时刷新都是可以的。

Linux crontab定时

以Linux crontab 为例 每隔一分钟,定时Get请求方式访问URL进行增量导入

Linux终端下输入crontab -e进入编辑模式:

*/1 * * * * /usr/bin/curl -G -d "/dataimport?command=delta-import&clean=false&commit=true" http://localhost:7080/solr/core1

然后重启服务

service crond restart //重启服务

我们可以看看crontab的基本语法格式 :

*  *  *  *  *  command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令

“*” 代表取值范围内的数字,
“/” 代表”每”,
“-” 代表从某个数字到某个数字,
“,” 分开几个离散的数字

没必要的坑那就不要去跳,人做事真的要有自己的思路和计划,不然费功真的必不可少。。。

附录:

https://wiki.apache.org/solr/DataImportHandler