|
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;
public> 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+"\n");
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+"\n");
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);
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>
System.out.println("id: " +> System.out.println("tt_s: " + area);
System.out.println();
}
} catch (SolrServerException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
final String zkHost = "192.168.0.2:8080";
final String defaultCollection = "userinfo";
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\n");
System.out.println("error message is:"+e);
e.printStackTrace();
System.exit(1);
}
SolrCloudTest solrt = new SolrCloudTest();
try{
solrt.addIndex(cloudSolrServer);
} catch(Exception e){
e.printStackTrace();
}
solrt.search(cloudSolrServer, "id:*"); ;
cloudSolrServer.shutdown();
}
} |
|
|
|
|
|
|