Use Java code to curd Solrcloud data in a zookeeper cluster

Source: Internet
Author: User
Tags solr zookeeper

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.