solr5.5配置定时全量、增量同步索引mysql数据

上篇文章中讲了如何下载solr、安装solr、启动、增加中文分词器、简单查询等传送门,这篇文章我们来配置solr定时全量和增量同步mysql数据。

1、先看数据库表

2、打开E:\solrHome\core1\conf文件夹下的data-config.xml文件,这个文件中主要配置数据源等信息,内容如下

<dataConfig>
    <dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8" user="用户名" password="密码" />
    <document>
		<!-- name属性,就代表着一个文档,可以随便命名 -->
        <!-- query是一条sql,代表在数据库查找出来的数据 -->
        <entity name="product" pk="id" 
				query="select id,name,catalog,catalog_name,price,description,picture from products where isdelete = 1" 
				deltaQuery="select id from products where createTime > '${dataimporter.last_index_time}'" 
				deletedPkQuery="select id from products where isdelete = 0" 
				deltaImportQuery="select id,name,catalog,catalog_name,price,description,picture from products where id = '${dataimporter.delta.id}'"
				>
			<!-- 
			query查询是指查询出表里所有的符合条件的数据,因为笔者测试的有删除业务,所以  
			where后面有一个限定条件isdelete=1,意思为查询未被删除的数据(注意这个query查询只对第一次全量导入有作用,对增量导入不起作用)  
			
			deltaQuery的意思是,查询出所有经过修改的记录的id  
			可能是修改操作,添加操作,删除操作产生的  
			(此查询只对增量导入起作用,而且只能返回id值)
			
			deletedPkQuery此操作值查询那些数据库里伪删除的数据的id(即isdelete标识为0的数据)  
			solr通过它来删除索引里面对应的数据  
			(此查询只对增量导入起作用,而且只能返回id值)
			
			deltaImportQuery次查询是获取以上两步的id,然后把其全部数据获取,根据获取的数据  
			对索引库进行更新操作,可能是删除,添加,修改  
			(此查询只对增量导入起作用,可以返回多个字段的值,一般情况下,都是返回所有字段的列)
			-->
            <!-- 每一个field映射着数据库中列与文档中的域,column是数据库列,name是solr的域(必须是在managed-schema文件中配置过的域才行) -->
            <field column="id" name="id"/>
            <field column="name" name="product_name"/>
            <field column="catalog" name="product_catalog"/>
            <field column="catalog_name" name="product_catalog_name"/>
            <field column="price" name="product_price"/>
            <field column="description" name="product_description"/>
            <field column="picture" name="product_picture"/>
        </entity>
    </document>
</dataConfig>

3、在solrHome文件夹下新建文件夹conf,将E:\solrHome\core1\conf下的dataimport.properties文件复制到solrHome

目录中的conf下(注意不是core1下的conf文件夹),将里面内容清空,重新配置如下:

#  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,我这里是我自定义的core1    
syncCores=core1
#  solr server name or IP address    
#  [defaults to localhost if empty]    
#  这个一般都是localhost不会变      
server=localhost
#  solr server port    
#  [defaults to 80 if empty]    
#  安装solr的tomcat端口,如果你使用的是默认的端口,就不用改了,否则你懂的      
port=18080
#  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     
#  [defaults to 30 if empty]    
#  这里是设置定时任务的,单位是分钟,也就是多长时间你检测一次数据同步,根据项目需求修改      
#  开始测试的时候为了方便看到效果,时间可以设置短一点,我这是60秒  
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
4、将solr下dist目录中的solr-dataimporthandler-5.5.4.jar和solr-dataimporthandler-extras-5.5.4.jar两个jar包复制到

tomcat\webapps\solr\WEB-INF\lib中,同时在网上下载apache-solr-dataimportscheduler-1.0.jar下载,下载后同样将jar包

放到tomcat\webapps\solr\WEB-INF\lib中。

5、更改tomcat\webapps\solr\WEB-INF下的web.xml文件,新增监听如下:

<listener>
	<listener-class>
	org.apache.solr.handler.dataimport.scheduler.ApplicationListener
	</listener-class>
</listener>
6、重启tomcat启动solr就可以了。大家一起相互学习,相互进步.

猜你喜欢

转载自blog.csdn.net/white_ice/article/details/79026546