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();
实例代码如下:
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: Seehttps://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=https://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=https://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=https://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=https://jiqh:8502/solr
}, name=slice2]}}
测试添加index!!!
测试查询query!!!!
文档个数:3
查询时间:15
id: 1
name: 张民
id: 2
name: 刘俊
id: 3
name: 刘俊2
测试删除!!!!
删除所有文档后的查询结果:
文档个数:0
查询时间:0