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

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 利用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.     }
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
10天前
|
关系型数据库 MySQL 数据库
ORM对mysql数据库中数据进行操作报错解决
ORM对mysql数据库中数据进行操作报错解决
35 2
|
10天前
|
SQL 关系型数据库 MySQL
MySQL如何排查和删除重复数据
该文章介绍了在MySQL中如何排查和删除重复数据的方法,包括通过组合字段生成唯一标识符以及使用子查询和聚合函数来定位并删除重复记录的具体步骤。
29 2
|
7天前
|
SQL 关系型数据库 MySQL
MySQL操作利器——mysql-connector-python库详解
MySQL操作利器——mysql-connector-python库详解
36 0
|
5天前
|
消息中间件 canal 关系型数据库
Maxwell:binlog 解析器,轻松同步 MySQL 数据
Maxwell:binlog 解析器,轻松同步 MySQL 数据
39 11
|
4天前
|
关系型数据库 MySQL 数据库
MySQL的语法涵盖了数据定义、数据操作、数据查询和数据控制等多个方面
MySQL的语法涵盖了数据定义、数据操作、数据查询和数据控制等多个方面
18 5
|
1天前
|
存储 关系型数据库 MySQL
MySQL中的索引及怎么使用
综上所述,MySQL索引的正确使用是数据库性能调优的关键一环。通过合理设计索引结构,结合业务需求和数据特性,可以有效提升数据库查询响应速度,降低系统资源消耗,从而确保应用的高效运行。
9 1
|
5天前
|
存储 关系型数据库 MySQL
MySQL索引失效及避免策略:优化查询性能的关键
MySQL索引失效及避免策略:优化查询性能的关键
23 3
|
11天前
|
关系型数据库 MySQL 数据库
MySQL删除全局唯一索引unique
这篇文章介绍了如何在MySQL数据库中删除全局唯一的索引(unique index),包括查看索引、删除索引的方法和确认删除后的状态。
32 9
|
5天前
|
存储 SQL 关系型数据库
MySQL 的索引是怎么组织的?
MySQL 的索引是怎么组织的?
12 1
|
6天前
|
存储 关系型数据库 MySQL
MySQL索引的概念与好处
本文介绍了MySQL存储引擎及其索引类型,重点对比了MyISAM与InnoDB引擎的不同之处。文中详细解释了InnoDB引擎的自适应Hash索引及聚簇索引的特点,并阐述了索引的重要性及使用原因,包括提升数据检索速度、实现数据唯一性等。最后,文章还讨论了主键索引的选择与页分裂问题,并提供了使用自增字段作为主键的建议。
MySQL索引的概念与好处
下一篇
无影云桌面