1 配置RequestHandler
<!-- DataImportHandler -->
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">db/db-data-config.xml</str>
</lst>
</requestHandler>
2 配置数据源和数据实体
包括全部导入和增量导入
<dataConfig>
<dataSource driver="net.sourceforge.jtds.jdbc.Driver" url="jdbc:jtds:sqlserver://IP;instance=SQLSRV2005;DatabaseName=CloudPlat" user="sa" password="sa" batchSize="100"/>
<document>
<entity name="article" pk="DocAutoId" query="select * from CP_InfoListPub order by DocAutoId"
deltaImportQuery="select * from CP_InfoListPubClone where DocAutoId=‘${dataimporter.delta.DocAutoId}‘"
deltaQuery="select DocAutoId from CP_InfoListPubClone where Doc_GatherDate > ‘${dataimporter.last_index_time}‘"
deletedPkQuery="select DocAutoId from CP_InfoListPubClone where DocAutoId=0"
transformer="ClobTransformer,HTMLStripTransformer,DateFormatTransformer" >
<field column="DocAutoId" name="id" />
<field column="Doc_Title" name="title" />
<field column="Doc_RegionName" name="region" />
<field column="Doc_TradeName" name="trade" />
<field column="Doc_Content" name="content" clob="true" stripHTML="true"/>
<field column="Doc_PubDate" name="update" dateTimeFormat="yyyy-MM-dd"/>
<field column="Doc_Url" name="link" />
</entity>
</document>
</dataConfig>
3 引入相关的文件,apache-solr-dataimporthandler-1.4.0.jar,apache-solr-dataimporthandler-extras-1.4.0.jar,mysql-connector-java-5.1.10.jar等
4 启动tomcat,访问http://localhost:8080/solr/dataimport?command=full-import 将数据全部导入solr服务器进行索引
访问http://localhost:8080/solr/dataimport?command=status可以查看运行状态
当修改data-config.xml 文件配置时运行http://localhost:8080/solr/dataimport?command=reload-config可以进行重新加载配置文件
如果想终止运行http://localhost:8080/solr/dataimport?command=abort