Solrj操做Solr4.0 中CloudSolrServer的通常過程

Solrj操做Solr4.0 中CloudSolrServer的通常過程

1、準備工做:建立eclipse工程

1.        下載solr4.0_src的源碼包,解壓後,進入目錄中, html

在命令行執行:ant eclipse 則能夠生成一個eclipse工程,會在目錄中多了關於eclipse工程的文件:.classpath 和 .project; java

在eclipse導入該生成的工程(lucene-solr),工程名爲lucene-solr,能夠看到其所有源碼; node

編寫本身的代碼,就能測試了! apache

注意:須要安裝ant 工具,以及ivy包,搜一下,去appache下來,配置好,才能編譯經過。Ant編譯時間有點長。關於ant(another neat tool)再也不過多介紹,網上有不少教程。只要明白 ant至關於一個make的工具,其解析biuld.xml文件的相關指令。 tomcat

2.        下載solr4.0 zip 包,將其中的全部jar包都添加你的eclipse工程中。(之因此是全部,以防萬一編譯不過,省的猜缺乏那個包,找呀找的!) app

3.        結合網上關於solr的搭建教程,進行搭建solr,能夠用tomcat,也能夠用jetty。 eclipse

我用的jetty+zookeeper,(因爲對tomcat的集羣配置不瞭解), 工具

zookeeper是獨立的zookeeper,而不是jetty內嵌的zookeeper;能夠從appache上直接下載! 性能

4.        進行相關集羣的配置,配置集羣,用在工程中用solrj操縱solrCloud 測試

2、用solrj操縱CloudSolrServer的通常步驟

1.        建立CloudSolrServer的實例: 2種方式:

(a)    CloudSolrServer cloudSolrServer=  new CloudSolrServer(zkHostURL);

(b)    CloudSolrserver.cloudSolrServer =  new CloudSolrServer(zkHostURL,lbHttpSolrServer);

2.        對CloudSolrServer實例進行設置

(a)    cloudSolrServer.setDefaultCollection(defaultCollectionName);

(b)    cloudSolrServer.setzkClientTimeout(zkClientTimeout);

(c)    cloudSolrServer.setzkConnectTimeout(zkConnectionTimeout);

3.        將cloudSolrServer實例鏈接到zookeeper

(a)    cloudSolrServer.connect();

4.        CloudSolrServer 的實例cloudSolrServer 實例化、鏈接完成,進而能夠對其進行add、query、delete操做。

(a)    建index:準備document,最好批量添加,有利於提升性能。添加文檔的字段於solr中配置文件schema.xml有關,須要對其設置。

(b)    經過SolrQuery 能夠對cloudSolrServer實例進行各類查找操做。

(c)    Delete操做能夠經過id、Query的結果進行delete。

5.        操做結束,關閉CloudSolrServer實例,以釋放資源。

cloudSolrServer.shutdown();

實例代碼以下:


package cn.wzb.cloud;

import java.io.IOException;
import java.net.MalformedURLException;
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.solr.common.cloud.CloudState;
import org.apache.solr.common.cloud.ZkStateReader;





public class TestCloudSolr {	
	private static CloudSolrServer cloudSolrServer;
	
	private  static synchronized CloudSolrServer getCloudSolrServer(final String zkHost) {
		if(cloudSolrServer == null) {
			try {
				cloudSolrServer = new CloudSolrServer(zkHost);
			}catch(MalformedURLException e) {
				System.out.println("The URL of zkHost is not correct!! Its form must as below:\n zkHost:port");
				e.printStackTrace();
			}catch(Exception e) {
				e.printStackTrace();				
			}
		}
		
		return cloudSolrServer;
	}
	
