将MySQL中的数据导入到solr索引库

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 利用solrJ向索引库导入数据http://www.bieryun.com/3229.html 需求:将MySQL中的数据导入到solr索引库 定义实体类: [java] view plain copy public class SearchItem implements Seri.

利用solrJ向索引库导入数据http://www.bieryun.com/3229.html

需求:将MySQL中的数据导入到solr索引库
定义实体类:

[java] view plain copy

  1. public class SearchItem implements Serializable{
  2.  
  3.     private String id;
  4.     private String title;
  5.     private String sell_point;
  6.     private long price;
  7.     private String image;
  8.     private String category_name;
  9.     public String getId() {
  10.         return id;
  11.     }
  12.     public void setId(String id) {
  13.         this.id = id;
  14.     }
  15.     public String getTitle() {
  16.         return title;
  17.     }
  18.     public void setTitle(String title) {
  19.         this.title = title;
  20.     }
  21.     public String getSell_point() {
  22.         return sell_point;
  23.     }
  24.     public void setSell_point(String sell_point) {
  25.         this.sell_point = sell_point;
  26.     }
  27.     public long getPrice() {
  28.         return price;
  29.     }
  30.     public void setPrice(long price) {
  31.         this.price = price;
  32.     }
  33.     public String getImage() {
  34.         return image;
  35.     }
  36.     public String[] getImages() {
  37.         if(image != null && !"".equals(image)) {
  38.             String[] images = image.split(",");
  39.             return images;
  40.         }
  41.         return null;
  42.     }
  43.     public void setImage(String image) {
  44.         this.image = image;
  45.     }
  46.     public String getCategory_name() {
  47.         return category_name;
  48.     }
  49.     public void setCategory_name(String category_name) {
  50.         this.category_name = category_name;
  51.     }
  52.     public SearchItem(String id, String title, String sell_point, long price, String image, String category_name) {
  53.         super();
  54.         this.id = id;
  55.         this.title = title;
  56.         this.sell_point = sell_point;
  57.         this.price = price;
  58.         this.image = image;
  59.         this.category_name = category_name;
  60.     }
  61.     public SearchItem() {
  62.         super();
  63.         // TODO Auto-generated constructor stub
  64.     }
  65.     @Override
  66.     public String toString() {
  67.         return "SearchItem [id=" + id + ", title=" + title + ", sell_point=" + sell_point + ", price=" + price
  68.                 + ", image=" + image + ", category_name=" + category_name + "]";
  69.     }
  70.  
定义mapper查询数据库:
 

[java] view plain copy

  1. List<SearchItem> selectAllItem();

[html] view plain copy

  1. <select id="selectAllItem" resultType="com.e3mall.search.SearchItem">
  2. SELECT
  3.     a.id,
  4.     a.title,
  5.     a.sell_point,
  6.     a.price,
  7.     a.image,
  8.     b.`name` category_name
  9. FROM
  10.     tb_item a
  11. LEFT JOIN tb_item_cat b ON a.cid = b.id
  12. WHERE a.`status`=1
  13. </select>
  14. </mapper>
利用solrJ向索引库导入数据:
 

[java] view plain copy

  1. /**
  2.      * 向索引库添加数据
  3.      */
  4.     public E3Result saveSearch(){
  5.         try {
  6.         //从数据库中查询数据
  7.         List<SearchItem> selectAllItem = searchMapper.selectAllItem();
  8.         for (SearchItem searchItem : selectAllItem) {
  9.             // 创建一个文档对象SolrInputDocument
  10.             SolrInputDocument document = new SolrInputDocument();
  11.             // 向文档对象中添加域,文档中必须包含一个id域,所有的域的名称必须在schema.xml中定义
  12.             document.addField("id", searchItem.getId());
  13.             document.addField("item_title", searchItem.getTitle());
  14.             document.addField("item_sell_point", searchItem.getSell_point());
  15.             document.addField("item_price", searchItem.getPrice());
  16.             document.addField("item_image", searchItem.getImage());
  17.             document.addField("item_category_name", searchItem.getCategory_name());
  18.             // 把文档写入索引库
  19.             solrServer.add(document);
  20.         }
  21.         // 提交
  22.         solrServer.commit();
  23.         //返回成功
  24.         return E3Result.ok();
  25.         } catch (Exception e) {
  26.             // TODO: handle exception
  27.             return E3Result.build(500"导入失败!");
  28.         }
  29.     }
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
14天前
|
存储 关系型数据库 MySQL
Mysql索引总结(1)
Mysql索引总结(1)
23 0
|
15天前
|
存储 关系型数据库 MySQL
MySQL 索引的10 个核心要点
MySQL 索引的10 个核心要点
20 0
|
15天前
|
SQL 关系型数据库 MySQL
MySQL8.0索引新特性
MySQL8.0索引新特性
17 0
|
18小时前
|
SQL 存储 关系型数据库
MySQL的3种索引合并优化⭐️or到底能不能用索引?
MySQL的3种索引合并优化⭐️or到底能不能用索引?
|
1天前
|
Java 关系型数据库 MySQL
MySQL 索引事务
MySQL 索引事务
11 0
|
1天前
|
SQL 关系型数据库 MySQL
MySQL决战:MySQL数据导入导出
MySQL决战:MySQL数据导入导出
|
1天前
|
存储 SQL 关系型数据库
MySQL 底层数据结构 聚簇索引以及二级索引 Explain的使用
MySQL 底层数据结构 聚簇索引以及二级索引 Explain的使用
11 0
|
1天前
|
存储 关系型数据库 MySQL
学习MySQL(5.7)第二战:四大引擎、账号管理以及建库(干货满满)
学习MySQL(5.7)第二战:四大引擎、账号管理以及建库(干货满满)
|
1天前
|
自然语言处理 关系型数据库 MySQL
一文明白MySQL索引的用法及好处
一文明白MySQL索引的用法及好处
10 0
|
2天前
|
存储 SQL 关系型数据库
MySQL的优化利器⭐️索引条件下推,千万数据下性能提升273%🚀
以小白的视角探究MySQL索引条件下推ICP的优化,其中包括server层与存储引擎层如何交互、索引、回表、ICP等内容
MySQL的优化利器⭐️索引条件下推,千万数据下性能提升273%🚀