solr5.5.4 添加mysql数据,实现同步更新

相关文章:第一篇要是安装部署第二篇如何定时同步mysql数据第三篇solr实战关键字查询全库

1.导入jar包。将目录E:\solr\test\solr-5.5.4\dist下的两个jar包,solr-dataimporthandler-5.5.4.jar  solr-dataimporthandler-extras-5.5.4.jar ,还又mysql驱动的jar导入E:\solr\test\apache-tomcat-8.5.20\webapps\solr\WEB-INF\lib下。

2.复制E:\solr\test\solr-5.5.4\server\solr\configsets\data_driven_schema_configs\conf,放到

3.E:\solr\test\solrhome\core1下,编辑E:\solr\test\solrhome\core1\conf\solrconfig.xml

添加<lib dir="../webapps/solr/WEB-INF/lib" regex=".*\.jar" /> ,位置如图,相对路径,相对的是tomcatbin路径,和上面一样的道理。然后搜索class="solr.SearchHandler",在此句上面添加

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">

        <lst name="defaults">

            <str name="config">data-config.xml</str>

        </lst>

   </requestHandler>

如图

4.在和solrconfig.xml的同目录项目添加文件data-config.xml

内容如下:

<?xml version="1.0" encoding="UTF-8"?>  

<dataConfig>  

<dataSource name="source1" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solrdata" user="root" password="root" batchSize="-1" />  

  <document>  

<entity name="goods" pk="id"  dataSource="source1"   

query="select * from  goods"  

 deltaImportQuery="select * from goods where id='${dih.delta.id}'"  

deltaQuery="select id from goods where updatetime> '${dataimporter.last_index_time}'">  

  

         <field column="id" name="id"/>  

        <field column="name" name="name"/>  

<field column="number" name="number"/>  

<field column="updatetime" name="updatetime"/>  

     </entity>  

  </document>  

</dataConfig>

其中deltaImportQuerydeltaQuery设置的内容是自动更新mysql数据到solr引擎中来所需要的。如果多个表再并列添加一个<entity 标签。

 

最后注释:否则后面可能启动报错

solrconfig.xml配置文件注释掉下面:

    <searchComponent name="elevator">

  <!-- pick a fieldType to analyze queries -->

  <str name="queryFieldType">string</str>

  <str name="config-file">elevate.xml</str>

  </searchComponent>

 

5.修改managed-schema(在同目录下面的)添加的内容如下图

内容为:

<field name="id" type="int" indexed="true" stored="true" required="true" multiValued="false" />  

<field name="name" type="string" indexed="true" stored="false"/>  

<field name="number" type="int" indexed="true" stored="false"/>  

<field name="updatetime" type="date" indexed="true" stored="true" />

6.solrhome文件夹里面创建conf文件,在conf里面创建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

#  修改成你所使用的core,我这里是我自定义了多个core,用逗号隔开

syncCores=testcore,big,core1

 

#  solr server name or IP address

#  [defaults to localhost if empty]

# 这个一般都是localhost不会变

server=localhost

 

#  solr server port

#  [defaults to 80 if empty]

#  安装solrtomcat端口,如果你使用的是默认的端口,就不用改了,否则改成自己的端口就好了

port=8080

 

#  application name/context

#  [defaults to current ServletContextListener's context (app) name]

#  这里默认不改

webapp=solr

 

#  URL params [mandatory]

#  remainder of URL

#  这里改成下面的形式,solr同步数据时请求的链接

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=/select?qt=/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

 

7.然后在E:\solr\test\apache-tomcat-8.5.20\webapps\solr\WEB-INF\lib添加

apache-solr-dataimportscheduler-1.1.jar,或者dataimportscheduler-1.2.jar,这是是别人修改过的。

下载链接:http://download.csdn.net/download/qq_14926389/9937657,有币的送几个,没有的q我吧

 8.在数据库中添加表,如下图

alter table goods modify  updatetime TIMESTAMP  NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,然后在客户端执行这句话,用户自动更新判别条件

然后重启tomcat,访问solr

说一solr 的操作,一半重启之后要在solr中重新加载一下配置文件

如下图,

最后这些qurey。点击去又一个excute的按钮,查询所有,得到如下结果,core1的数据11条,当你去插入一条数据,updatetime的数据设置为当前时间,重新执行查询,回发现多了一条数据

 

以下我的qq  1195748576

posted @ 2017-08-17 10:03  longtengdama  阅读(2698)  评论(0编辑  收藏  举报