	private void addIndex(SolrServer solrServer) {		
		try {
			SolrInputDocument doc1 = new SolrInputDocument();
			doc1.addField("id", "1");
			doc1.addField("name", "張民");
		
	
			SolrInputDocument doc2 = new SolrInputDocument();
			doc2.addField("id", "2");
			doc2.addField("name", "劉俊");
			
	
			SolrInputDocument doc3 = new SolrInputDocument();
			doc3.addField("id", "3");
			doc3.addField("name", "劉俊2");
			
	
			Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
			docs.add(doc1);
			docs.add(doc2);
			docs.add(doc3);
			
			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 String) {		
		SolrQuery query = new SolrQuery();
		query.setQuery(String);

		try {
			QueryResponse response = solrServer.query(query);
			SolrDocumentList docs = response.getResults();

			System.out.println("文檔個數:" + docs.getNumFound());
			System.out.println("查詢時間:" + response.getQTime());

			for (SolrDocument doc : docs) {
				String name = (String) doc.getFieldValue("name");
				String id = (String) doc.getFieldValue("id");
				System.out.println("id: " + id);
				System.out.println("name: " + name);
				System.out.println();
			}
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch(Exception e) {
			System.out.println("Unknowned Exception!!!!");
			e.printStackTrace();
		}
	}
	
    public void deleteAllIndex(SolrServer solrServer) {
    	try {
    		solrServer.deleteByQuery("*:*");// delete everything!
    		solrServer.commit();
    	}catch(SolrServerException e){
    		e.printStackTrace();
    	}catch(IOException e) {
    		e.printStackTrace();
    	}catch(Exception e) {
    		System.out.println("Unknowned Exception !!!!");
    		e.printStackTrace();
    	}
    }
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {		
	    final String zkHost = "localhost:2181";		
		final String  defaultCollection = "collectionOne";
		final int  zkClientTimeout = 20000;
		final int zkConnectTimeout = 1000;
		
		CloudSolrServer cloudSolrServer = getCloudSolrServer(zkHost);	    
	    System.out.println("The Cloud SolrServer Instance has benn created!");
	    
	    cloudSolrServer.setDefaultCollection(defaultCollection);
	    cloudSolrServer.setZkClientTimeout(zkClientTimeout);
	    cloudSolrServer.setZkConnectTimeout(zkConnectTimeout);  
	    
	    
		cloudSolrServer.connect();
		System.out.println("The cloud Server has been connected !!!!");
		
		ZkStateReader zkStateReader = cloudSolrServer.getZkStateReader();
		CloudState cloudState  = zkStateReader.getCloudState();
		System.out.println(cloudState);
		
		//測試實例!
		TestCloudSolr test = new TestCloudSolr();		
		System.out.println("測試添加index!!!");		
		//添加index
		test.addIndex(cloudSolrServer);
		
		System.out.println("測試查詢query!!!!");
		test.search(cloudSolrServer, "id:*");
		
		System.out.println("測試刪除!!!!");
		test.deleteAllIndex(cloudSolrServer);
		System.out.println("刪除全部文檔後的查詢結果:");
		test.search(cloudSolrServer, "*:*");	
		
		
				
		 // release the resource 
	    cloudSolrServer.shutdown();
 
	}

}



.測試結果以下:

SLF4J: Failed to load class"org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation(NOP) logger implementation

SLF4J: Seehttp://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

The Cloud SolrServer Instancehas benn created!

The cloud Server has beenconnected !!!!

live nodes:[jiqh:8502_solr,jiqh:8501_solr] collections:{collectionOne={slice1=Slice [shards={jiqh:8501_solr_core_collectionOne_slice1_shard1=shard=slice1

roles=null

leader=true

state=active

core=core_collectionOne_slice1_shard1

collection=collectionOne

node_name=jiqh:8501_solr

base_url=http://jiqh:8501/solr

,jiqh:8502_solr_core_collectionOne_slice1_shard2=shard=slice1

roles=null

state=active

core=core_collectionOne_slice1_shard2

collection=collectionOne

node_name=jiqh:8502_solr

base_url=http://jiqh:8502/solr

}, name=slice1], slice2=Slice[shards={jiqh:8501_solr_core_collectionOne_slice2_shard2=shard=slice2

roles=null

leader=true

state=active

core=core_collectionOne_slice2_shard2

collection=collectionOne

node_name=jiqh:8501_solr

base_url=http://jiqh:8501/solr

,jiqh:8502_solr_core_collectionOne_slice2_shard1=shard=slice2

roles=null

state=active

core=core_collectionOne_slice2_shard1

collection=collectionOne

node_name=jiqh:8502_solr

base_url=http://jiqh:8502/solr

}, name=slice2]}}

測試添加index!!!

測試查詢query!!!!

文檔個數:3

查詢時間:15

id: 1

name: 張民

id: 2

name: 劉俊

id: 3

name: 劉俊2

測試刪除!!!!

刪除全部文檔後的查詢結果:

文檔個數:0

查詢時間:0

相關文章
相關標籤/搜索