solrj操纵CloudSolrServer的一般步骤
来源:互联网 发布:相册软件哪个好 编辑:程序博客网 时间:2024/06/05 23:59
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();
1package cn.wzb.cloud; 2 3import java.io.IOException; 4import java.net.MalformedURLException; 5import java.util.ArrayList; 6import java.util.Collection; 7 8import org.apache.solr.client.solrj.SolrQuery; 9import org.apache.solr.client.solrj.SolrServer; 10import org.apache.solr.client.solrj.SolrServerException; 11import org.apache.solr.client.solrj.impl.CloudSolrServer; 12import org.apache.solr.client.solrj.response.QueryResponse; 13import org.apache.solr.common.SolrDocument; 14import org.apache.solr.common.SolrDocumentList; 15import org.apache.solr.common.SolrInputDocument; 16import org.apache.solr.common.cloud.CloudState; 17import org.apache.solr.common.cloud.ZkStateReader; 18 19 20 21 22 23public class TestCloudSolr { 24 private static CloudSolrServer cloudSolrServer; 25 26 private static synchronized CloudSolrServer getCloudSolrServer(final String zkHost) { 27 if(cloudSolrServer == null) { 28 try { 29 cloudSolrServer = new CloudSolrServer(zkHost); 30 }catch(MalformedURLException e) { 31 System.out.println("The URL of zkHost is not correct!! Its form must as below:\n zkHost:port"); 32 e.printStackTrace(); 33 }catch(Exception e) { 34 e.printStackTrace(); 35 } 36 } 37 38 return cloudSolrServer; 39 } 40 41 private void addIndex(SolrServer solrServer) { 42 try { 43 SolrInputDocument doc1 = new SolrInputDocument(); 44 doc1.addField("id", "1"); 45 doc1.addField("name", "张民"); 46 47 48 SolrInputDocument doc2 = new SolrInputDocument(); 49 doc2.addField("id", "2"); 50 doc2.addField("name", "刘俊"); 51 52 53 SolrInputDocument doc3 = new SolrInputDocument(); 54 doc3.addField("id", "3"); 55 doc3.addField("name", "刘俊2"); 56 57 58 Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); 59 docs.add(doc1); 60 docs.add(doc2); 61 docs.add(doc3); 62 63 solrServer.add(docs); 64 solrServer.commit(); 65 66 }catch(SolrServerException e) { 67 System.out.println("Add docs Exception !!!"); 68 e.printStackTrace(); 69 }catch(IOException e){ 70 e.printStackTrace(); 71 }catch (Exception e) { 72 System.out.println("Unknowned Exception!!!!!"); 73 e.printStackTrace(); 74 } 75 76 } 77 78 79 public void search(SolrServer solrServer, String String) { 80 SolrQuery query = new SolrQuery(); 81 query.setQuery(String); 82 83 try { 84 QueryResponse response = solrServer.query(query); 85 SolrDocumentList docs = response.getResults(); 86 87 System.out.println("文档个数:" + docs.getNumFound()); 88 System.out.println("查询时间:" + response.getQTime()); 89 90 for (SolrDocument doc : docs) { 91 String name = (String) doc.getFieldValue("name"); 92 String id = (String) doc.getFieldValue("id"); 93 System.out.println("id: " + id); 94 System.out.println("name: " + name); 95 System.out.println(); 96 } 97 } catch (SolrServerException e) { 98 e.printStackTrace(); 99 } catch(Exception e) { 100 System.out.println("Unknowned Exception!!!!"); 101 e.printStackTrace(); 102 } 103 } 104 105 public void deleteAllIndex(SolrServer solrServer) { 106 try { 107 solrServer.deleteByQuery("*:*");// delete everything! 108 solrServer.commit(); 109 }catch(SolrServerException e){ 110 e.printStackTrace(); 111 }catch(IOException e) { 112 e.printStackTrace(); 113 }catch(Exception e) { 114 System.out.println("Unknowned Exception !!!!"); 115 e.printStackTrace(); 116 } 117 } 118 119 /** 120 * @param args 121 */ 122 public static void main(String[] args) { 123 final String zkHost = "localhost:2181"; 124 final String defaultCollection = "collectionOne"; 125 final int zkClientTimeout = 20000; 126 final int zkConnectTimeout = 1000; 127 128 CloudSolrServer cloudSolrServer = getCloudSolrServer(zkHost); 129 System.out.println("The Cloud SolrServer Instance has benn created!"); 130 131 cloudSolrServer.setDefaultCollection(defaultCollection); 132 cloudSolrServer.setZkClientTimeout(zkClientTimeout); 133 cloudSolrServer.setZkConnectTimeout(zkConnectTimeout); 134 135 136 cloudSolrServer.connect(); 137 System.out.println("The cloud Server has been connected !!!!"); 138 139 ZkStateReader zkStateReader = cloudSolrServer.getZkStateReader(); 140 CloudState cloudState = zkStateReader.getCloudState(); 141 System.out.println(cloudState); 142 143 //测试实例! 144 TestCloudSolr test = new TestCloudSolr(); 145 System.out.println("测试添加index!!!"); 146 //添加index 147 test.addIndex(cloudSolrServer); 148 149 System.out.println("测试查询query!!!!"); 150 test.search(cloudSolrServer, "id:*"); 151 152 System.out.println("测试删除!!!!"); 153 test.deleteAllIndex(cloudSolrServer); 154 System.out.println("删除所有文档后的查询结果:"); 155 test.search(cloudSolrServer, "*:*"); 156 157 158 159 // release the resource 160 cloudSolrServer.shutdown(); 161 162 } 163 164}
测试结果如下:
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
- solrj操纵CloudSolrServer的一般步骤
- Solrj操作Solr4.0 中CloudSolrServer的一般过程
- Solrj操作Solr4.0/Solr4.3中CloudSolrServer的一般过程
- Solr笔记四之Solrj创建索引和搜索的一般步骤
- Solr笔记四之Solrj创建索引和搜索的一般步骤
- Solrj操作Solr4.0 中CloudSolrServer
- SolrJ 操作HttpSolrServer, ConcurrentUpdateSolrServer和CloudSolrServer
- Solrj操作Solr4.0 中CloudSolrServer
- SolrJ 操作HttpSolrServer, ConcurrentUpdateSolrServer和CloudSolrServer
- Solrj操作Solr4.0 中CloudSolrServer
- solrj 查询SolrServer的一般流程
- CloudSolrServer
- DebugVista的一般步骤
- DebugVista的一般步骤
- 算法的一般步骤
- 一般的安装步骤
- 数据库连接的一般步骤
- Org.apache.solr.client.solrj.impl.CloudSolrServer$RouteException:DEF_CONTENT_CHARST 错误解决
- Android 发布开源项目到jcenter
- iOS开发中sqlite数据库的基本使用
- ButterKnife--View注入框架
- c语言:递归求最大公约数
- 基于Redux架构的单页应用开发总结(二)
- solrj操纵CloudSolrServer的一般步骤
- ubuntu下crontab 定时执行php脚本
- css3选择器
- 【Android开发细节】之【@id与@+id区别】
- Spring的IOC原理[通俗解释一下]
- vs2013快捷键的使用
- Linux内核---多线程
- Caused by: org.xml.sax.SAXParseException; systemId: file:/opt/apache-nutch-2.2.1/runtime/local/conf/
- leetcode:60. Permutation Sequence