solrJ操作schema文件

package com.uniclues.solr;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.request.CollectionAdminRequest.Delete;
import org.apache.solr.client.solrj.request.schema.SchemaRequest;
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
import org.apache.solr.client.solrj.response.schema.SchemaResponse;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.NamedList;


import com.uniclues.dbsearch.dbconfig.model.Column;

/**
 * Copyright (C) 2018 Uniclues
 * 
 * Solr Collection 工具类
 *
 * @author Zhanglq
 * @Date 2018-09-04
 * @version 1.00
 */
public class CollectionManager {

    private static Logger log = Logger.getLogger(CollectionManager.class);
    public final static String groupFiledNamePostfix = "_singleterm";
    /**
     * 创建Collection
     */
    public static void createCollection(String collection, List<Column> columns) throws Exception {
        // 创建collection
        createCollection(collection);
        // 定义Field
        createFields(collection, columns);
        System.out.println("CollectionManager.createCollection");
    }
    
    public static void createCollection(String collection) throws Exception {
        if(isExist(collection)) {
            return ;
        } else {
            setZKConfig(collection);
            // 创建Collection
            // collection不存在
            CollectionAdminRequest.Create create = CollectionAdminRequest.Create.createCollection(collection,collection+"_config",
                    3, 2);
            // 配置Collection
            create.setMaxShardsPerNode(3);
            //Properties properties=new Properties();
            //properties.setProperty("name", collection+"_shard1_replica_n2");
            //properties.setProperty("schema", "managed-schema"+collection);
            //properties.setProperty("config", "solrconfig.xml");
            //create.getProperties();
            //create.setProperties(properties);
            //create.setConfigName(collection);
            CollectionAdminResponse response = create.process(ClientManager.getInstance().getSearchClient());
            
            System.out.println(response);
        }
        
    
        
        System.out.println("CollectionManager.createCollection");
    }
    

