当前位置:   article > 正文

【Solr】深入浅出Solr(三)——使用Solrj向索引库中导入数据_solrj依赖

solrj依赖

一、前言

      在上一篇博客中小编向大家简单介绍了一下如何搭建单机版的Solr服务,这样我们的搭建完成了Solr服务,但是如何使用呢?Solr服务中有我们要用的索引库,所以首先要做的就是向索引库中导入数据,那在java中又要如何操作呢?小编在这篇博客中,向大家介绍一下如何使用Solrj向索引库中导入数据。

二、Solrj是什么?

solrJ是Java连接solr进行查询检索和索引更新维护的jar包。

      我们可以通过solrj,对solr进行操作。就是这么简单。从数据库中根据sql语句查询数据,遍历数据创建文档对象,把文档对象写入索引库。

三、导入数据

3.1 引入依赖

<!-- solr客户端 -->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

3.2 DAO层

      因为需要自己写sql语句,就不能用Mybatis的逆向工程生成的了,所以需要自己写一个Mapper。

      mapper文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.taotao.search.mapper.ItemMapper" >
    <select id="getItemList" resultType="com.taotao.search.pojo.SearchItem">
        SELECT
            a.id,
            a.title,
            a.sell_point,
            a.price,
            a.image,
            b.`name` category_name,
            c.item_desc
        FROM
            tb_item a
        LEFT JOIN tb_item_cat b ON a.cid = b.id
        LEFT JOIN tb_item_desc c ON a.id = c.item_id
        WHERE
            a.`status` = 1

    </select>
</mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

      Mapper接口:

package com.taotao.search.mapper;

import java.util.List;

import com.taotao.search.pojo.SearchItem;

public interface ItemMapper {
    List<SearchItem> getItemList();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.3 Service

      取出需要的商品信息,创建文档对象,把对象写入索引库。要操作索引库需要SolrService对象,可以把SolrService放到spring容器中,注入到Service。

      下面给出了单机版和集群版的注入方式:

<!-- 单机版solr客户端 -->
    <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
       <!--  构造方法 -->
        <constructor-arg name="baseURL" value="http://192.168.137.13:8080/solr"/>
    </bean> 

    <!-- 集群版solr客户端 -->
    <!-- <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
        <constructor-arg name="zkHost" value="192.168.137.13:2181,192.168.137.13:2182,192.168.137.13:2183"></constructor-arg>
        <property name="defaultCollection" value="collection2"></property>
    </bean> -->
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

      使用solrj.SolrServer导入数据:

package com.taotao.search.service.impl;

import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.search.mapper.ItemMapper;
import com.taotao.search.pojo.SearchItem;
import com.taotao.search.service.ItemService;
/**
 * 商品导入service
 * @author Ares
 *
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private SolrServer solrServer;

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public TaotaoResult importItems() throws Exception{
        //查询数据库获得商品列表
        List<SearchItem> itemList = itemMapper.getItemList();
        for (SearchItem item : itemList) {
            //创建文档对象
            SolrInputDocument document = new SolrInputDocument();
            //添加域
            document.addField("id", item.getId());
            document.addField("item_title", item.getTitle());
            document.addField("item_sell_point", item.getSell_point());
            document.addField("item_price", item.getPrice());
            document.addField("item_image", item.getImage());
            document.addField("item_category_name", item.getCategory_name());
            document.addField("item_desc", item.getItem_desc());
            //写入索引库
            solrServer.add(document);

        }
        //提交
        solrServer.commit();
        return TaotaoResult.ok();
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

3.4 Controller

      导入:

package com.taotao.search.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.util.ExceptionUtil;
import com.taotao.search.service.ItemService;
/**
 * 导入商品数据Controller
 * @author Ares
 *
 */
@Controller
public class ItemController {
    @Autowired
    private ItemService itemService;

    @RequestMapping("/importall")
    @ResponseBody
    public TaotaoResult importAll(){
        try {
            return itemService.importItems();
        } catch (Exception e) {
            e.printStackTrace();
            return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
        }

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

      这样我们的服务就写好了,项目跑起来,直接运行服务,http://localhost:8080/search/importall ,运行的时候可能会遇到如下错误:

这里写图片描述

      解决方案:

这里写图片描述

3.5 查看导入效果

      访问solr,进行全文查询,就可以查看到我们导入的数据:

这里写图片描述

四、小结

      通过对Solr导入就可以很深入的数据是如何处理的了,当我们把数据导入后,就可以进行下一步的查询工作了。所以在下一篇博客中,小编向大家介绍如何进行查询工作。敬请期待。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/789008
推荐阅读
相关标签
  

闽ICP备14008679号