导入配置可参考官网:http://lucene.apache.org/solr/guide,http://lucene.apache.org/solr/guide/7_7/
1、数据准备(MySQL8.x数据库)
DROP TABLE IF EXISTS `dept`; CREATE TABLE `dept` ( `id` int(11) NOT NULL, `code` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, `name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, `remark` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; INSERT INTO `dept` VALUES (1, '01', '总裁办', '经营管理公司各项事务', '2019-09-28 21:19:42'); INSERT INTO `dept` VALUES (2, '02', '研发中心', NULL, '2019-09-28 22:02:55');
2、创建field,修改managed-schema文件,添加字段信息
<field name="code" type="string" uninvertible="true" indexed="true" stored="true"/> <field name="name" type="text_ik" uninvertible="true" indexed="true" stored="true"/> <field name="remark" type="text_ik" uninvertible="true" indexed="true" stored="true"/> <field name="createTime" type="pdate" uninvertible="true" indexed="true" stored="true"/>
也可以通过url添加,两种添加方式是等效的,url添加的字段会写入managed-schema文件中
3、修改solrconfig.xml文件,添加配置
<requestHandler name="/import" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">db-config.xml</str> </lst> </requestHandler>
4、在同目录创建db-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.cj.jdbc.Driver" url="jdbc:mysql://192.168.114.131:3306/zhi_test?characterEncoding=utf8" user="root" password="abc123"/> <document> <entity name="dept" query="select id,code,name,remark,create_time from dept" deltaQuery="select id from dept where create_time>'${dataimporter.last_index_time}'" deltaImportQuery="select * from dept where id='${dataimporter.delta.id}'"> <field column="create_time" name="createTime"/> </entity> </document> </dataConfig>
5、复制solr-7.7.2dist目录下的solr-dataimporthandler-7.7.2.jar和solr-dataimporthandler-extras-7.7.2.jar到solr-7.7.2/server/solr-webapp/webapp/WEB-INF/lib目录下
6、复制mysql驱动到solr-7.7.2/server/solr-webapp/webapp/WEB-INF/lib目录下
7、开始导入数据
1)全量导入数据。导入了原有的2条记录
2)增量导入数据。只导入新增的1条记录