solr4 importdata

来源:互联网 发布:java中方法的概念 编辑:程序博客网 时间:2024/06/06 09:48

solr4 mysql导入数据创建索引

本文章主要描述solr4.10.3版本的创建索引的功能,solr前期配置参见上一篇文章

1.前期准备

1.按照上篇章博客步骤,将solr4.10.3版本环境配置好,solr配置地址

2.创建3张表作为数据源,体验一下多表查询及多值等能力
比如我在创建一个用户信息表,一个书本信息表,一个用户关联书本的中间表。中间表包含用户的id,书本的id字段

2.配置步骤。

1.我们需要准备好以下几个包,或者确认在tomcat-webapps-solr下面的lib里面有以下几个包
solr-dataimporthandler-4.10.3.jar
solr-dataimporthandler-extras-4.10.3.jar
以上两个包之前配置solr就已经放入。还需要放入以下两个jar包
mysql-connector-java-5.1.36.jar
solr-dataimportscheduler-1.1.jar

2.容器中的WEB-INF下solr的web.xml中添加监听

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

3.在core的同级目录下创建conf文件夹。里面存放dataimport.properties文件。**注意**conf在core 的同级目录,该目录下还有solr.xml配置文件。千万不要放错。本人环境存放地址E:\solr410\solrtomcat\solr\conf\dataimport.properties.文件内容如下所示

syncEnabled=1
syncCores=xmcore
server=127.0.0.1 (solr启动容器IP)
port=8180(solr启动容器端口号)
webapp=solr
params=/dataimport?command=delta-import&clean=false&commit=true
interval=1 (增量创建索引间隔)
reBuildIndexInterval=7200(全量创建索引间隔)
reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true
reBuildIndexBeginTime=18:25:00

4.最为重要和容易出错的一步
在solr core的conf下的solrconf.xml文件中
在大概820行左右存在name=”/dataimport” 请求配置。修改为:

 <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">    <lst name="defaults">      <str name="config">xmcore-data-config.xml</str>    </lst> </requestHandler>

xmcore-data-config.xml 文件在solrconf.xml同级目录下创建该文件。示例内容为:

<?xml version="1.0" encoding="UTF-8" ?><dataConfig><dataSource name="jdbc" driver="com.mysql.jdbc.Driver"    url="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=utf8"    user="dev" password="dev123"/><document>    <entity pk="ID"  dataSource="mydb" name="user" query="select  *  from user_info WHERE status=0 and isdelete=0 "        deltaQuery="select ID  from user_info where last_modify > '${dataimporter.last_index_time}'"        deletedPkQuery="select ID from user_info where isdelete=1"        deltaImportQuery="select * from user_info where ID='${dataimporter.delta.ID}'"  >        <field column="ID" name="id" />        <field column="name" name="name" />        <field column="age" name="age" />        <field column="phone" name="phone" />        <field column="address" name="address" />        <field column="status" name="status" />        <field column="last_modify" name="last_modify" />        <field column="isdelete" name="isdelete" />        <entity pk="ID"  dataSource="mydb" name="user_book" query="select t1.bookid, t2.name from user_book t1 ,book t2 where  t1.bookid =   t2.id   and   t1.USERID =  '${user.id}' " >        <field column="bookid" name="book_id" />        <field column="name" name="book_name" />        </entity>    </entity>               </document></dataConfig>

注意该文件的节点开始及结束格式。不能错误,不然就会读取该文件失败。该文件演示了带有中间表的多表关联查询及多值效果

**5.**schame.xml配置文件列

 <!--下面属性是用户表中心字段信息--> <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />  <field name="name" type="text_zh" indexed="true" stored="true"/> <field name="age" type="int" indexed="true" stored="true"/>  <field name="phone" type="text_general" indexed="true" stored="true"/>  <field name="address" type="text_zh" indexed="true" stored="true"/>  <field name="status" type="int" indexed="true" stored="true"/>  <field name="last_modify" type="date" indexed="true" stored="true"/>  <field name="isdelete" type="int" indexed="true" stored="true"/>  <!--下面两个属性是中间表与书本表中信息--> <field name="book_id" type="int" indexed="true" stored="true" multiValued="true" />  <field name="book_name" type="text_zh" indexed="true" stored="true" multiValued="true" /> 

6.验证全量与增量创建索引
http://localhost:8180/solr/#/xmcore/dataimport//dataimport下面存在entity。下拉列表是否存在user的选项,如果存在表明上面步骤的配置文件没有问题,否则则配置格式存在问题。
选择user。可以进行全量与增量导入的测试。看看是否成功创建索引。
注意 mysql设计的表中必须存在last_modify列,改列用来记录最后修改或者新增时间,用于增量创建索引。列名随意,作为第四部文件中deltaQuery中的条件。

7.可以修改数据库数据与last_modify。修改第三步dataimport.properties文件的增量创建索引时间。查看能够增量创建索引.

3.需要的包材料及全部配置好的solr环境,solrtomcat只需要解压,修改一下web.xml的solrhome地址即可以启动,并且具备同义词,分词,扩展词。可以定时增量与全量创建索引,只需要修改第3步的配置文件。地址:http://download.csdn.net/detail/duxiaomeng1986_2008/9147669 传送

4.注意,上面下载的包启动地址是http://localhost:8180/solr/ 注意端口号

0 0