solrcloud sample

  在solrcloud出来之前,如果通过solrj连接solrserver,需要程序自己实现一致性hash.
新版本的solr支持cloud的部署方式,可以自动实现lb和sharding的功能(通过CloudSolrServer类连接cloud),可以用下面代码做测试
需要的jar包如下:

apache-solr-solrj.jar
apache-solr-core.jar
zookeeper.jar   
commons-logging.jar 
apache-logging-log4j.jar 
httpclient.jar  
httpcore.jar
slf4j-api.jar
slf4j-nop.jar

sample code:

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Collection; 
import org.apache.solr.client.solrj.SolrQuery; 
import org.apache.solr.client.solrj.SolrServer; 
import org.apache.solr.client.solrj.SolrServerException; 
import org.apache.solr.client.solrj.impl.CloudSolrServer; 
import org.apache.solr.client.solrj.response.QueryResponse; 
import org.apache.solr.common.SolrDocument; 
import org.apache.solr.common.SolrDocumentList; 
import org.apache.solr.common.SolrInputDocument; 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;   
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.ZkStateReader;
   
    public class SolrCloudTest {       
       public static final Log LOG = LogFactory.getLog(SolrCloudTest.class);
        private static CloudSolrServer cloudSolrServer;             
        private  static synchronized CloudSolrServer getCloudSolrServer( final String zkHost) {   
               LOG.info("connect to :" +zkHost+"\n" );
            if(cloudSolrServer == null) {   
                try {   
                    cloudSolrServer = new CloudSolrServer(zkHost);   
                } catch(Exception e) {   
                    e.printStackTrace();                   
                }   
            }                   
            return cloudSolrServer ;   
        }   
           
        private void addIndex(SolrServer solrServer) {         
            try {
              Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
               for (int i = 0;i<=200;i++){
                     SolrInputDocument doc = new SolrInputDocument();
                     String key = "";
                     key = String. valueOf(i);
                     doc.addField( "id", key);
                     doc.addField( "test_s", key+"value" );
                     docs.add(doc);             
              }    
                LOG.info("docs info:" +docs+"\n" );
                System. out.println("docs length " + docs.size());
                solrServer.add(docs);              
                solrServer.commit();                   
            } catch(SolrServerException e) {   
                System. out.println("Add docs Exception !!!" );   
                e.printStackTrace();           
            } catch(IOException e){   
                e.printStackTrace();   
            } catch (Exception e) {   
                System. out.println("Unknowned Exception!!!!!" );   
                e.printStackTrace();   
            }                         
        }                        
        public void search(SolrServer solrServer, String Str) {         
            SolrQuery query = new SolrQuery();
            query.setRows(20); //默认row是10,只返回10条,如果设置query.setRows(Integer. MAX_VALUE)会报错,默认最大为2147483391
            query.setQuery(Str);   
            try { 
               LOG.info("query string: " + Str);
              System. out.println("query string: " + Str);
                QueryResponse response = solrServer.query(query);   
                SolrDocumentList docs = response.getResults();
                System. out.println(docs);
                System. out.println(docs.size());
                System. out.println("doc num:" + docs.getNumFound());   
                System. out.println("elapse time:" + response.getQTime());           
                for (SolrDocument doc : docs) {   
                    String area = (String) doc.getFieldValue( "test_s");   
                    String id = (String) doc.getFieldValue( "id");   
                    System. out.println("id: " + id);   
                    System. out.println("tt_s: " + area);   
                    System. out.println();   
                }   
            } catch (SolrServerException e) {   
                e.printStackTrace();   
            } catch(Exception e) {   
                e.printStackTrace();   
            }   
        }   
           
        public void deleteAllIndex(SolrServer solrServer) {   
            try {   
                solrServer.deleteByQuery( "*:*");
                solrServer.commit();   
            } catch(SolrServerException e){   
                e.printStackTrace();   
            } catch(IOException e) {   
                e.printStackTrace();   
            } catch(Exception e) {   
                e.printStackTrace();   
            }   
        }           
        public static void main(String[] args) {     
                final String zkHost = "xxxxx:8080" ;        
                final String  defaultCollection = "test" ;   
                final int   zkClientTimeout = 10000;   
                final int zkConnectTimeout = 10000;                     
                CloudSolrServer cloudSolrServer = getCloudSolrServer(zkHost);                    
                cloudSolrServer.setDefaultCollection(defaultCollection);   
                cloudSolrServer.setZkClientTimeout(zkClientTimeout);   
                cloudSolrServer.setZkConnectTimeout(zkConnectTimeout);
                try{
                     cloudSolrServer.connect();
                    System. out.println("connect solr cloud zk sucess" );           
                } catch (Exception e){   
                      LOG.error("connect to collection " +defaultCollection+" error\n");
                     System. out.println("error message is:" +e);
                     e.printStackTrace();
                     System. exit(1);
                }
                      //ZkStateReader zkStateReader = cloudSolrServer.getZkStateReader();
                      //ClusterState cloudState  = zkStateReader.getClusterState();
                      //System.out.println(cloudState);
                     
                SolrCloudTest solrt = new SolrCloudTest();
                System. out.println("=====" );
                try{
                     solrt.addIndex(cloudSolrServer);
                } catch(Exception e){
                     e.printStackTrace();
                }
                solrt.search(cloudSolrServer, "id:*");   
                //solrt.deleteAllIndex(cloudSolrServer);         
                System. out.println("hashCode" +solrt.hashCode());  
                cloudSolrServer.shutdown();           
        }         
    }

你可能感兴趣的:(solrCloud,实例)