天天看點

将MySQL中的資料導入到solr索引庫

利用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.     public String getTitle() {
  14.         return title;
  15.     public void setTitle(String title) {
  16.         this.title = title;
  17.     public String getSell_point() {
  18.         return sell_point;
  19.     public void setSell_point(String sell_point) {
  20.         this.sell_point = sell_point;
  21.     public long getPrice() {
  22.         return price;
  23.     public void setPrice(long price) {
  24.         this.price = price;
  25.     public String getImage() {
  26.         return image;
  27.     public String[] getImages() {
  28.         if(image != null && !"".equals(image)) {
  29.             String[] images = image.split(",");
  30.             return images;
  31.         }
  32.         return null;
  33.     public void setImage(String image) {
  34.         this.image = image;
  35.     public String getCategory_name() {
  36.         return category_name;
  37.     public void setCategory_name(String category_name) {
  38.         this.category_name = category_name;
  39.     public SearchItem(String id, String title, String sell_point, long price, String image, String category_name) {
  40.         super();
  41.     public SearchItem() {
  42.         // TODO Auto-generated constructor stub
  43.     @Override
  44.     public String toString() {
  45.         return "SearchItem [id=" + id + ", title=" + title + ", sell_point=" + sell_point + ", price=" + price
  46.                 + ", image=" + image + ", category_name=" + category_name + "]";

定義mapper查詢資料庫:

  1. List<SearchItem> selectAllItem();
[html]
  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向索引庫導入資料:

  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.         solrServer.commit();
  22.         //傳回成功
  23.         return E3Result.ok();
  24.         } catch (Exception e) {
  25.             // TODO: handle exception
  26.             return E3Result.build(500, "導入失敗!");

原文位址

http://www.bieryun.com/3229.html