    /**
     * 根据数据表字段创建Field
     */
    public static void createFields(String collection, List<Column> columns) throws Exception {
        System.out.println("CollectionManager.createFields");
        List<String> schemaFields = getSchemaFields(collection);
        List<SchemaRequest.Update> list = new ArrayList<SchemaRequest.Update>();
        for(Column column : columns) {
            // schema中没有该字段
            if(schemaFields != null && !schemaFields.contains(column.getColumnZhName())) {
                // 增加到字段到Collection

                Map<String, Object> fieldAttributes=new HashMap<>();
                fieldAttributes.put("name", column.getColumnZhName()+groupFiledNamePostfix);
                fieldAttributes.put("type", "string");
                fieldAttributes.put("indexed", "true");
                fieldAttributes.put("stored", "true");
                SchemaRequest.AddField addField=new SchemaRequest.AddField(fieldAttributes);
                //SchemaResponse.UpdateResponse response=addField.process(ClientManager.getInstance().getSearchClient(),collection);
                list.add(addField);
                fieldAttributes=new HashMap<>();
                fieldAttributes.put("name", column.getColumnZhName());
                fieldAttributes.put("type", "text_general");
                fieldAttributes.put("indexed", "true");
                fieldAttributes.put("stored", "true");
                addField=new SchemaRequest.AddField(fieldAttributes);
                //response=addField.process(ClientManager.getInstance().getSearchClient(),collection);
                list.add(addField);
            }
        }
        SchemaRequest.MultiUpdate multiUpdateRequest = new SchemaRequest.MultiUpdate(list);
        SchemaResponse.UpdateResponse multipleUpdatesResponse = multiUpdateRequest.process(ClientManager.getInstance().getSearchClient(),collection);
        System.out.println(multipleUpdatesResponse.getRequestUrl()+" : "+multipleUpdatesResponse.getResponseHeader());
    }
    /**
     *使用copyFields创建union域
     * 添加CopyField 复制域   主要参数source、dest,复制与只要将多个域组合成一个域
     */
    public static void createCopyFields(String collection, List<Column> columns) {
        System.out.println("CollectionManager.createCopyFields");
        List<String> sorecesCopyFields = getSchemaCopyFields(collection);
        List<String> destFields=new ArrayList<>();
        for(Column column : columns) {
            if(!sorecesCopyFields.contains(column.getColumnZhName())) {
                destFields.add(column.getColumnZhName()+groupFiledNamePostfix);
            }
        }
          SchemaRequest.AddCopyField addCopyField=new SchemaRequest.AddCopyField("union", destFields);
            
            try {
                SchemaResponse.UpdateResponse response = addCopyField.process(ClientManager.getInstance().getSearchClient(),collection );
                NamedList<Object> result=response.getResponse();
                System.out.println(result.toString());
            } catch (SolrServerException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
    /**
     * 查看所有copyFields的union域
     */
    public static List<String> getSchemaCopyFields(String collection) {
//        String fieldname="name";
        List<String> sources=new ArrayList<>();
        ModifiableSolrParams params=new ModifiableSolrParams();
        params.add("wt","json");
        // 设置返回sourcefield信息
        params.add("source.fl","name");
        //params.add("dest.fl","union");
        SchemaRequest.CopyFields allCopyFields=new SchemaRequest.CopyFields(params);
        try {
            SchemaResponse.CopyFieldsResponse response = allCopyFields.process(ClientManager.getInstance().getSearchClient(), collection);
            NamedList<Object>  result=response.getResponse();
            List<NamedList<Object>> copyFields=(List<NamedList<Object>>)result.get("copyFields");
            for (NamedList<Object> copyField : copyFields) {
                for (Entry<String, Object> entry : copyField) {
                    String key=entry.getKey();
                    Object val=entry.getValue();
                    if(key.equals("source")) {
                        sources.add(val.toString());
                    }
                }
            }
            
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
        return sources;
    }
     /**
       * 删除Field
       */
      public static void deleteField(String connection,String fieldName) {
          SchemaRequest.DeleteField  deleteField=new SchemaRequest.DeleteField(fieldName);
            try {
                SchemaResponse.UpdateResponse response=deleteField.process(ClientManager.getInstance().getSearchClient(), connection);
               System.out.println( response.getRequestUrl());
        } catch (Exception e) {
            System.out.println(fieldName+"schema文件中的删除失败");
            e.printStackTrace();
        }
          
      }
      /**
       * 更新Field
       */
      public static void updateField(String collection) {
          
          Map<String, Object> fieldAttributes=new HashMap<>();
          fieldAttributes.put("name", "product_name");
          fieldAttributes.put("type", "date");
          fieldAttributes.put("stored", "true");
          fieldAttributes.put("omitNorms", true);
          
          SchemaRequest.ReplaceField replaceField=new SchemaRequest.ReplaceField(fieldAttributes);
            try {
                SchemaResponse.UpdateResponse response=replaceField.process(ClientManager.getInstance().getSearchClient(), collection);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
      
      
      
    /**
     * 检查Collection是否存在
     */
    private static boolean isExist(String collection) throws Exception {
       boolean isConnection=ClientManager.getInstance().getSearchClient().getZkStateReader().getClusterState().hasCollection(collection);
       if(isConnection) {
           System.out.println(collection+"CollectionManager.isExist");
       }else {
           System.out.println(collection+"CollectionManager.totExist");
       }
        return isConnection;
    }
    
    /**
     * 配置Collection
     */
    private static void setZKConfig(String collection) throws Exception {
        //先创建并且上传collectionConfig
//        CollectionConfigManager.downConfig(collection);
//        CollectionConfigManager.upConfig(collection);
        if(!CollectionConfigManager.exists(collection)){
            CollectionConfigManager.copyDefaultConfig(collection);
        }
        System.out.println("CollectionManager.setZKConfig");
    }

    /**
     * 返回collection所有Field的定义信息
     * @param collection
     * @return
     * @throws Exception
     */
    private static List<String> getSchemaFields(String collection) throws Exception {
        
        //System.out.println("CollectionManager.getSchemaFields");
        List<String> AllField=new ArrayList<>();
         ModifiableSolrParams params=new ModifiableSolrParams();
           //是否显示域类型的默认信息
           params.add("showDefaults","false");
           //是否返回动态域的定义信息
           params.add("includeDynamic","true");
           //指定返回那些域的定义信息
           params.add("f1","name,_version_");
           SchemaRequest.Fields allFields=new SchemaRequest.Fields(params);
           SchemaResponse.FieldsResponse response;
        try {
            response = allFields.process(ClientManager.getInstance().getSearchClient(), collection);
            NamedList<Object> result=response.getResponse();
              @SuppressWarnings("unchecked")
            List< NamedList<Object> > fields=(List<NamedList<Object>>)result.get("fields");
              for (NamedList<Object>  field : fields) {
                 for (Entry<String, Object> entry : field) {
                     String key=entry.getKey();
                     Object val=entry.getValue();
                     if(key.equals("name")) {
                         if(val==null) {
                          AllField.add("");
                         }else {
                         AllField.add(val.toString());
                         }
                     }
                }
                
            }
             
        } catch (SolrServerException e) {
            e.printStackTrace();
            log.error("返回collection所有Field的定义信息异常!", e);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("返回collection所有Field的定义信息异常!", e);
        }
           
        return AllField;
    }
    
    /**
     * 删除collection
     * @param collection
     * @throws Exception
     */
    public static void deleteCollection(String collection) throws Exception {
        if(CollectionManager.isExist(collection)){
            System.out.println("***collection:"+collection);
            Delete delete = CollectionAdminRequest.deleteCollection(collection);
            delete.process(ClientManager.getInstance().getSearchClient());
        }else{
            System.out.println("***找不到要删除的collection["+collection+"]***");
        }
    }
    
    
    public static void main(String[] args) {
        //CollectionAdminRequest.MoveReplica.
        //CollectionAdminRequest.
//        try {
//            ConcurrentUpdateSolrClient solrClient = ClientManager.getInstance().getUpdateClient("SA_VW_XGDZ_IN_NN109_TXL");
//            List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
//            for(int i=1; i<20; i++){
//                SolrInputDocument doc = new SolrInputDocument();
//                //doc.setField("name", "张三");
//                doc.setField("uniclues_id", UUID.randomUUID().toString());
//                docs.add(doc);
//            }
//            
//            UpdateResponse updateResponse = solrClient.add(docs);
//            System.out.println("Add doc size" + docs.size() + " result:" + updateResponse.getStatus() + " Qtime:" + updateResponse.getQTime());
//            UpdateResponse updateResponse2 = solrClient.commit();
//            System.out.println("Add doc size" + docs.size() + " result:" + updateResponse2.getStatus() + " Qtime:" + updateResponse2.getQTime());
//            solrClient.close();
//            System.out.println("添加");
//        } catch (SolrServerException | IOException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        
        
        try {
            deleteCollection("SA.VW_XGDZ_IN_NNJDCXX");
            System.out.println("ook");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞腾创客

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值