Apache Solr学习 第七篇 实现solr定时数据同步

1、准备环境

  已安装配置好solr和mysql数据。java

    jar包:solr-dataimportscheduler-1.1.1.jar。mysql

    原理:看了下jar的大概逻辑,就是在solr启动的时候,启动一个定时任务,以http方式访问solr 的dataimport接口。web

2、配置

    1.data-config.xml配置sql

<?xml version="1.0" encoding="UTF-8"?>
  <dataConfig>
   <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/moxiangbooks" user="root" password="www1928..com" batchSize="-1"/>
     <document name="books">
        <entity name="books" pk="bid" query="SELECT A.bid, A.bname, A.author, A.currPrice, A.discount, A.press, A.publishtime, A.edition, A.pageNum, A.wordNum, A.printtime, A.booksize, A.paper, A.cid, A.image_w as imageW, A.image_b as imageB, A.orderBy, price FROM books A" deltaImportQuery="SELECT A.bid, A.bname, A.author, A.currPrice, A.discount, A.press, A.publishtime, A.edition, A.pageNum, A.wordNum, A.printtime, A.booksize, A.paper, A.cid, A.image_w as imageW, A.image_b as imageB, A.orderBy, price FROM books A WHERE A.bid = '${dih.delta.bid}'" deltaQuery="select bid from books" >
            <field column="bid" name="bid" />
            <field column="bname" name="bname" />
            <field column="author" name="author" />
            <field column="price" name="price" />
            <field column="currPrice" name="currPrice" />
            <field column="discount" name="discount" />
            <field column="press" name="press" />
            <field column="publishtime" name="publishtime" />
            <field column="edition" name="edition" />
            <field column="pageNum" name="pageNum" />
            <field column="wordNum" name="wordNum" />
            <field column="printtime" name="printtime" />
            <field column="booksize" name="booksize" />
            <field column="paper" name="paper" />
            <field column="cid" name="cid" />
            <field column="imageW" name="imageW" />
            <field column="imageB" name="imageB" />
            <field column="orderBy" name="orderBy" />
        </entity>
    </document>
配置参数说明:
  • driver:数据库驱动
  • url:数据库访问地址
  • query:全量更新SQL
  • deltaQuery: 增量更新SQL
  • ${dih.delta.bid}表的主键
2.dataimport.properties配置

      有的solr-dataimportscheduler-1.1.1.jar自带该文件,有的没有,若是是Tomcat启动solr,在/solrhome下新建文件夹conf,并在conf下新建dataimport.properties,数据库

若是是用solr自带的jetty启动,则在solr-6.6.6\server\solr目录下新建conf,注意,在每个core对应的conf文件夹也有dataimport.properties,可是和咱们新建不是一个apache

文件,dataimport.properties内容以下:json

 ################################################# # # # dataimport scheduler properties # # # ################################################# # server BASIC authorization by userName and password # format:userName:password # if no server BASIC authorization,please set: # authorizationMsg= #authorizationMsg=userName:password # to sync or not to sync # 同步执行更新 syncEnabled=1 # # solr中对应得core名,可写多个,多个时用逗号“,”分隔 # syncCores=books # solr server name or IP address # [defaults to localhost if empty] server=localhost # solr服务的端口号 # [defaults to 80 if empty] port=8983 # application name/context # [defaults to current ServletContextListener's context (app) name] webapp=solr # URL params [mandatory] # remainder of URL #增量更新对应的访问参数,注意/dataimport?地址不一样版本sorl了能地址名不一样,具体可登陆solr管理后台##查看dataimport的具体访问ULR params=/dataimport?command=delta-import&clean=false&commit=true&optimize=false&wt=json&indent=true&verbose=false&debug=false # schedule interval # number of minutes between two runs # 定时任务执行增量更新的间隔,不能为0.5这样的数,默认设置为1分钟 interval=1 # 重作索引的时间间隔,单位分钟,默认7200,即5天; # 为空,为0,或者注释掉:表示永不重作索引,即全量更新的时间间隔 reBuildIndexInterval=7200 # 重作索引的参数,即全量更新 reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true&optimize=true&wt=json&indent=true&verbose=false&debug=false # 重作索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000; # 两种格式:2012-04-11 03:10:00 或者 03:10:00,后一种会自动补全日期部分为服务启动时的日期 reBuildIndexBeginTime=03:10:00
3.全量更新和增量更新
  • 全量更新:即删除原来core下的全部索引数据,并从数据库中从新导入
  • 增量更新:根据/core/conf/dataimport.properties记录的时间,将查到的数据作对比,只更新有改变的索引会更新
  4.将solr-dataimportscheduler-1.1.1.jar放入到WEB-INF下的lib目录中
  5.在WEB-INF/web.xml中配置监听器
<listener> 
  <listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class>
</listener>

配置完成启动项目,一分钟之后就能够去查看数据更新了app

3、遇到的问题

涉及问题,启动时候报了webapp

ERROR: Solr at http://localhost:8983/solr did not come online within 30 seconds!

查看solr启动日志,发现测试

java.lang.NoSuchMethodError: org.apache.solr.core.SolrResourceLoader.getInstanceDir()Ljava/lang/String;
    at org.apache.solr.handler.dataimport.scheduler.SolrDataImportProperties.loadProperties(SolrDataImportProperties.java:37)
    at org.apache.solr.handler.dataimport.scheduler.BaseTimerTask.reloadParams(BaseTimerTask.java:57)
    at org.apache.solr.handler.dataimport.scheduler.BaseTimerTask.<init>(BaseTimerTask.java:39)

      像这种报错通常都是jar包不兼容引发的,打开了jar看了看,发现有solr-core的有个类的构造方法发生了改变,因此反编译修改了solr-dataimportscheduler-1.1.1.jar,

修改后已测试好使, 修改后的jar地址。

      连接:https://pan.baidu.com/s/15K4ZKIDjVQkgR5IoCDySpg

      提取码:vf3u

solr涉及资源已所有分享网盘,喜欢的给文章点个赞,谢谢你们了
相关文章
相关标签/搜索