Created
March 18, 2016 05:06
-
-
Save rajanim/52b5ac63e27171c94052 to your computer and use it in GitHub Desktop.
solrj_cloudsolrclient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| solr_j_index_sample | |
| import org.apache.solr.client.solrj.SolrServerException; | |
| import org.apache.solr.client.solrj.impl.CloudSolrClient; | |
| import org.apache.solr.client.solrj.request.UpdateRequest; | |
| import org.apache.solr.client.solrj.response.UpdateResponse; | |
| import org.apache.solr.common.SolrInputDocument; | |
| import java.io.IOException; | |
| import java.text.DecimalFormat; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.UUID; | |
| public class Main { | |
| private static DecimalFormat numFormatter = new DecimalFormat("###,###"); | |
| public static void main(String[] args) { | |
| Main main = new Main(); | |
| try { // The "localhost:2181" parameter could easily be a Zookeeper ensemble. | |
| main.addDocs("localhost:2181", "mycollection"); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| List<SolrInputDocument> docs = new ArrayList<>(1_000); | |
| static final int NUM_DOCS = 10_000; | |
| private void addDocs(String ensemble, String collection) throws IOException, SolrServerException { | |
| try (CloudSolrClient client = new CloudSolrClient(ensemble)) { | |
| client.setDefaultCollection(collection); | |
| int counter = 0; | |
| for (int idx = 0; idx < NUM_DOCS; ++idx) { | |
| SolrInputDocument doc = assembleDoc(); | |
| docs.add(doc); | |
| if (docs.size() >= 1000) { // 1,000 is arbitrary, but updates should be batched for speed | |
| doUpdate(client); | |
| counter += docs.size(); | |
| if ((counter % 1_000) == 0) log(String.format("Indexed doc # %d", counter)); | |
| docs.clear(); | |
| } | |
| } | |
| doUpdate(client); | |
| docs.clear(); | |
| client.commit(); | |
| } | |
| } | |
| // Simplest possible document, just an ID. | |
| private SolrInputDocument assembleDoc() { | |
| SolrInputDocument doc = new SolrInputDocument(); | |
| doc.addField("id", UUID.randomUUID().toString()); | |
| return doc; | |
| } | |
| private void doUpdate(CloudSolrClient client) throws IOException, SolrServerException { | |
| if (docs == null || docs.size() == 0) return; | |
| UpdateRequest upReq = new UpdateRequest(); | |
| upReq.add(docs); | |
| UpdateResponse resp = upReq.process(client); | |
| if (resp.getStatus() != 0) { | |
| log("Update failed"); | |
| } | |
| } | |
| static void log(String msg) { | |
| System.out.println(msg); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment