solr.home D:solr\home
solr.war D:solr\server\solr
参考文档
- IBM developerWorks 文档库 Apache Solr 的新特性
- Solr中国 http://www.solr.cc/blog/
- 官方文档 http://wiki.apache.org/solr/DataImportHandler#Using_delta-import_command
全量索引与增量索引
索引导入分全量索引与增量索引。Solr支持多种创建索引的方式。这里通过solr官方提供的一个工具的—Data Import Handler,来做数据库建立索引,DIH支持增量索引。Solr/example中有example-DIH的项目,用了hsqldb作为数据库演示了DIH的使用,可以看下
1、添加jar包 (mysql的jar包与dataimporthandler的jar包)
将解压后的solr-4.10.0文件夹dist目录下的:
- solr-dataimporthandler-4.10.0.jar
- solr-dataimporthandler-extras-4.10.0.jar
- mysql-connector-java-5.1.13-bin.jar (这个需根据使用的数据库不同添加不同的驱动包,所以dist目录下是没有的)
拷贝到solr.war服务的lib目录下,对应本机D:\solr\server\solr\WEB-INF\lib
2、配置一个 SolrRequestHandler
将 DIH 和 Solr 关联起来
在每个核心中的solrconfig.xml中配置dataimport,对应本机D:solr\home\core1\conf\solrconfig.xml
<!-- 全量索引与增量索引 --> <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
3、全量索引与增量索引配置文件,同目录下新增data-config.xml
<pre class="html" name="code"><?xml version="1.0" encoding="UTF-8"?> <dataConfig> <dataSource name="jdbc" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://ip:port/db_name?characterEncoding=UTF-8" user="root" password="root"/> <document name="TestCore"> <entity name="test_table" pk="testId" query="select * from test_table" deltaImportQuery="select * from test_table where test_id='${dih.delta.testId}'" deltaQuery="select Test_Id as testId from test_table where create_time > '${dih.last_index_time}'" transformer="RegexTransformer"> <field column="Test_Id" name="testId"/> <field column="Test_Name" name="testName"/> <field column="Create_Time" name="createTime"/> </entity> </document> </dataConfig>
- entity:需要从数据库中取出的数据,name为数据库表名,支持sql语句,支持多表查询。注意pk="id" 不能随便改 ,需要和schema.xml中的<uniqueKey>id</uniqueKey>匹配,否则会报错注[1] ,且<uniqueKey>id</uniqueKey>中的主键必须为String类型
- field:接受到的数据。列名column为数据库字段名(必须与数据库字段相同,区分大小写),name为索引库(必须和schema.xml)中field定义的name名字相同
- query属性为全量索引时查询用,其他的为增量索引用
注[1]:
注[2]:
Note the variable ${dataimporter.last_index_time} The DataImportHandler exposes a variable called last_index_time which is a timestamp value denoting the last time full-import 'or' delta-import was run。
内置变量${dataimporter.last_index_time}用来记录最后一次索引的时间(包括全量与增量),并保存在core~/conf/dataimport.properties文件中,该文件为自动创建(如果solr服务启动后请求索引后,未在相应核下找到该文件,说明DIH配置有误,可以通过solr后台logging查看异常日志)。这里dataimport.properties内容如下:
#Mon Mar 15 16:10:05 CST 2015 test_table.last_index_time=2015-03-15 16\:10\:04 last_index_time=2015-03-15 16\:10\:04
内置变量 “${dataimporter.request.length}”和 “${dataimporter.request.offset}”用来设置一个批次索引的数据表记录数
至此,DIH配置完成,在浏览器中输入请求:
全量索引 http://localhost:8080/solr/core1/dataimport?command=data-import&clean=false&commit=true 增量索引 http://localhost:8080/solr/core1/dataimport?command=delta-import&clean=false&commit=true 查看导入状态 http://localhost:8080/solr/core1/dataimport?command=status
- clean:选择是否要在索引开始构建之前删除之前的索引,默认为true
- commit:选择是否在索引完成之后提交。默认为true
- offset=0&length=100000 0到100000的数据创建索引,全量索引分批次索引用于数据量较大时,如果数据量较小,可以直接全部索引(未调通)
当然也可以通过
http://localhost:8080/solr
进入solr管理页面中相应索引库(如core1)中找到dataimport 点击执行,这样好处是还可以通过日志logging查看异常
修改数据源位置
定时重做索引
定时自动重建索引可以通过自己写程序实现,也可以通过solr-dataimportscheduler-1.0.jar包完成此功能,具体如下:
1、将dataimporter.properties(网上找的)放到solr.home/conf目录下,对应本机D:\solr\home\conf(conf文件夹是没有的,需要新建)注[3]
dataimporter.properties (重要)(不同于core/conf下的dataimport.properties,那个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=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
2、修改solr.war中WEB-INF/web.xml, 对应本机D:\solr\server\solr\WEB-INF\web.xml,在servlet节点前增加:
这个有点坑爹!这个类并不存在solr几个包中,需要额外导入solr-dataimportscheduler-1.1.jar(官方还不支持定期重建索引,所以只能通过修改过的第三方jar, 参考这里)
<listener> <listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class> </listener>
注[3]:
?看别人的资料说上面的dataimporter.properties位置可以随意放,只需要在solr.war中WEB-INF/web.xml中指定位置(还未测试,不清楚说的对不对)
<context-param> <param-name>autoDeltaImportConfPath</param-name> <param-value>/yourconfpath</param-value> </context-param>
常见异常
org.apache.solr.common.SolrException: Document is missing mandatory uniqueKey field: testId
参见注[1]
请加入数据库驱动jar包java.lang.RuntimeException: org.apache.solr.handler.dataimport.DataImportHandlerException: Could not load driver: com.mysql.jdbc.Driver Processing Document # 1
xxxx这个字段对应的column的字段名与数据库中的字段不一致(包括大小写)org.apache.solr.common.SolrException: [doc=454a2d2f-c199-483a-a8d2-7c49c1adbfe1] missing required field: xxxx
全量索引异常:data-config.xml中配置了批次索引,但创建索引的时候参数offset&length出现问题,我也未调通Exception while processing: test_table document : SolrInputDocument(fields: []):org.apache.solr.handler.dataimport.DataImportHandlerException: Unable to execute query: select * from test_table limit offset Processing Document # 1 at org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow(DataImportHandlerException.java:71) at org.apache.solr.handler.dataimport.JdbcDataSource$ResultSetIterator.<init>(JdbcDataSource.java:283)
出现这种错误是因为 dataimporter.properties配置文件的存放位置不对严重: 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:4994) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)