1 Importing related pom dependencies
<Dependencies> <Dependency> <groupId>Org.apache.solr</groupId> <Artifactid>Solr-solrj</Artifactid> <version>4.10.2</version> </Dependency> <Dependency> <groupId>Commons-logging</groupId> <Artifactid>Commons-logging-api</Artifactid> <version>1.1</version> </Dependency> </Dependencies>
2 Write code to complete curd
//Add index@Test Public voidCreateindextosolrcloud ()throwsIOException, solrserverexception {//Create a service object that connects Solrcloud//String Zkhost: The address of the zookeeper cluster needs to be passedString zkhost = "192.168.44.28:2181,192.168.44.29:2181,192.168.44.30:2181"; Cloudsolrserver Solrserver=NewCloudsolrserver (zkhost); //set which SOLR index library to connect toSolrserver.setdefaultcollection ("Collection2"); //Optional Parameters//set the time to connect zookeeperSolrserver.setzkclienttimeout (5000); //set the time to get and SOLR connectionsSolrserver.setzkconnecttimeout (5000); //perform a Get connectionSolrserver.connect (); //Add index OperationSolrinputdocument document =Newsolrinputdocument (); Document.addfield ("id", "1"); Document.addfield ("Name", "Hello Solrcloud"); Solrserver.add (document); //Execute CommitSolrserver.commit (); } //Delete Index@Test Public voidDeleteindex ()throwsIOException, solrserverexception {//Create a service object that connects Solrcloud//String Zkhost: The address of the zookeeper cluster needs to be passedString zkhost = "192.168.44.28:2181,192.168.44.29:2181,192.168.44.30:2181"; Cloudsolrserver Solrserver=NewCloudsolrserver (zkhost); //set which SOLR index library to connect toSolrserver.setdefaultcollection ("Collection2"); //Optional Parameters//set the time to connect zookeeperSolrserver.setzkclienttimeout (5000); //set the time to get and SOLR connectionsSolrserver.setzkconnecttimeout (5000); //perform a Get connectionSolrserver.connect (); //Delete IndexSolrserver.deletebyid ("1"); //Execute CommitSolrserver.commit (); } //Enquiry@Test Public voidQueryindex ()throwssolrserverexception {//Create a service object that connects Solrcloud//String Zkhost: The address of the zookeeper cluster needs to be passedString zkhost = "192.168.44.28:2181,192.168.44.29:2181,192.168.44.30:2181"; Cloudsolrserver Solrserver=NewCloudsolrserver (zkhost); //set which SOLR index library to connect toSolrserver.setdefaultcollection ("Collection2"); //Optional Parameters//set the time to connect zookeeperSolrserver.setzkclienttimeout (5000); //set the time to get and SOLR connectionsSolrserver.setzkconnecttimeout (5000); //perform a Get connectionSolrserver.connect (); //Add a query operationSolrquery query =NewSolrquery ("*:*"); Queryresponse Response=solrserver.query (query); Solrdocumentlist Documents=response.getresults (); for(solrdocument document:documents) {Object ID= Document.get ("id"); Object name= Document.get ("name"); System.out.println (ID+ "--" +name); } }
Use Java code to curd Solrcloud data in a zookeeper cluster