Search code examples
javasolrcassandradatastax-enterprisedatastax

Datastax DSE Search : How to post solrconfig.xml and schema.xml using java?


I'm writing an application using Datastax Enterprise and DSE Search (SolR on Cassandra).

My problem is i need to generate schema.xml and send to SolR both solrconfig.xml and schema.xml using Java, dynamically. Not using curl.

I tried doing it using Commons HttpComponents but the solrconfig.xml file inserted into cassandra table is not correct. Http related data is inserted before the beginning of the XML file, like this :

��:
solr_adminsolr_resourcesresource_value��--9NDJNu2AW4jtIyX6ggQAgEqI3FXp3JpDZ6
Content-Disposition: form-data; name="solrconfig.xml"; filename="solrconfig.xml"
Content-Type: application/xml; charset=ISO-8859-1
Content-Transfer-Encoding: binary

<config>
<!-- In all configuration below, a prefix of "solr." for class names
     is an alias that causes solr to search appropriate packages,
     including org.apache.solr.(search|update|request|core|analysis)

[Continued...]

And of course SolR fails when it tries to read this configuration.

Do someone know how to send these two files programmatically from java to SolR correctly?

Thanks in advance for your answers.


Solution

  • I finally found the way to do it. I looked at Solandra tests and found how they were sending files to solr.

             URL url = new URL("http://hostname:port/solr/resource/solrksname.tablename/schema.xml");
    
            // write
            try {
    
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                wr.write(builder.toString());
                wr.flush();
                wr.close();
    
                System.out.println("Schema insert response code : " + conn.getResponseCode());
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    And response code is 200, checking returns the correct schema, no error in solr admin, and no error while creating solr core. Don't know how i did it wrong the first time, the way was to use the basic HttpURLConnection and stream data with an output stream writer. No need for HttpCommons.