solr 数据同步,全量、增量方式
DIH全量同步(全表数据)(一般做第一次数据同步)
首先创建对应的数据库表
solr配置
将solr现有数据清空
cd /usr/local/solr/example/solr/collection1/data && rm -rf * && mkdir index && mkdir tlog
添加数据源配置(@)
vim /usr/local/solr/example/solr/collection1/conf/solrconfig.xml
添加配置信息:
data-config.xml
在同目录下创建data-config.xml文件,并添加源信息(@)
vim /usr/local/solr/example/solr/collection1/conf/data-config.xml
添加配置如下:
添加数据同步所需依赖jar到tomcat中solr
cd /usr/local/solr/dist && cp solr-dataimporthandler-4.10.3.jar solr-dataimporthandler-extras-4.10.3.jar /usr/local/tomcat/webapps/solr/WEB-INF/lib/
同时把mysql驱动添加到/usr/local/tomcat/webapps/solr/WEB-INF/lib/中
将需要同步的数据库表字段添加到schema.xml中,如果已存在,则无需
进行数据库访问授权操作,如果已授权则略过
GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY '123456' WITH GRANT OPTION; flush privileges;
时间同步,mysql数据源服务器时间跟solr服务器时间一致
date -s "2016-04-17 21:15:00" 设置时间 mysql数据源服务器时间一定大于等于solr服务器时间,否则solr检测不到mysql时间变化了(认为是旧数据,增量同步来说)
启动tomcat即可。
浏览器访问http://ip:port/solr,点击Dataimport,Command选择full-import,Entity选择要同步的表,Custom Parameters输入data-config.xml中需要的参数(${dataimport.request.id}),点击Execute执行即可。
solr-HID全量.png
DIH增量同步(新增数据)(常用模式)
只需要对上面全量导入solr配置中带有(@)标示的配置进行修改即可
添加数据源配置(@)
vim /usr/local/solr/example/solr/collection1/conf/solrconfig.xml
添加配置信息:
delta-data-config.xml
```
在同目录下创建delta-data-config.xml文件,并添加源信息(@)
vim /usr/local/solr/example/solr/collection1/conf/delta-data-config.xml
添加配置如下:
query="SELECT id,name,price,url,last_modified FROM user WHERE id >= ${dataimporter.request.id}"
deltaImportQuery="SELECT * FROM user WHERE id = ${dih.delta.id}" deltaQuery="SELECT id FROM user where last_modified > '${dataimporter.last_index_time}' ">
```
id一致
solr_pk.png
last_modified字段要在schema.xml文件中配置,
query : 首先查询出所有满足id > ${dataimporter.request.id} 的数据
deltaQuery : 从query的数据中查询所有last_modified大于上一次同步${dataimporter.last_index_time}时间的id(即为需要增量同步的数据),该id = ${dih.delta.id}
deltaImportQuery :根据 deltaQuery 返回的id查找所有信息,为增量同步提供数据源
${dih.delta.id} : 记录本次要索引的id
${dataimporter.last_index_time} : 最后一次索引的时间,即上一次同步的时间
注意:last_modified字段很重要,为增量同步提供数据变更依据,一定为时间类型,具体字段名可自定义,同时也要注意solr服务器与mysql服务器时间同步问题
配置完毕,启动tomcat即可。
最后访问,浏览器访问http://ip:port/solr,点击Dataimport,Command选择full-import,Entity选择要同步的表,Custom Parameters输入data-config.xml中需要的参数(${dataimport.request.id}),点击Execute执行即可。
solr-HID增量.png
全量、增量也可以同时配置。
备注
内存溢出
在使用 DIH 时,容易报内存溢出错误。可以通过设置 jvm 大小来解决。设置方法如下:
在tomcat\bin\catalina.sh 加入SET JAVA_OPTS=-Xms128m -Xmx1024m 配置 这里设置的是1024M,根据情况可以适量增大
```
定时增量同步(全量不支持定时同步--没必要)
Solr官方提供了很强大的Data Import Request Handle
将apache-solr-dataimportscheduler-1.0.jar 拷贝到/usr/local/tomcat/lib/目录下
修改solr.war中的web.xml文件
cd /usr/local/tomcat/webapps/solr/WEB-INF/web.xml
添加以下配置信息:
org.apache.solr.handler.dataimport.scheduler.ApplicationListener
在tomcat的solr.war中web.xml配置的solr.home下创建conf文件夹,并新建文件dataimport.properties,配置定时任务依赖信息
solr-tomcat配置目录.png
cd /usr/local/solr/example/solr && mkdir conf && cd conf && vim dataimport.properties
dataimport.properties文件信息如下:
# dataimport.properties example
#
# From this example, copy everything bellow "dataimport scheduler properties" to your
# dataimport.properties file and then change params to fit your needs
#
# IMPORTANT:
# Regardless of whether you have single or multiple-core Solr,
# use dataimport.properties located in your solr.home/conf (NOT solr.home/core/conf)
# For more info and context see here:
# http://wiki.apache.org/solr/DataImportHandler#dataimport.properties_example
#Tue Jul 21 12:10:50 CEST 2010
last_index_time=2016-04-19 15\:28\:37
user.last_index_time=2016-04-19 15\:28\:37
#################################################
# #
# dataimport scheduler properties #
# #
#################################################
#
# http://192.168.58.144:8080/solr/collection1/deltaimport?command=delta-import&clean=false&commit=ture
# 以下参数对应参数
# http://server:port/webapp/syncCores/params
#
# 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=collection1
#
# 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
#增量url
params=/deltaimport?command=delta-import&clean=false&commit=ture
#
# schedule interval
# number of minutes between two runs
# [defaults to 30 if empty]
# 定时更新间隔时间,单位分钟
interval=1
配置完毕,启动tomcat即可。
定时增量备注: 如果想支持定时重做索引,只需要将上面引入的jar更换为solr-dataimportscheduler-1.1.jar,在dataimport.properties中添加三行配置即可,实质就是全量同步。
# 重做索引的时间间隔,单位分钟,默认7200,即5天;
# 为空,为0,或者注释掉:表示永不重做索引
reBuildIndexInterval=1
#
# 重做索引的参数
reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true
#
# 重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;
# 两种格式:2016-04-19 05:00:00 或者 05:00:00,后一种会自动补全日期部分为服务启动时的日期
reBuildIndexBeginTime=05:00:00
相关文件如下:
[solr-dataimportscheduler-1.1](C:\Users\ljdo\Desktop\笔记\file\solr\solr-dataimportscheduler-1.1.jar)
[solr-dataimportscheduler-1.1-source](C:\Users\ljdo\Desktop\笔记\file\solr\solr-dataimportscheduler-1.1-source.zip)
[dataimport.properties.zip](C:\Users\ljdo\Desktop\笔记\file\solr\dataimport.properties.zip)
[apache-solr-dataimportscheduler-1.0](http://solr-dataimport-scheduler.googlecode.com/files/apache-solr-dataimportscheduler-1.0.jar)
[apache-solr-dataimportscheduler-1.0-with-source](http://solr-dataimport-scheduler.googlecode.com/files/apache-solr-dataimportscheduler-1.0-with-source.jar)
作者:逐暗者 *(转载请注明出处)*