Solr JAVA CloudSolrClient - Getting Started

This article is about getting started with a Java-Maven Project that connects to SolrCloud instance using newly available CloudSolrClient.

1. Quickly Setting up Solr Cloud

2. Creating a Java-Maven Project and setting up POM

3. Index to SolrCloud

4. Search over SolrCloud

5. Ping Solr Cloud to check status.

 

1. Quickly Setting up Solr Cloud

If you do not have a solr cloud instance running on your machine, then there are two ways of setting it up.  i. Via Fusion  ii. Direct Solr download


i. Via Fusion

  • Download Fusion from Lucidworks site
  • Unzip and navigate to /fusion/bin folder. Start it. ./fusion start
  • Once you have the fusion started, you also have the SolrCloud instance setup and started. Default running ports are solr-8983 and zookeeper -9983

ii.Direct Solr

  • Download Solr 5 from the Apache Solr site 
  • Unzip and navigate to the unzip folder
  • cd /Users/rajanishivarajmaski1/Lucidworks/solr-5.0.0/bin
  • And command to setup and start solr cloud instance, execute the below command ./solr start -e cloud -noprompt 

Now you have the SolrCloud up and running with its zookeeper working at host:local and port:9983(by default)



2. Creating a Java-Maven Project and setting up POM

I am assuming that you already have Maven configured on your IDE (Eclipse/Intellij). Create a simple archtype maven project. Go to POM and add the following dependencies. I have also attached the pom.xml for reference.

<dependency>
  <groupId>org.apache.solr</groupId>
  <artifactId>solr-solrj</artifactId>
  <version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>



3. Index to SolrCloud

Assumptions.  1) You have the SolrCloud instance up and running 2)Zookeeper is on localhost and running on default port 9983. Iff you have different host or port, change the zkHost[1] string in the code snippet accordingly

Create a java class by name 'SolrJCloudClientIndex' under /src/main/java package. Have the following code snippets in it.

This code basically talks to zookeeper (cluster manager) and indexes a document object.

public static void main(String[] args) throws SolrServerException, IOException {
String zkHost = "localhost:9983"; [1]
CloudSolrClient server = new CloudSolrClient(zkHost);
server.setDefaultCollection("Test");
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "1234");
doc.addField("name", "A lovely summer holiday");
server.add(doc);
server.commit();
}

Execute/Run the Java class. 


 

4. Search over SolrCloud

Create a java class by name 'SolrJCloudClientSearch' under /src/main/java package. Have the following code snippets in it.

This code talks to zookeeper again[cluster manager], queries from the respective collection as mentioned in the query object [as shown in code snippet[2]]

public static void main(String[] args) throws SolrServerException, IOException {
String zkHost = "127.0.0.1:9983";
CloudSolrClient server = new CloudSolrClient(zkHost);
server = new CloudSolrClient(zkHost);
server.setParser(new XMLResponseParser());
SolrQuery parameters = new SolrQuery();
parameters.set("q", "*:*");
parameters.set("qt", "/select");
parameters.set("collection", "Test");
QueryResponse response = server.query(parameters);
SolrDocumentList list = response.getResults();
System.out.println(list.size());
}

Execute/Run the Java class.  And you should see '1' printed on your console. 

 

5. Ping Solr Cloud to check status

String zkHost = "localhost:9983";
CloudSolrClient server = new CloudSolrClient(zkHost);
server.setDefaultCollection("Test");
SolrPingResponse response = server.ping();
System.out.println(response.getStatus());

Status 0 means solrcloud is in running state. 

 

 

 

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk