Solr提供了丰富的数据导入接口,可以导入数据库表、xml、json、csv各种格式的数据信息。
大多数的应用程序将数据存储在关系数据库、xml文件中。对这样的数据进行搜索是很常见的应用。所谓的DataImportHandler提供一种可配置的方式向solr导入数据,可以一次全部导入,也可以增量导入。他可以实现能够读取关系数据库中的数据。通过可配置的方式,能够将数据库中多列、多表的数据生成solr文档能够通过solr文档更新solr提供通过配置文件就能够导入所有数据的能力能够发现并处理 由insert、update带来的变化(我们假定在表中有一个叫做“last-modified的列”)能够配置"完全导入"和"增量导入"的时间让读取xml文件,并建立索引成为可配置。能够将 其他的数据源(例如:ftp,scp,etc)或者其他格式的文档(Json,csv)以插件的形式集成到项目中。
导入mysql数据库
配置workspace/temp_info/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>
配置workspace/temp_info/conf/schema.xml添加
<field name="temp_id" type="string" indexed="true" stored="true" multiValued="false" />
<field name="temp_name" type="string" indexed="true" stored="true" />
<field name="temp_phone" type="string" indexed="true" stored="true" />
注释掉
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
修改
<uniqueKey>id</uniqueKey>
为
<uniqueKey>temp_id</uniqueKey>
增加文件workspace/temp_info/conf/data-config.xml文件内容为
<?xml version="1.0" encoding="utf-8"?>
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://ip:3306/test?useUnicode=true&characterEncoding=utf-8" temp="name" password="password"/>
<document >
<entity name="parteam_test_temp" pk="temp_id" query="select a.temp_id as temp_id,a.nick_name as temp_name from temp_info a">
<field column="temp_id" name="temp_id" />
<field column="temp_name" name="temp_name" />
</entity>
</document>
</dataConfig>