09、solr学习-MultiCore的配置方法(下)

1. 验证多个core的情况下

第一步:Master的core0的访问管理界面

 

第二步:Slaver的core0的访问管理界面

 

第三步:Slaver的core0的索引复制管理界面

 

第四步:Master/Slave模式的主从索引架构如图

2、 solrj操作数据代码;

第一步:采用Maven方式集成solrj所需要的包

                 <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>4.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.http</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.2.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.http</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.http</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.noggit</groupId>
            <artifactId>noggit</artifactId>
            <version>0.5</version>
        </dependency>

第二步:通过单例模式创建获取solr服务类

package com.solr.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.impl.HttpSolrServer;

public class SolrServer {

    private static transient Log logger = LogFactory.getLog(SolrServer.class);
    // solr服务地址连接-The URL of the Solr server.
    private static final String baseURL = "http://192.168.17.128:9080/solr/core0";
    // solr服务实例对象
    private static HttpSolrServer server;

    // 获取solr服务
    public static synchronized HttpSolrServer getInstance() {
        try {
            if (server == null) {
                logger.info("connect solrServer is start.....");
                // 主机URLbaseURL - The URL of the Solr server. For example,
                // " http://localhost:8983/solr/" if you are using the standard
                // distribution Solr webapp on your local machine.
                server = new HttpSolrServer(baseURL);
                // 超时时间 Set SoTimeout (read timeout).
                server.setSoTimeout(5000);
                // 连接超时时间 HttpConnectionParams.setConnectionTimeout
                server.setConnectionTimeout(1000);
                // Set the maximum number of connections that can be open to a
                // single host at any given time. If http client was created
                // outside the operation is not allowed.
                server.setDefaultMaxConnectionsPerHost(1000);
                // 连接solr最大连接数 Set the maximum number of connections that can be
                // open at any given time.
                server.setMaxTotalConnections(100);
                // // defaults to false,Configure whether the client should
                // follow redirects or not.
                server.setFollowRedirects(false);
                // Server side must support gzip or deflate for this to have any
                // effect.
                server.setAllowCompression(true);
                // 最大尝试连接次数-Set maximum number of retries to attempt in the
                // event of transient errors.
                server.setMaxRetries(3);
                logger.info("connect solrServer success");
            }
        } catch (Exception e) {
            logger.error("connect solr is exception,please retry it!", e);
        }
        return server;
    }

    public void destory() {
        server = null;
        System.runFinalization();
        System.gc();
    }
}

其中:http://192.168.17.128:9080/solr/core0根据需要给出指定的baseURL.

第三步:创建维护索引工具类

package com.solr.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;

/**
 * <p>
 * solr服务器操作工具类
 * </p>
 * 
 * @author shenfl
 * @version V1.0
 * 
 * 
 */
public class SolrUtil {

    private static transient Log logger = LogFactory.getLog(SolrUtil.class);

    /**
     * 添加或者更新索引
     * 
     * @param docs
     * @param solr
     */
    public static void addOrUpdate(List<SolrInputDocument> docs, HttpSolrServer solr) {
        List<SolrInputDocument> docList = new ArrayList<SolrInputDocument>();
        try {
            for (int i = 0; i < docs.size(); i++) {
                docList.add(docs.get(i));
                solr.add(docList);
                if (docList.size() % 100 == 0) {
                    solr.commit();
                    docList.clear();
                }
            }
            solr.commit();
        } catch (Exception e) {
            logger.error(" addOrUpdate data to solr server is exception ", e);
            e.printStackTrace();
        }
    }

    /**
     * 查询所有索引
     * 
     * @param solr
     * @return
     */
    public static SolrDocumentList query(HttpSolrServer solr) {
        ModifiableSolrParams params = null;
        SolrDocumentList documentList = null;
        try {
            params = new ModifiableSolrParams();
            // Query String: *:*标识查询所有的数据库索引
            params.set("q", "*:*");
            // Start Row
            params.set("start", 0);
            // Maximum Rows Returned
            params.set("rows", 10);
            QueryResponse response = solr.query(params);
            fail("query time:" + response.getQTime());
            fail("Elapsed Time:" + response.getElapsedTime());
            fail("status:" + response.getStatus());
            documentList = response.getResults();
        } catch (Exception e) {
            logger.error(" query data to solr server is exception ", e);
        }
        return documentList;
    }
    /**
     * 根据ID单个删除的索引:<delete><id>1403001</id></delete>
     * 
     * @param id
     */
    public static void removeById(String id, HttpSolrServer solr) {

        try {
            solr.setRequestWriter(new BinaryRequestWriter());//提高性能采用流输出方式
            solr.deleteById(id);
            solr.commit();
            if (logger.isDebugEnabled()) {
                logger.debug("removeById id = " + id + " success");
            }
        } catch (Exception e) {
            logger.error(" removeById data to solr server is exception ", e);
        }
    }

    /**
     * 批量删除多个ID的索引
     * 
     * @param ids
     */
    public static void batchRemoveByIds(List<String> ids, HttpSolrServer solr) {

        try {
            solr.setRequestWriter(new BinaryRequestWriter());//提高性能采用流输出方式
            solr.deleteById(ids);
            solr.commit();
            if (logger.isDebugEnabled()) {
                logger.debug("batchRemoveByIds ids = " + ids.toString() + " success");
            }
        } catch (Exception e) {
            logger.error(" batchRemoveByIds data to solr server is exception ", e);
        }
    }
    /**
     * 批量删除所有的索引
     * 
     * @param ids
     */
    public static void batchRemove(HttpSolrServer solr) {

        try {
            solr.setRequestWriter(new BinaryRequestWriter());//提高性能采用流输出方式
            solr.deleteByQuery("*:*");
            solr.commit();
        } catch (Exception e) {
            logger.error(" batchRemove data to solr server is exception ", e);
        }
    }

    public final static void fail(Object o) {
        logger.info(o);
    }

}

第四步:创建维护索引测试类

package com.solr.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import com.solr.util.SolrUtil;

public class SolrUtilTest {

    @Test
    public void testAddDoc() {
        List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
        SolrInputDocument solrDocument = new SolrInputDocument();

        solrDocument.addField("id", "1000001");
        solrDocument.addField("name", "shenfl40");
        docs.add(solrDocument);

        SolrUtil.addOrUpdate(docs, SolrServer.getInstance());
    }

    @Test
    public void testQuery() {

        SolrDocumentList solrDocumentList = null;
        try {
            solrDocumentList = SolrUtil.query(SolrServer.getInstance());
            // 输出文档列表数据
            for (SolrDocument doc : solrDocumentList) {
                String info = doc.getFieldValue("id") + "," + doc.getFieldValue("name");
                System.out.println("==>>" + info);//==>>1000001,shenfl40
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testClear(){
        SolrUtil.batchRemove(SolrServer.getInstance());
    }

    public void testClear2(){
        SolrUtil.removeById("1000000",SolrServer.getInstance() );
    }
}

说明:testAddDoc生产索引,结果可以通过solr的管理界面查看,也可以通过代码testQuery查看。