solr(六)增量导入及定时自动更新

xiaoxiao2021-02-28  52

接着总结整理,接着来~

内容:

1.设置数据导入时的两种方式:增量导入(delta-import)和全量导入(full-import)的配置

2.定时自动增量更新设置

一:增量导入实现配置:

 

1.  导入jar包,将solr-dataimporthandler-5.3.1.jar和solr-dataimporthandler-extras-5.3.1.jar从solr-7.1/dist/文件夹下copy到solr-7.1/server/solr-webapp/webapp/WEB-INF/lib当中

 

2.  增加data-config中增量配置,如下:

 

<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/dbname" user="db_username" password="db_password"/>

·        数据源也可以配置在solrconfig.xml

·        属性type 指定了实现的类型。它是可选的。默认的实现是JdbcDataSource

·        属性 name  datasources的名字,当有多个datasources时,可以使用name属性加以区分

·        其他的属性都是随意的,根据你使用的DataSource实现而定。

·        当然你也可以实现自己的DataSource

 

我们做如下配置:

<dataConfig>     <dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/ceshi" user="root" password="root"/>     <document name="salesDoc">         <entity name="test"  pk="id"                 query="SELECT* from test"                 deltaQuery="selectid as id from test where update_time > '${dih.last_index_time}'"                 deltaImportQuery="select id,name,sex,age from test where id='${dih.delta.id}'">             <field name="id" column="id" />             <field name="textname" column="name"/>             <field name="sex" column="sex"/>             <field name="age" column="age"/>         </entity>     </document> </dataConfig>

query是获取全部数据的SQLdeltaImportQuery是获取增量数据时使用的SQLdeltaQuery是获取pkSQLparentDeltaQuery是获取父EntitypkSQL

 

注意:该处必须包含ID,否则不能进行

 

 多数据源

一个配置文件可以配置多个数据源。增加一个dataSource元素就可以增加一个数据源了。name属性可以区分不同的数据源。如果配置了多于一个的数据源,那么要注意将name配置成唯一的。

 

<dataSource type="JdbcDataSource" name="ds-1" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://db1-host/dbname" user="db_username" password="db_password"/> <dataSource type="JdbcDataSource" name="ds-2" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://db2-host/dbname" user="db_username" password="db_password"/>

使用为:

 

<entity name="one" dataSource="ds-1" ...>    .. </entity> <entity name="two" dataSource="ds-2" ...>    .. </entity>

 

 

3: managed-schema中添加

<!--  test data --> <!-- <field name="id" type="string" indexed="true" stored="true" />  --> <field name="textname" type="text_ik" indexed="true" stored="true" /> <field name="sex" type="string" indexed="true" stored="true" /> <field name="age" type="string" indexed="true" stored="true" />

 

4:在solrconfig.xml引入添加的data-config.xml文件

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">     <lst name="defaults">         <str name="config">./data-config.xml</str>     </lst> </requestHandler>

 

即可完成增量导入数据;

 

二:自动增量更新

 

1 除了将上述两个jar包导入,再将solr-dataimport-scheduler.jar同样导入(scheduler调度,日程安排

2 在webapps/solr/WEB-INF下web.xml配置监听。如下:

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

 

3 在solr-home目录下新建conf文件夹,在文件夹下新建dataimport.properties文件,在其里面设置自动增量及全量导入时间,如下:

################################################# #                                               # #       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 #  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=core1 #  solr server name or IP address #  [defaults to localhost if empty] #solr服务器的ip地址 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 #增量 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 #  [defaults to 30 if empty] #自动增量更新的时间间隔,单位为分钟,默认为30分。 interval=1 #  重做索引的时间间隔,单位分钟,默认7200,即5天; #  为空,为0,或者注释掉:表示永不重做索引 reBuildIndexInterval=43200 #  重做索引的参数 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=23:59:59

注意:不能存在空格,否则找不到执行的URL

 

 

 

转载请注明原文地址: https://www.6miu.com/read-2619345.html

最新回复(0)