solr的应用(二)在分布式电商项目中批量导入数据

项目搭建

在父工程中添加依赖

<spring-data-solr.version>1.5.5.RELEASE</spring-data-solr.version>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-solr</artifactId>
    <version>${spring-data-solr.version}</version>
</dependency>

在实体类jar工程的pom文件上加入spring-data-solr的依赖和tb_item类上加入注解,修改完后安装pojo工程

public class TbItem implements Serializable {

    @Field
    private Long id;//与solr中name一致,不用在注解中指定名称

    @Field("item_title")
    private String title;

    private String sellPoint;

    @Field("item_price")
    private BigDecimal price;

    private Integer stockCount;

    private Integer num;

    private String barcode;

    @Field("item_image")
    private String image;

    private Long categoryid;

    private String status;

    private Date createTime;

    @Field("item_updatetime")
    private Date updateTime;

    private String itemSn;

    private BigDecimal costPirce;

    private BigDecimal marketPrice;

    private String isDefault;

    @Field("item_goodsid")
    private Long goodsId;

    private String sellerId;

    private String cartThumbnail;

    @Field("item_category")
    private String category;

    @Field("item_brand")
    private String brand;

    private String spec;

    @Field("item_seller")
    private String seller;
	省略了get和set方法
}

在父工程下创建一个moudle,jar工程youlexuan-solr-util
pom依赖
添加自己的项目依赖youlexuan_dao
因为dao工程依赖了pojo工程,pojo工程导入了spring-data-solr依赖,所以youlexuan-solr-util也依赖了spring-data-solr

<artifactId>youlexuan-solr-util</artifactId>
<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
    <dependency>
        <groupId>com.youxin</groupId>
        <artifactId>youlexuan_dao</artifactId>
        <version>1.0.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

配置spring-solr-util.xml,并在创建的类上加上@Compent注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.youxin.solrutil" />

</beans>

配置spring-solr.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:solr="http://www.springframework.org/schema/data/solr"
       xsi:schemaLocation="http://www.springframework.org/schema/data/solr
  		http://www.springframework.org/schema/data/solr/spring-solr.xsd
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- solr服务器地址和solr库 -->
    <solr:solr-server id="solrServer" url="http://192.168.2.123:8983/solr/core1" />

    <!-- solr模板,使用solr模板可对索引库进行CRUD操作 -->
    <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
        <constructor-arg ref="solrServer" />
    </bean>

</beans>

搭建的最后一步创建solrUtil类
注入TbItemMapper和SolrTemplate

@Component
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring-*.xml")
public class SolrUtil {

    @Autowired
    private TbItemMapper itemMapper;

    @Autowired
    private SolrTemplate solrTemplate;

    /*
    导入商品数据
     */
    @Test
    public void importItemData(){
        
    }
}

导入数据到solr

基本域

@Test
public void importItemData(){
    //先条件查询出审核通过的商品
    TbItemExample example = new TbItemExample();
    TbItemExample.Criteria criteria = example.createCriteria();
    criteria.andStatusEqualTo("1");
    List<TbItem> items = itemMapper.selectByExample(example);
    //导入solr
    solrTemplate.saveBeans(items);
    solrTemplate.commit();
}

运行测试方法,查看效果
在这里插入图片描述
动态域
数据库中规格的形式
在这里插入图片描述
代码中字段的类型
在这里插入图片描述
规格的名称要以动态的形式命名,而代码中的规格选项是字符串类型,无法操作其中的键和值,所以我们在实体类中新创建一个Map类型的属性specMap,并加上注解以及get、set方法

@Dynamic
@Field("item_spec_*")
private Map<String,String> specMap;

public Map<String, String> getSpecMap() {
    return specMap;
}

public void setSpecMap(Map<String, String> specMap) {
    this.specMap = specMap;
}

public static long getSerialVersionUID() {
    return serialVersionUID;
}

注意:solr 5.x后,安装方式从tomcat中独立出来,且键不支持中文了。因此,itme_spec_网络 的动态属性,会保存为itme_spec___ 形式的下划线。
解决方案:将中文转为拼音。

<dependency>
	<groupId>com.github.promeg</groupId>
	<artifactId>tinypinyin</artifactId>
	<version>2.0.3</version>
</dependency>

代码实现

@Test
public void importItemData(){
    //先条件查询出审核通过的商品
    TbItemExample example = new TbItemExample();
    TbItemExample.Criteria criteria = example.createCriteria();
    criteria.andStatusEqualTo("1");
    List<TbItem> items = itemMapper.selectByExample(example);
    for (TbItem item : items) {
        Map<String,Object> specMap = JSON.parseObject(item.getSpec(), Map.class);//转成map格式
        System.out.println(specMap);
        System.out.println("分割线");
        Map map = new HashMap();//临时map
        for (String key : specMap.keySet()) {
            // 第一个参数:要转换的中文
            // 第二参数:分隔符,比如逗号之类的,比如从哪转换到哪,没有的话,就写空串
            map.put(Pinyin.toPinyin(key,"").toLowerCase(),specMap.get(key));
        }
        System.out.println(map);
        //给动态域名赋值
        item.setSpecMap(map);
    }
    //导入solr
    solrTemplate.saveBeans(items);
    solrTemplate.commit();
}

运行测试类
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值