Solr8批量导入数据库内容
2020-02-21 本文已影响0人
長得太帥忚四種檌
1.在数据存储目录中创建lib目录

2.找到以下路径中的jar包, 复制到数据存储目录中的lib目录中

还需要添加MySQL数据库的驱动jar包:

3.数据存储目录中的核心配置文件中,添加请求处理器

添加以下内容:
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>

4.在数据存储目录中的conf目录中新建data-config.xml文件,该文件用来连接数据库,用于导入时完成数据库字段和域的映射,配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/book_reader?useUnicode=true&serverTimezone=Hongkong&characterEncoding=UTF-8&tinyInt1isBit=false"
user="gongxm"
password="12345678"/>
<document>
<entity name="book" query="SELECT id,book_name,author,cover,book_category_id,description,last_chapter,last_update_time FROM book ">
<field column="id" name="book_id"/>
<field column="book_name" name="book_name"/>
<field column="author" name="book_author"/>
<field column="cover" name="book_author"/>
<field column="book_category_id" name="book_category_id"/>
<field column="description" name="book_description"/>
<field column="last_chapter" name="book_last_chapter"/>
<field column="last_update_time" name="book_last_update_time"/>
</entity>
</document>
</dataConfig>
注意:data-config.xml中的field标签中的name值,在schema.xml中一定要存在。否则会出错。
5.开始导入数据
1,启动solr服务
2,打开浏览器, 输入地址访问solr
3,根据以下步骤操作:

导入结果:

6.验证

可以看到solr里面已经是有数据的了, 说明已经导入数据成功!