Solr(二): 整合 MySQL

找到配置文件:

${solr/home}/collection1/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>

在 conf 下新建 data-config.xml

<dataConfig>
    <dataSource 
            type="JdbcDataSource" 
            driver="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8" 
            user="root" 
            password="" />
    <document name="documents">
        <entity name="user" pk="id"
            query="select * from user"
            deltaImportQuery="select * from user where id='${dataimporter.delta.id}'"
            deltaQuery="select id from user where last_modified > '${dataimporter.last_index_time}'"
            <field column="id" name="id" />
            <field column="username" name="username" />
        </entity>
    </document>
</dataConfig>

复制相关的 jar 包到 solr lib

cp /opt/solr/dist/solr-dataimporthandler-4.3.0.jar /opt/tomcat/webapps/solr/WEB-INF/lib	
cp /opt/solr/dist/solr-dataimporthandler-extras-4.3.0.jar /opt/tomcat/webapps/solr/WEB-INF/lib
cp /opt/solr/example/solr-webapp/webapp/WEB-INF/lib/mysql-connector-java-5.1.6.jar /opt/tomcat/webapps/solr/WEB-INF/lib

修改 schema.xml(其它的字段已经存在)

<field name="username" type="text_general" indexed="true" stored="true" />

MySQL 脚本

CREATE TABLE `user` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `username` varchar(50) DEFAULT NULL,
      `last_modified` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`)
);

INSERT INTO `user` (`id`, `username`, `last_modified`)
VALUES
(1001,'zhangsan','2013-06-27 13:01:44'),
(1002,'lisi','2013-06-28 14:00:22'),
(1003,'wangwu','2013-06-28 11:04:23');

批量导入(full-import):

http://localhost:8080/solr/dataimport?command=full-import&commit=true 

增量导入(delta-import):

http://localhost:8080/solr/dataimport?command=delta-import&commit=true

导入状态查询(status):

http://localhost:8080/solr/dataimport 

重新装载配置文件(reload-config):

http://localhost:8080/solr/dataimport?command=reload-config 

终止导入(abort):

http://localhost:8080/solr/dataimport?command=abort