Solrj操作Solr4.0 中CloudSolrServer

Solrj操作Solr4.0 中CloudSolrServer的一般过程

一、准备工作:创建eclipse工程

1. 下载solr4.0_src的源码包,解压后,进入目录中,

在命令行执行:ant eclipse 则可以生成一个eclipse工程,会在目录中多了关于eclipse工程的文件:.classpath 和 .project;

在eclipse导入该生成的工程(lucene-solr),工程名为lucene-solr,可以看到其全部源码;

编写自己的代码,就能测试了!

注意:需要安装ant 工具,以及ivy包,搜一下,去appache下来,配置好,才能编译通过。Ant编译时间有点长。关于ant(another neat tool)不再过多介绍,网上有很多教程。只要明白 ant相当于一个make的工具,其解析biuld.xml文件的相关指令。

2. 下载solr4.0 zip 包,将其中的所有jar包都添加你的eclipse工程中。(之所以是所有,以防万一编译不过,省的猜缺少那个包,找呀找的!)

3. 结合网上关于solr的搭建教程,进行搭建solr,可以用tomcat,也可以用jetty。

我用的jetty+zookeeper,(由于对tomcat的集群配置不了解),

zookeeper是独立的zookeeper,而不是jetty内嵌的zookeeper;可以从appache上直接下载!

4. 进行相关集群的配置,配置集群,用在工程中用solrj操纵solrCloud

二、用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();

实例代码如下:

  1. package cn.wzb.cloud;
  2. import java.io.IOException;
  3. import java.net.MalformedURLException;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import org.apache.solr.client.solrj.SolrQuery;
  7. import org.apache.solr.client.solrj.SolrServer;
  8. import org.apache.solr.client.solrj.SolrServerException;
  9. import org.apache.solr.client.solrj.impl.CloudSolrServer;
  10. import org.apache.solr.client.solrj.response.QueryResponse;
  11. import org.apache.solr.common.SolrDocument;
  12. import org.apache.solr.common.SolrDocumentList;
  13. import org.apache.solr.common.SolrInputDocument;
  14. import org.apache.solr.common.cloud.CloudState;
  15. import org.apache.solr.common.cloud.ZkStateReader;
  16. public class TestCloudSolr {
  17. private static CloudSolrServer cloudSolrServer;
  18. private static synchronized CloudSolrServer getCloudSolrServer(final String zkHost) {
  19. if(cloudSolrServer == null) {
  20. try {
  21. cloudSolrServer = new CloudSolrServer(zkHost);
  22. }catch(MalformedURLException e) {
  23. System.out.println("The URL of zkHost is not correct!! Its form must as below:\n zkHost:port");
  24. e.printStackTrace();
  25. }catch(Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. return cloudSolrServer;
  30. }
  31. private void addIndex(SolrServer solrServer) {
  32. try {
  33. SolrInputDocument doc1 = new SolrInputDocument();
  34. doc1.addField("id", "1");
  35. doc1.addField("name", "张民");
  36. SolrInputDocument doc2 = new SolrInputDocument();
  37. doc2.addField("id", "2");
  38. doc2.addField("name", "刘俊");
  39. SolrInputDocument doc3 = new SolrInputDocument();
  40. doc3.addField("id", "3");
  41. doc3.addField("name", "刘俊2");
  42. Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
  43. docs.add(doc1);
  44. docs.add(doc2);
  45. docs.add(doc3);
  46. solrServer.add(docs);
  47. solrServer.commit();
  48. }catch(SolrServerException e) {
  49. System.out.println("Add docs Exception !!!");
  50. e.printStackTrace();
  51. }catch(IOException e){
  52. e.printStackTrace();
  53. }catch (Exception e) {
  54. System.out.println("Unknowned Exception!!!!!");
  55. e.printStackTrace();
  56. }
  57. }
  58. public void search(SolrServer solrServer, String String) {
  59. SolrQuery query = new SolrQuery();
  60. query.setQuery(String);
  61. try {
  62. QueryResponse response = solrServer.query(query);
  63. SolrDocumentList docs = response.getResults();
  64. System.out.println("文档个数:" + docs.getNumFound());
  65. System.out.println("查询时间:" + response.getQTime());
  66. for (SolrDocument doc : docs) {
  67. String name = (String) doc.getFieldValue("name");
  68. String id = (String) doc.getFieldValue("id");
  69. System.out.println("id: " + id);
  70. System.out.println("name: " + name);
  71. System.out.println();
  72. }
  73. } catch (SolrServerException e) {
  74. e.printStackTrace();
  75. } catch(Exception e) {
  76. System.out.println("Unknowned Exception!!!!");
  77. e.printStackTrace();
  78. }
  79. }
  80. public void deleteAllIndex(SolrServer solrServer) {
  81. try {
  82. solrServer.deleteByQuery("*:*");// delete everything!
  83. solrServer.commit();
  84. }catch(SolrServerException e){
  85. e.printStackTrace();
  86. }catch(IOException e) {
  87. e.printStackTrace();
  88. }catch(Exception e) {
  89. System.out.println("Unknowned Exception !!!!");
  90. e.printStackTrace();
  91. }
  92. }
  93. /**
  94. * @param args
  95. */
  96. public static void main(String[] args) {
  97. final String zkHost = "localhost:2181";
  98. final String defaultCollection = "collectionOne";
  99. final int zkClientTimeout = 20000;
  100. final int zkConnectTimeout = 1000;
  101. CloudSolrServer cloudSolrServer = getCloudSolrServer(zkHost);
  102. System.out.println("The Cloud SolrServer Instance has benn created!");
  103. cloudSolrServer.setDefaultCollection(defaultCollection);
  104. cloudSolrServer.setZkClientTimeout(zkClientTimeout);
  105. cloudSolrServer.setZkConnectTimeout(zkConnectTimeout);
  106. cloudSolrServer.connect();
  107. System.out.println("The cloud Server has been connected !!!!");
  108. ZkStateReader zkStateReader = cloudSolrServer.getZkStateReader();
  109. CloudState cloudState = zkStateReader.getCloudState();
  110. System.out.println(cloudState);
  111. //测试实例!
  112. TestCloudSolr test = new TestCloudSolr();
  113. System.out.println("测试添加index!!!");
  114. //添加index
  115. test.addIndex(cloudSolrServer);
  116. System.out.println("测试查询query!!!!");
  117. test.search(cloudSolrServer, "id:*");
  118. System.out.println("测试删除!!!!");
  119. test.deleteAllIndex(cloudSolrServer);
  120. System.out.println("删除所有文档后的查询结果:");
  121. test.search(cloudSolrServer, "*:*");
  122. // release the resource
  123. cloudSolrServer.shutdown();
  124. }
  125. }
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值