引言:
① 实现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"/>
二、全量索引和增量索引
-
全量索引
在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 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"
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}'"
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
deltaQuery查询出有更改过的id
deltaImportQuery根据id查询 【在此方法中,使用了数据库中的一个字段createTime来确定新创建的数据在创建索引之后添加的。真正的使用法欢迎知道的朋友评论说明,谢谢】
使用时发送url:http://localhost:8080/solr/new_core/deltaimport?command=delta-import
注意:以上的参数说明
pk为主键和managed-schema中配置的<uniqueKey>id</uniqueKey>对应。
column对应数据库中的字段
name对应managed-schema中的name
url中的参数:
command:full-import/deltaimport 全量索引/增量索引
commit:选择是否在索引完成之后提交。默认为true
clean:选择是否要在索引开始构建之前删除之前的索引,默认为true
optimize:是否在索引完成之后对索引进行优化。默认为true
debug:是否以调试模式运行,适用于交互式开发(interactive development mode)之中。
<document>
<!--
entity就是一张表对应的实体,pk是数据库主键,与schema.xml中的uniquekey无关,query是SQL查询语句
field对应一个字段,column是数据库里的column名,后面的name属性对应着Solr的Filed的名字
Solr本身有一个默认值last_index_time,记录最后一次做full import或者是delta import(增量导入)的时间,这个值存储在文件conf目录的dataimport.properties文件中。
query:查询数据库表符合记录数据
deltaQuery:增量索引查询主键ID 注意这个只能返回ID字段
deltaImportQuery:增量索引查询导入的数据
增量索引的原理是从数据库中根据deltaQuery指定的SQL语句查询出所有需要增量导入的数据的ID号。
然后根据deltaImportQuery指定的SQL语句返回所有这些ID的数据,即为这次增量导入所要处理的数据。
核心思想是:通过内置变量“${dih.delta.id}”和 “${dataimporter.last_index_time}”来记录本次要索引的id和最近一次索引的时间。
如果需要关联子表查询,可能需要用到parentDeltaQuery
deletedPkQuery:增量索引删除主键ID查询 注意这个只能返回ID字段
如果业务中还有删除操作,可以在数据库中加一个isDeleted字段来表明该条数据是否已经被删除,这时候Solr在更新index的时候,可以根据这个字段来更新哪些已经删除了的记录的索引。
这时候Solr进行增量索引的时候,就会删除数据库中isDeleted=0的数据的索引。
-->
<entity dataSource="ds" name="AAAROLE" pk="id"
query="SELECT * FROM aaa_role "
deltaImportQuery="select * from aaa_role where id='${dih.delta.id}'"
deltaQuery="SELECT id FROM aaa_role where update_date > '${dataimporter.last_index_time}'"
deletedPkQuery="select id from aaa_role where isDeleted=0" >
<field column="id" name="ID" />
<field column="yewuid" name="YEWUID"/>
<field column="name" name="NAME"/>
<field column="isDeleted" name="DATADELETED"/>
<field column="enname" name="ENNAME"/>
<field column="update_date" name="UPDATEDATE" />
</entity>
</document>