Solr5中如何增量索引MySQL数据库表中的数据,这个问题之前有某个童鞋问过我,今天午休时间就腾空更新篇博客,希望能帮助到你们。
为了测试方便,我首先从京东网站弄了点测试数据,如图:
这里要声明下,我不是在给京东商城打广告哈,仅仅是随便找个网站弄点测试数据,这部分工作全是我无聊手动插入MySQL数据库中的,如图:
建表SQL以及测试数据,我待会儿会上传到底下的附件里。然后你需要在solrconfig.xml配置文件中启用全量导入和增量导入请求处理器,如图:
然后分别配置你的data-config.xml和delta-data-config.xml配置文件,具体配置如图:
<dataConfig> <dataSource name="jdbcDataSource" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8" user="root" password="1234"/> <document name="mobile-doc"> <entity dataSource="jdbcDataSource" name="mobile" query="select * from mobile"> <field column="id" name="id"/> <field column="pname" name="productName"/> <field column="price" name="price"/> <field column="color" name="color"/> <field column="brand" name="brand"/> <field column="wlan" name="wlan"/> </entity> </document> </dataConfig>
<dataConfig> <dataSource name="jdbcDataSource" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8" user="root" password="1234"/> <document name="mobile-doc"> <entity dataSource="jdbcDataSource" name="mobile" query="select * from mobile" deltaImportQuery="select * from mobile where id= ${dih.delta.id}" deltaQuery="select id from mobile where last_update_time > '${dih.last_index_time}'"> <field column="id" name="id"/> <field column="pname" name="productName"/> <field column="price" name="price"/> <field column="color" name="color"/> <field column="brand" name="brand"/> <field column="wlan" name="wlan"/> </entity> </document> </dataConfig>
query:表示一个查询SQL,根据你提供的查询SQL来做全量导入的
deltaQuery:表示一个查询SQL语句,会根据主键id去查询这条数据,根据比对最后一次更新时间,如果最后一次更新时间大于deltaimport.properties配置文件中记录的last_index_time时间,那表示该数据更新过了,需要做增量导入,每次不管是全量导入还是增量导入,deltaimport.properties配置文件中的last_index_time时间都会更新,deltaimport.properties配置文件若不存在也会自动创建,不需要你手动创建。
deltaImportQuery:表示一个SQL语句,即如何去加载当前数据,一般是根据主键id去查询。
然后你需要配置下schema.xml定义一些域,如图:
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="productName" type="text_ik" indexed="true" stored="true" omitNorms="true" multiValued="false"/> <field name="price" type="double" indexed="true" stored="true" /> <field name="color" type="string" indexed="true" stored="true" sortMissingLast="true"/> <field name="brand" type="string" indexed="true" stored="true" sortMissingLast="true"/> <field name="wlan" type="text_ik" indexed="true" stored="true" /> <uniqueKey>id</uniqueKey>
最后重启你的Tomcat开始测试,如图:
先做次全量导入
全量导入成功后,请手动往数据库表里添加一条测试数据用于增量索引测试,如图:
然后请切换到增量导入,如图:
接着看图:
OK,大功告成,如果你在实践过程中出现任何问题,请联系我,solr_home整个目录以及测试数据库SQL我都会打包上传到底下的附件里,供你们参考。lib目录下依赖的jar我没上传,因为Jar包体积太大,ITeye上传不了,如果你找不到jar包,请参阅我的这篇博客
《跟益达学Solr5之索引文件夹下所有文件》,在那篇博客里有提供jar包的百度网盘下载地址,特此提醒!!!
如果你还有什么问题请加我Q-Q:7-3-6-0-3-1-3-0-5,
或者加裙
一起交流学习!