1. 程式人生 > >solrj操作zookeeper上傳schema檔案

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");
    }
    
}