您现在的位置是:首页 > 文章详情

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

日期:2018-04-19点击:379

利用solrJ向索引库导入数据

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

[java] view plain copy

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

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章