SolrJ 7.2.1 连接集群测试

SolrJ 7.2.1 连接集群测试

先吐槽一下solrj版本差异太大了,为了测试连接zookeeper集群试了不下五个版本的jar包,测试的想吐血以此记录一下:

我这里用的jar包是7.2.1的,不是官网最新的。
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.2.1</version>
</dependency>

测试类

import java.util.UUID;

import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

public class SearchTest {

    //创建一个和solr集群的连接,参数为zookeeper地址,以逗号分隔
    private static final String ZK_HOST="192.168.4.52:2181,192.168.4.53:2181,192.168.4.54:2181";


    @Test
    public void add()throws Exception{
        CloudSolrClient solr=new CloudSolrClient.Builder().withZkHost(ZK_HOST).build();
        solr.setDefaultCollection("text");
        SolrInputDocument document=new SolrInputDocument();
        String str=UUID.randomUUID().toString();
        System.out.println(str);
        document.setField("id", str);
        document.setField("title", "小胖真可爱");
        System.out.println(document);
        solr.add(document);
        solr.commit();


    }

}`

spring 整合

<bean id="cloudSolrBuilder" class="org.apache.solr.client.solrj.impl.CloudSolrClient.Builder" />
    <bean id="builder" factory-bean="cloudSolrBuilder" factory-method="withZkHost" >
        <constructor-arg type="java.lang.String"
            value="192.168.4.52:2181,192.168.4.53:2181,192.168.4.54:2181" />
    </bean>
    <bean  id="cloudSolrClient" factory-bean="builder" factory-method="build">
        <property name="defaultCollection" value="collection"/>
    </bean>

单机版这里就不放了,单机版挺简单的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值