solrj操作zookeeper上传schema文件

package com.uniclues.solr;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.Vector;

import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient;

/**
 * Copyright (C) 2018 Uniclues
 * 
 * SolrClient工具类
 *
 * @author Zhanglq
 * @Date 2018-09-04
 * @version 1.00
 */
public class ClientManager {
    
    // 连接池大小
    private final static int POOL_SIZE = 3;
    // ZK URL
    private final static String ZK_HOST = "192.168.8.200:9983";
    // private final static String ZK_HOST = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
    // CloudSolrClient 连接池
    private static List<CloudSolrClient> searchClientPool = new Vector<CloudSolrClient>();
    
    private static ClientManager instance;
    
    private ClientManager() {
        try {
            initSolrClientPool();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static synchronized ClientManager getInstance() {
        if (instance == null) {
            instance = new ClientManager();
        }
        return instance;
    } 
    
    /**
     * 初始化SolrClient连接池
     */
    private void initSolrClientPool() throws Exception {
        // 初始化检索用SolrClient连接池
        for(int i = 0; i < POOL_SIZE; i ++) {
            searchClientPool.add(makeCloudSolrClient());
        }
    }
    
    private synchronized CloudSolrClient makeCloudSolrClient() throws Exception {
        final List<String> hosts = new ArrayList<String>();
        hosts.add(ZK_HOST);
        CloudSolrClient.Builder builder = new CloudSolrClient.Builder(hosts, Optional.empty());
        CloudSolrClient client = builder.build();
        //设置collection缓存的存活时间,单位 分钟
        client.setCollectionCacheTTl(2);
        //索引优化
        //client.optimize();
        client.connect();
        return client;
    }
    
    /**
     * 获取检索用SolrClient
     * @return
     */
    public CloudSolrClient getSearchClient() throws Exception {
        return searchClientPool.get((new Random()).nextInt(POOL_SIZE));
    }
    
    private String getBaseSolrUrl() throws Exception {
        String baseSolrUrl = null;
        CloudSolrClient client = getSearchClient();
        Set<String> liveNodes = client.getZkStateReader().getClusterState().getLiveNodes();
        if(liveNodes != null) {
            baseSolrUrl = liveNodes.iterator().next();
        }
        if(baseSolrUrl != null) {
            baseSolrUrl = baseSolrUrl.replaceAll("_", "/");
        }
        return baseSolrUrl;
    }
    /**
     * 获取维护索引用SolrClient
     * @return
     */
    public ConcurrentUpdateSolrClient getUpdateClient(String collection) throws Exception {
        int cussThreadCount = 2;
        int cussQueueSize = 10;
        String baseSolrUrl = getBaseSolrUrl();
        ConcurrentUpdateSolrClient client
        = (new ConcurrentUpdateSolrClient.Builder("http://" + baseSolrUrl + "/" + collection))
        .withQueueSize(cussQueueSize)
        .withThreadCount(cussThreadCount).build();
        return client;
    }
}

——————————————————————————————————————————————————————

package com.uniclues.solr;

import java.nio.file.Path;
import java.nio.file.Paths;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.solr.common.cloud.SolrZkClient;
import org.apache.solr.common.cloud.ZkConfigManager;

import com.uniclues.dbsearch.common.Uniclues;

/**
 * Copyright (C) 2018 Uniclues
 * 
 * CollectionConfigManager工具类
 *
 * @author dong
 * @Date 2018-09-04
 * @version 1.00
 */
public class CollectionConfigManager {
    private final static String CONFIG_PATH = Uniclues.getInstance().getUnicluesHome()+"/tableConfig/";
    private final static String DEFAULTTABLECONFIG="defaultTable";
    
    private static Logger log = Logger.getLogger(CollectionConfigManager.class);
    
    /**
     * 下载默认配置
     * @throws Exception
     */
//    public static void downDefaultConfig() throws Exception {
//        downConfig(DEFAULTTABLECONFIG);
//        log.info("***下载默认配置***");
//    }
    
    /**
     * 上传默认配置
     * @throws Exception
     */
    public static void upDefaultConfig() throws Exception {
        upConfig(DEFAULTTABLECONFIG);
        log.info("***上传默认配置***");
    }
    
    /**
     * 下载zk下默认的collectionConfig到本地
     * @param name 新的配置名前缀
     * @throws Exception
     */
    public static void downConfig(String collection) throws Exception {
        SolrZkClient solrZkClient = ClientManager.getInstance().getSearchClient().getZkStateReader().getZkClient();
        Path path=Paths.get(CONFIG_PATH+collection+"_config");
        solrZkClient.downConfig(DEFAULTTABLECONFIG+"_config", path);
        log.info("***下载zk的collectionConfig到本地【"+collection+"】***");
    }
    
    /**
     * 上传collectionConfig到zk
     * @param name
     * @throws Exception
     */
    public static void upConfig(String collection) throws Exception {
        SolrZkClient solrZkClient = ClientManager.getInstance().getSearchClient().getZkStateReader().getZkClient();
        String collectionName=collection+"_config";
        Path path=Paths.get(CONFIG_PATH+collectionName);
        solrZkClient.upConfig(path, collectionName);
        log.info("***上传collectionConfig到zk【"+collection+"】***");
    }
    /**
     * 复制默认配置生成新的collection配置
     * @param collection 新的配置名称
     * @throws Exception
     */
    public static void copyDefaultConfig(String collection) throws Exception {
        ZkConfigManager zkConfigManager = ClientManager.getInstance().getSearchClient().getZkStateReader().getConfigManager();
        String collectionName=collection+"_config";
        //solrZkClient.upConfig(path, collectionName);
        zkConfigManager.copyConfigDir(DEFAULTTABLECONFIG+"_config", collectionName);
        log.info("***复制默认配置生成新的collection配置【"+collection+"】***");
    }
    
    public static void main(String[] args) {
        try {
            //upConfig("SA_VW_XGDZ_IN_NN109_TXL");
            //System.out.println("ook");
            //ClientManager.getInstance().getSearchClient().getZkStateReader().getZkClient().getSolrZooKeeper().getData("/clusterstate.json", null, null);
//            JSONObject jsonclusterstate=JSONObject.fromObject(new String(ClientManager.getInstance().getSearchClient().getZkStateReader().getZkClient().getSolrZooKeeper().getData("/clusterstate.json", null, null),"utf-8"));
//            System.out.println(jsonclusterstate.toString());
            String collection="gettingstarted";
            //deleteConfig(collection);
//            if(!exists(collection)){
//                System.out.println("***["+collection+"]配置不存在***");
//                copyDefaultConfig(collection);
//            }
            
//            SolrZkClient solrZkClient = ClientManager.getInstance().getSearchClient().getZkStateReader().getZkClient();
//            Path path=Paths.get(CONFIG_PATH+collection+"_config");
//            solrZkClient.downConfig("gettingstarted", path);
            //upConfig("gettingstarted");
            //upDefaultConfig();
            //upConfig("newcollection");
            //deleteConfig("newcollection");
            //copyDefaultConfig("newcollection");
            //upConfig("newcollection2");
            deleteConfig("SA.VW_XGDZ_IN_NNJDCXX");
            System.out.println("ook");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /**
     * 检测默认配置是否存在
     * @return
     * @throws Exception
     */
    public static boolean existsDefaultConfig() throws Exception{
        log.info("***检测默认配置是否存在***");
        return exists(DEFAULTTABLECONFIG);
    }

    /**
     * 检测配置是否存在
     * @param name
     * @return
     * @throws Exception
     */
    public static boolean exists(String collection) throws Exception{
        SolrZkClient solrZkClient = ClientManager.getInstance().getSearchClient().getZkStateReader().getZkClient();
        log.info("***检测配置是否存在【"+collection+"】***");
        return solrZkClient.exists("/configs/"+collection+"_config", false);
    }
    
    /**
     * 删除collection配置
     * @param collection
     * @throws Exception
     */
    public static void deleteConfig(String collection) throws Exception{
        ZkConfigManager zkConfigManager = ClientManager.getInstance().getSearchClient().getZkStateReader().getConfigManager();
        log.info("***删除collection配置【"+collection+"】***");
        zkConfigManager.deleteConfigDir(collection+"_config");
    }
    
}
 

猜你喜欢

转载自blog.csdn.net/qq_39716220/article/details/82532189