`
290434409
  • 浏览: 26798 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

SolrCloud之zookeeper中使用java代码创建集合

阅读更多

javaAPI调用solrcloud上传配置和删除/创建集合。网上大多都是通过http路径调用创建,以下是通过Zookeeper+solr进行操作:

 

public class SolrConfig {
    static String ZK_HOST="10.8.177.204:2181,10.8.177.210:2181,10.8.177.33:2181,10.8.177.22:2181";
    static SolrZkClient zkClient=new SolrZkClient(ZK_HOST,30000);
    static ZkConfigManager zkConfigManager=new ZkConfigManager(zkClient);
    /**
     * 上传配置
     * @param configName
     * @param configPath
     * @throws IOException
     */
    public static void uploadConfig(String configName,String configPath)throws IOException{
        System.out.println("准备上传配置:"+configName);
        zkConfigManager.uploadConfigDir(Paths.get(configPath),configName);
        System.out.println("上传配置成功!");
    }

    /**
     * 删除集合
     * @param deleteName
     * @throws IOException
     * @throws SolrServerException
     */
    public static void deleteCollection(String deleteName)throws IOException,SolrServerException{
        SolrClient solrClient=new CloudSolrClient(ZK_HOST);
        CollectionAdminRequest.Delete delete=new CollectionAdminRequest.Delete();
        delete.setCollectionName(deleteName);
        CollectionAdminResponse response=delete.process(solrClient);
        System.out.println(response);
        solrClient.close();
    }

    /**
     * 创建集合
     * @param collectionName
     * @param configName
     * @throws IOException
     * @throws SolrServerException
     */
    public static void createCollection(String collectionName,String configName)throws IOException,SolrServerException{
        SolrClient solrClient=new CloudSolrClient(ZK_HOST);
        CollectionAdminRequest.Create create=new CollectionAdminRequest.Create();
        create.setConfigName(configName);
        create.setCollectionName(collectionName);
        create.setNumShards(4);//分片
        create.setMaxShardsPerNode(4);//每个节点最多持有片
        create.setReplicationFactor(2);//复制
        CollectionAdminResponse response=create.process(solrClient);
        System.out.println(response);
        solrClient.close();
    }
    public static void main(String[] args){
        try {
            String collectionName="shb1026";
            String config="solrAndHbase";
//            String configPath = "G:\\config\\solr_conf\\nlp\\";
//            uploadConfig(config, configPath);
//            deleteCollection(collectionName);
            createCollection(collectionName,config);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

 

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics