CloudSolrServer
7217 ワード
solrcloudが出るまでsolrjでsolrserverを接続するには、プログラム自体で一貫性hashを実現する必要がある.新しいバージョンのsolrはcloudの配置方式をサポートし、lbとshardingの機能(CloudSolrServerクラスを通じてcloudを接続する)を自動的に実現することができ、以下のコードでテストに必要なjarパッケージを以下のようにすることができます.
sample code:
菜光光さんのブログ
」ブログでは、必ずこの出典を残しておいてくださいhttp://caiguangguang.blog.51cto.com/1652935/1433748
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+"
" );
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+"
" );
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
");
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();
}
}
菜光光さんのブログ
」ブログでは、必ずこの出典を残しておいてくださいhttp://caiguangguang.blog.51cto.com/1652935/1433748