开发者社区> 推荐码发放> 正文

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

简介: 利用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.     }

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
使用Sqoop将HIVE中的数据导入Mysql
使用Sqoop将HIVE中的数据导入Mysql
50 0
从mysql查询数据导入redis入队中
从mysql查询数据导入redis入队中
31 0
深聊MySQL,从入门到入坟之:如何优化数据导入?
深聊MySQL,从入门到入坟之:如何优化数据导入?
60 0
MySQL低配数据库被大量数据导入时KO
MySQL低配数据库被大量数据导入时KO
46 0
通过sqoop将mysql数据导入到hive中进行计算示例
通过sqoop将mysql数据导入到hive中进行计算示例
53 0
将Excel数据导入MySQL(图解)
将Excel数据导入MySQL(图解)
107 0
Springboot 最简单的结合MYSQL数据实现EXCEL表格导出及数据导入
Springboot 最简单的结合MYSQL数据实现EXCEL表格导出及数据导入
258 0
通过sqoop将mysql数据导入到hive中进行计算示例
通过sqoop将mysql数据导入到hive中进行计算示例
94 0
【RDS MySQL】将Excel的数据导入数据库
您可以将Excel的数据通过数据管理服务DMS(Data Management Service)导入到RDS MySQL数据库中。
413 0
阿里云ElasticSearch使用LogStash通过公网将MySQL数据导入
阿里云Logstash(简称Logstash)作为服务器端的数据处理管道,提供了100%兼容开源Logstash的能力。Logstash能够动态地从多个来源采集数据、转换数据,并且将数据存储到所选择的位置。通过输入、过滤和输出插件,Logstash可以对任何类型的事件加工和转换。本文主要演示如何基于公网方式将MySQL数据通过LogStash管道导入到ElasticSearch实例中。
435 0
+关注
推荐码发放
阿里云优惠码阿里云推荐券bieryun.com
文章
问答
文章排行榜
最热
最新
相关电子书
更多
让 MySQL 原生分布式触手可及
立即下载
好的 MySQL 兼容可以做到什么程度
立即下载
云数据库RDS MySQL从入门到高阶
立即下载