引言:
① 实现MySQL(Oracle)表数据全量索引和增量索引,基于Solr DIH组件实现起来比较简单,只需要重复使用Solr的DIH(Data Import Handler)组件,对data-config.xml进行简单的修改即可。Solr DIH组件的实现类为org.apache.solr.handler.dataimport.DataImportHandler,在Solr的solrconfig.xml中配置两个handler。
② 实现定时增量索引,使用solr-dataimporthandler-scheduler配置。
一、在managed-schema文件中配置用到的字段名称
<field name="nickName" type="text_ik" indexed="true" stored="true"/>
二、全量索引和增量索引
1、全量索引
在solr_home\solr\new_core\conf\solrconfig.xml文件中增加
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
在solr_home\solr\new_core\conf目录下新建data-config.xml,添加:
<dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/dbName" user="root" password="123456"/> <document name="user"> <entity name="user" pk="id" query="select user_id as id,nick_name from user"> <field column="user_id" name="id" /> <field column="nick_name" name="nickName" /> </entity> </document> </dataConfig>
使用时发送url:http://localhost:8080/solr/new_core/dataimport?command=full-import&commit=true&clean=true
2、增量索引
在solr_home\solr\new_core\conf\solrconfig.xml文件中增加
<requestHandler name="/deltaimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">delta-data-config.xml</str> </lst> </requestHandler>
在solr_home\solr\new_core\conf目录下新建delta-data-config.xml,添加:
<dataConfig> <dataSource name="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/dbName" user="root" password="123456"/> <document name="user"> <entity name="user" pk="id" dataSource="JdbcDataSource" query="select user_id, nick_name from user" deltaImportQuery="select user_id as id, nick_name from user where user_id = '${dih.delta.id}'" deltaQuery="select user_id as id from user where create_time > '${dih.last_index_time}'" deletedPkQuery="select user_id as id from user where is_delete = 1" transformer="RegexTransformer"> <field column="user_id" name="id" /> <field column="nick_name" name="nickName" /> </entity> </document> </dataConfig>
${dih.delta.id} 和 ${dih.last_index_time} 是内置函数。在 dataimport.properties 中会记录id和最后添加索引的时间
#Mon Dec 12 17:16:29 CST 2016 last_index_time=2016-12-12 17:16:29 user.last_index_time=2016-12-12 17:16:29
dataSource 配置说明:
name:dataSource名称
driver:jdbc驱动名称(必需)
url:jdbc链接(必需)
user:用户名
password:密码
doucment 节点配置说明:用来配置如何从数据库导入数据构建document对象, 主要有一个或多个即实体组成。
name:节点名称
entity 配置说明:
name:实体名称,对应managed-schema中的name
processor:只有当datasource不是RDBMS时才是必需的。默认值是SqlEntityProcessor
rootEntity:默认情况下,document元素下就是根实体了,如果没有根实体的话,直接在实体下面的实体将会被看做根实体。对于根实体对应的数据库中返回的数据的每一行,solr都将生成一个document。
dataSource:dataSource名称
pk:entity的主键,它是可选的,但使用“增量导入”的时候是必需。managed-schema中配置的<uniqueKey>id</uniqueKey>对应。
transformer:转换器将会被应用到这个entity上,详情请浏览transformer部分。
query:查询符合条件的记录
deltaQuery:此查询只对增量导入起作用,而且只能返回ID值(增加,修改,删除操作)
deletedPkQuery:此操作值查询那些数据库里伪删除的数据的ID(即is_delete标识为1的数据) solr通过它来删除索引里面对应的数据
deltaImportQuery:增量导入数据,此查询是获取deltaQuery和deletedPkQuery的ID,然后把其全部数据获取,根据获取的数据 ,对索引库进行更新操作,可能是删除,添加,修改 。
parentDeltaQuery:获取父Entity的pk的SQL
field 配置说明:
column:数据库查询列名称,对应数据库中的字段
name:managed-schema中的字段
使用Url发送:http://localhost:8080/solr/new_core/deltaimport?command=delta-import
url中的参数:
command:full-import/deltaimport 全量索引/增量索引
commit:选择是否在索引完成之后提交。默认为true
clean:选择是否要在索引开始构建之前删除之前的索引,默认为true
optimize:是否在索引完成之后对索引进行优化。默认为true
debug:是否以调试模式运行,适用于交互式开发(interactive development mode)之中。
三、定时增量索引
定时自动重建索引可以通过自己写程序实现,也可以通过solr-dataimportscheduler.jar包完成此功能【源码】【solr6.X使用jar】,具体如下:
Download JAR(DHIS): http://code.google.com/p/solr-data-import-scheduler/
1、将dataimporter.properties【配置文件】放到solr_home/conf目录下,对应本机solr_home\conf(conf文件夹是没有的,需要新建)
################################################# # # # 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 # 修改成你所使用的core syncCores=new_core # solr server name or IP address # [defaults to localhost if empty] server=localhost # solr server port # [defaults to 80 if empty] port=8089 # 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=true # schedule interval # number of minutes between two runs # [defaults to 30 if empty] # 调度执行时间(分钟) interval=1 # 重做索引的时间间隔,单位分钟,默认7200,即5天; # 为空,为0,或者注释掉:表示永不重做索引 reBuildIndexInterval=7200 # 重做索引的参数 reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true # 重做索引时间间隔的计时开始时间,第一次真正执行的时间 = reBuildIndexBeginTime + reBuildIndexInterval * 60 * 1000; # 两种格式:2016-012-11 14:10:00 或者 03:10:00,后一种会自动补全日期部分为服务启动时的日期 reBuildIndexBeginTime=14:05:00
2、修改tomcat下solr中WEB-INF/web.xml, 在servlet节点前增加:
<listener> <listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class> </listener>
注意:使用第三方的jar需要注意<listener-class>中的包名。