solr定时实时重建索引和增量更新


分类: solr   2659人阅读  评论(0)  收藏  举报

注:在上一篇的solr增量索引中遇到了一个很大的问题:定时任务一直无法执行,后来找了很多原因,分析日志后发现增量索引的请求都没发送,又经过一番折腾才在网上找到了解决方法,是apache-solr-dataimportscheduler-1.0.jar的一个bug,post请求无法正确发送,这位仁兄的文章真是精华所在,感谢这位仁兄!


Solr Data Import Hander Scheduler 说明: 
       Solr官方提供了很强大的Data Import Request Handler,同时提供了一个简单的 Scheduler, 
示例中的 Scheduler 只支持增量更新,不支持定期重做索引,因此我做了一个简单的封装,增加了重做索引的定时器. 
(原定时器作者是:Marko Bonaci, 在此表示感谢!)The original Scheduling source by Marko Bonaci, Thank him! 
使用说明 
   1. 将 apache-solr-dataimportscheduler-1.0.jar 和solr自带的 apache-solr-dataimporthandler-.jar, apache-solr-dataimporthandler-extras-.jar 放到solr.war的lib目录下面 
   2.修改solr.war中WEB-INF/web.xml, 在servlet节点前面增加: 

?
1
2
3
4
5
< listener >
           < listener-class >
                 org.apache.solr.handler.dataimport.scheduler.ApplicationListener
           </ listener-class >
</ listener >
      3.将apache-solr-dataimportscheduler-.jar 中 dataimport.properties 取出并根据实际情况修改,然后放到 solr.home/conf (不是solr.home/core/conf) 目录下面  
    4.重启tomcat或者jboss 即可 

dataimport.properties 配置项说明


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#################################################
#                                               #
#       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=core1,core2
 
#  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= /dataimport ? command =delta- import &clean= false &commit= true
 
#  schedule interval
#  number of minutes between two runs
#  [defaults to 30 if empty]
interval=1
 
#  重做索引的时间间隔,单位分钟,默认7200,即5天;
#  为空,为0,或者注释掉:表示永不重做索引
reBuildIndexInterval=7200
 
#  重做索引的参数
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

上文出处:http://code.google.com/p/solr-dataimport-scheduler/


博主ps:

如果你运行报以下异常:

org.apache.catalina.core.StandardContext listenerStart 
SEVERE: Exception sending context initialized event to listener instance of class org.apache.solr.handler.dataimport.scheduler.ApplicationListener 
java.lang.NullPointerException 
    at org.apache.solr.handler.dataimport.scheduler.ApplicationListener.contextInitialized(ApplicationListener.java:93) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4210) 
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4709) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:583) 
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079) 
    at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002) 
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506) 
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317) 
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324) 
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142) 
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065) 
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:822) 
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 
    at org.apache.catalina.core.StandardService.start(StandardService.java:525) 
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754) 
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

那说明你dataimport.properties的存放位置不正确,注意第3点,dataimport.properties存放在tomcat的solr.xml里配置的solr/home路径的conf文件夹下,conf文件夹是没有的,需新建

例如solr.xml的配置如下:


?
1
2
3
4
<? xml version = "1.0" encoding = "UTF-8" ?>
< Context docBase = "E:\solrbase\tomcat7\webapps\solr" debug = "0" crossContext = "true" >
     < Environment name = "solr/home" type = "java.lang.String" value = "E:\solrbase\solr" override = "true" />
</ Context >



dataimport.properties位置是:E:\solrbase\solr\conf\dataimport.properties


dataimport.properties属性文件param配置项示例:

例如:/deltaJob?command=delta-import&clean=false&commit=true& optimize=false&wt=json&indent=true&entity=companyJob&verbose=false&debug=false

上面链接根据实际solrconfig.xml和db-config.xml里配置文件里情况来定的。


发现两个bug,一个是如果reBuildIndexBeginTime的值为空,则会导致interval强转时报错,另一个是请求solr服务器时返回的状态码永远是415(对于当前请求的方法和所请求的资源,请求中提交的实体并不是服务器中所支持的格式,因此请求被拒绝。),

为什么了?

先看第一个bug,

看代码:

QQ截图20140307100358.png

                                                                        图1

这一块代码直接导致一下方法调用的时候报强转错,

QQ截图20140307101559.png

                                                                        图2

这个方法对interval属性进行了强转

QQ截图20140307101901.png

                                                                        图3

看如果reBuildIndexBeginTime为空时的设置的代码:

QQ截图20140309224036.png

                                                                         图4

以上红框是更改后的格式,原格式只有“yyyy-MM-dd”部分,这样会导致启动的时候会不停地http请求solr服务器,因此需加“HH:mm:ss”,以此解决问题。

第一个bug解决方法把图1中画红框的代码去除就行了。

第二个bug,看代码:

QQ截图20140307183708.png

                                                                                图4

导致原因是HTTP以post请求,我们知道dataimport.properties的param是以get方式的链接,参数也包含在里面,程序未对此链接形式做处理封装成post提交方式,所以导致请求solr失败,把Http链接方式改成get就行了。


修复bug,重新打包后的jar下载:

solr-dataimportscheduler-1.1

solr-dataimportscheduler-1.1-source

dataimport.properties.zip

dataimport.properties.zip解压到tomcat的solr.xml里配置的solr/home路径,里面含有conf文件夹及dataimport.properties

原jar下载:

apache-solr-dataimportscheduler-1.0.jar 

apache-solr-dataimportscheduler-1.0-with-source.jar


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值