solr7.3.1定时增量索引

    Solr7.3.1定时任务,增量索引

一.修改数据库配置文件

1.修改配置文件指定路径:/solrhome/core/solrconfig.xml

修改内容

<requestHandler name="/dataimport" 

    class="org.apache.solr.handler.dataimport.DataImportHandler"> 

    <lst name="defaults"> 

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

    </lst> 

 </requestHandler> 

2.创建配置文件:/solrhome/core/ delta-data-config.xml

添加内容:

<dataConfig> 

   <dataSource

           type="JdbcDataSource"

           driver="com.mysql.jdbc.Driver"

           url="jdbc:mysql://localhost:3306/mydb"

           user="root"

           password="root"/>

    <document> 

        <entity name="info" pk="id" 

                query="select id,infodesc,sort,name,user,last_update_time from info" 

                deltaImportQuery="select id,infodesc,sort,name,user,last_update_time from info where id='${dih.delta.id}'" 

                deltaQuery="select id from info where last_update_time > '${dih.last_index_time}'" 

                transformer="RegexTransformer"> 

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

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

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

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

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

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

        </entity> 

    </document> 

</dataConfig> 

3.定时任务指定字段

         deltaQuery="select id from info where last_update_time > '${dih.last_index_time}'"

         last_index_time字段为指定定时更新条件字段

4. 创建sql依赖配置文件

/solrhome/core/ conf/ dataimport.properties

二.创建定时任务配置文件

1.创建配置文件:solrhome/conf/dataimport.properties

2.添加内容:

################################################# 

#                                               # 

#       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 

syncCores=mysb

#  solr server name or IP address 

#  [defaults to localhost if empty] 

server=localhost

#  solr server port 

#  [defaults to 80 if empty] 

port=80

#  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&optimize=false&wt=json&indent=true&entity=info&verbose=false&debug=false&name=dataimport

#  schedule interval 

#  number of minutes between two runs 

#  [defaults to 30 if empty]  分钟 默认30分钟

interval=1

#  重做索引的时间间隔,单位分钟,默认7200,即1天;  

#  为空,为0,或者注释掉:表示永不重做索引 

reBuildIndexInterval=7200

#  重做索引的参数 

reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true&optimize=true&wt=json&indent=true&entity=info&verbose=false&debug=false

#  重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000; 

#  两种格式:2012-04-11 03:10:00 或者  03:10:00,后一种会自动补全日期部分为服务启动时的日期 

reBuildIndexBeginTime=2012-04-11 09:00:00

*3.注意:和一中3的sql配置文件路径不通

三.添加定时任务jar包

1.下载并添加jar包

下载solr-data-import-scheduler-1.1.2.jar或者apache-solr-dataimportscheduler-1.1.jar

         Jar包添加路径webapps/solr/WEB-INF/lib

我用的是:solr-data-import-scheduler-1.1.2.jar

2.添加任务监听:webapps/solr/WEB-INF/web.xml

<listener> 

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

</listener>

3. 定时任务配置:同二中内容

将solr-data-import-scheduler-1.1.2.jar 中 dataimport.properties 取出并根据实际情况修改,然后放到 solr_home/conf (不是solrhome/core/conf) 目录下面,solrhome/conf文件夹没有的话手动创建

四.启动

1.报错-版本错误

这是因为solr-data-import-scheduler-1.1.2.jar中的方法:

org.apache.solr.handler.dataimport.scheduler. SolrDataImportProperties

报错;

修改错误内容:

SolrResourceLoader loader = new SolrResourceLoader();

      logger.info("Instance dir = " + loader.getConfigDir());

      String configDir = loader.getConfigDir();

      configDir = SolrResourceLoader.normalizeDir(configDir);

2.错误-sql错误1

         字段错误:kdkdkdk

         java.lang.RuntimeException: org.apache.solr.handler.dataimport.DataImportHandlerException: Unable to execute query: select id,kdkdkdk from info where last_update_time > '' Processing Document # 1

3.错误-sql错误2

         条件错误:dih

deltaImportQuery="select id,infodesc,sort,name,user,last_update_time from info  where id='${dih.delta.id}'"

deltaQuery="select id from info where last_update_time > '${dih.last_index_time}'"

两个sql中的dih对应/solrhome/core/conf/dataimport.properties中:

         archives_bi.last_index_time=2018-06-25 10\:45\:05

dih不用修改为info,会自动对应,假如修改为info,则deltaQuery会查询条件last_update_time > '${dih.last_index_time}'会变为last_update_time > '',导致定时更新不成功,同理deltaImportQuery也是如此。

4.错误-连接错误

         数据库jar包要和数据库版本保持一致

5.错误-未知错误

         Exception while processing: test_table document : SolrInputDocument(fields: []):org.apache.solr.handler.dataimport.DataImportHandlerException: Unable to execute query: select * from test_table limit  offset  Processing Document # 1 

    at org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow(DataImportHandlerException.java:71) 

at org.apache.solr.handler.dataimport.JdbcDataSource$ResultSetIterator.<init>(JdbcDataSource.java:283) 

Exception while processing: test_table document : SolrInputDocument(fields: []):org.apache.solr.handler.dataimport.DataImportHandlerException: Unable to execute query: select * from test_table limit  offset  Processing Document # 1

at org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow(DataImportHandlerException.java:71)

at org.apache.solr.handler.dataimport.JdbcDataSource$ResultSetIterator.<init>(JdbcDataSource.java:283)

***全量索引异常:data-config.xml中配置了批次索引,但创建索引的时候参数offset&length出现问题,我也未调通,出现此问题在apache-solr-dataimportscheduler-1.1.jar中,未在solr-data-import-scheduler-1.1.2.jar中出现此问题***

6.错误-其他

还有其他一些问题,例如配置文件找不到等,就不说了

五.注意

此文件只介绍了,如何添加定时任务的增量索引内容,未介绍如何配置solr服务等内容。此服务使用的是继承Tomcat服务。

猜你喜欢

转载自www.cnblogs.com/milude0161/p/9228547.html