关于solr:Solr-811入门教程3数据库导入数据

20次阅读

共计 2910 个字符,预计需要花费 8 分钟才能阅读完成。

数据库导入数据

生产环境下咱们个别须要从数据库中导入数据。

筹备测试数据

建一个 solrtest 的表

CREATE TABLE `solrtest` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(255) DEFAULT NULL COMMENT '姓名',
  `age` tinyint(3) DEFAULT NULL COMMENT '年龄',
  `description` varchar(1000) DEFAULT NULL COMMENT '介绍',
  `createTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创立工夫',
  `updateTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新工夫',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

导入几条数据

insert  into `solrtest`(`id`,`name`,`age`,`description`,`createTime`,`updateTime`) values 
(1,'张三',21,'大家好,欢送大家来到这里','2022-09-26 10:23:21','2022-09-26 10:23:24'),
(2,'李四',22,'欢送欢送','2022-09-26 10:23:27','2022-09-26 10:23:29'),
(3,'王五',23,'热烈欢迎','2022-09-26 10:23:37','2022-09-26 10:23:39');

配置导入处理器

官网文档上说,数据导入处理器 9.0 就会移除。这个性能将被合并到第三方插件 dataimporthandler 中。

The Data Import Handler is deprecated and will be removed in 9.0.

This functionality is being migrated to a new 3rd party plugin available at https://github.com/rohitbemax…

See the section Package Manager for information about Solr’s plugin framework.

—— 以上内容摘自官网文档:https://solr.apache.org/guide…

solrconfig.xml 中存在一些 requestHandler,首先查看solrconfig.xml 中是否存在名字为 dataimportrequestHandler
通过查问,如果没有发现名字为 dataimportrequestHandler,则须要手动增加。为了当前便于保护此文件,咱们就在 requestHandler 最初,约为 1296 行处,增加如下内容:

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
        <str name="config">db-data-config.xml</str>
    </lst>
</requestHandler>

配置数据库导入文件

而后配置 db-data-config.xml 文件,上面以 mysql 为例,将 mysqljar包增加到 server\solr-webapp\webapp\WEB-INF\lib 目录或 server\lib\ext 目录下,留神先创立对应的表。
其中 updateTimestamptimeStamp格局的字段,name 字段转换为 solr 中的 dd 字段。

~$ cd solr-8.11.2/server/solr/mytest/conf
~$ touch db-data-config.xml
<dataConfig>
    <dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/test" user="root" password="123" />
    <document>
        <!-- deltaQuery 返回值为 id 即 ${dih.delta.id}中的 id,这两处名称须要统一 --> 
        <entity name="test" pk="id"
            query="select id,name as dd,age,description,createTime,updateTime from solrtest"
            deltaImportQuery="select id,name as dd from solrtest where id='${dih.delta.id}'"deltaQuery="select id from solrtest where updateTimestamp > STR_TO_DATE('${dih.last_index_time}','%Y-%m-%d %T')">
            <!-- name 字段转换为 solr 中的 dd 字段 -->
            <field column="name" name="dd"/>
        </entity>
    </document>
</dataConfig>
  • query 是获取全副数据的 SQL
  • deltaImportQuery 是获取增量数据时应用的 SQL
  • deltaQuery 是获取 pk 的 SQL
  • parentDeltaQuery 是获取父 Entity 的 pk 的 SQL

    在 conf 文件夹下有个 dataimport.properties 文件,用来记录每个表(entity)索引最初更新的工夫。

    Mon Sep 26 12:10:14 CST 2022

    test.last_index_time=2022-09-26 12\:10\:13
    last_index_time=2022-09-26 12\:10\:13

${dih.last_index_time} 能够在 deltaQuery 语句中应用,DIH(DataImportHandler)

这里须要留神的是 tinyint(1) 类型的字段导入时默认会转换为 boolean 类型的,这时须要应用 mysql 的 convert(columnName,SIGNED) 函数进行解决,其中第一个参数为须要转换的列,第 2 个参数为转换成的类型,这里应用 SIGNED 意思为整型

增加 jar 包

把用到的数据库驱动 jar 包(jdbc,本人筹备)放到 server/solr-webapp/webapp/WEB-INF/lib 目录下,我这里用的是 mysql-connector-java-8.0.29.jar

复制 jar 包

~$ cp solr-8.11.2
~$ cp dist/solr-dataimporthandler-8.11.2.jar server/solr-webapp/webapp/WEB-INF/lib

重启 solr,而后就能够导入数据了

导入数据

执行胜利后能够查问到

能够看到右侧曾经有数据了。

参考文档

  1. 官网文档

正文完
 0