Note that there are some explanatory texts on larger screens.

plurals
  1. POSending CSV file on HTTP
    primarykey
    data
    text
    <p>I want to send this CSV data to SOLR.</p> <pre><code>id,author,name,year,category 001-001,H. G. Wells,The Time Machine,1895,Fiction 001-002,H. G. Wells,The Wonderful Visit,1895,Fiction </code></pre> <p>Here is my code which doesn't work.</p> <pre><code> private static final String postURL = "http://localhost:8983/solr/update/csv?separator=,&amp;commit=true"; public static void main(String[] args) throws IOException { BookCsvIndexer poster = new BookCsvIndexer(); BufferedReader dbr = new BufferedReader(new FileReader("E:/solr-example/csv/books.csv")); String line; StringBuilder dsb = new StringBuilder(); while ((line = dbr.readLine()) != null) { dsb.append(line); } dbr.close(); String data = dsb.toString(); System.out.println(data); poster.doPost(data); } public void doPost(String data) { String contentLength = Integer.toString(data.getBytes().length); System.out.println("Length=" + contentLength); StringBuilder sb = new StringBuilder(); try { URL url = new URL(postURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Accept", "text/plain"); connection.setRequestProperty("Connection", "close"); connection.setRequestProperty("Content-Type", "application/csv; charset=utf-8"); connection.setRequestProperty("Content-Length", "" + contentLength); connection.connect(); if (data.length() &gt; 0) { OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream()); wr.write(data); wr.flush(); wr.close(); } BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream()))); String output; System.out.println("Server Respose CODE=" + connection.getResponseCode()); while ((output = br.readLine()) != null) { sb.append(output); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(sb.toString()); } </code></pre> <p>I can use this same code for sending JSON and other text files successfully by simply changing the 'Content-Type'. I have been using this same code for hitting many REST services. I can't understand why this code is not working for sending CSV to SOLR.</p> <p>Here is the SOLR log after running the above code.</p> <pre><code>INFO - 2013-11-21 11:20:09.090; org.apache.solr.update.DirectUpdateHandler2; start commit{,optimize=false,openSearcher=true,waitSearcher=true,expungeDeletes=false,softCommit=false,prepareCommit=false} INFO - 2013-11-21 11:20:09.093; org.apache.solr.core.SolrDeletionPolicy; SolrDeletionPolicy.onInit: commits: num=1 commit{dir=NRTCachingDirectory(org.apache.lucene.store.SimpleFSDirectory@E:\solr-example\solr\collection1\data\index lockFactory=org.apache.lucene.store.NativeFSLockFactory@15b46ef; maxCacheMB=48.0 maxMergeSizeMB=4.0),segFN=segments_1,generation=1} INFO - 2013-11-21 11:20:09.093; org.apache.solr.core.SolrDeletionPolicy; newest commit generation = 1 INFO - 2013-11-21 11:20:09.094; org.apache.solr.update.DirectUpdateHandler2; No uncommitted changes. Skipping IW.commit. INFO - 2013-11-21 11:20:09.094; org.apache.solr.search.SolrIndexSearcher; Opening Searcher@ae20f5 main INFO - 2013-11-21 11:20:09.095; org.apache.solr.update.DirectUpdateHandler2; end_commit_flush INFO - 2013-11-21 11:20:09.095; org.apache.solr.core.QuerySenderListener; QuerySenderListener sending requests to Searcher@ae20f5 main{StandardDirectoryReader(segments_1:1:nrt)} INFO - 2013-11-21 11:20:09.096; org.apache.solr.core.QuerySenderListener; QuerySenderListener done. INFO - 2013-11-21 11:20:09.096; org.apache.solr.handler.component.SpellCheckComponent$SpellCheckerListener; Building spell index for spell checker: suggest INFO - 2013-11-21 11:20:09.096; org.apache.solr.spelling.suggest.Suggester; build() INFO - 2013-11-21 11:20:09.099; org.apache.solr.core.SolrCore; [collection1] Registered new searcher Searcher@ae20f5 main{StandardDirectoryReader(segments_1:1:nrt)} INFO - 2013-11-21 11:20:09.100; org.apache.solr.update.processor.LogUpdateProcessor; [collection1] webapp=/solr path=/update/csv params={commit=true&amp;separator=,} {commit=} 0 17 </code></pre> <p>And the response from the last line <code>System.out.println(sb.toString());</code> is</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;response&gt; &lt;lst name="responseHeader"&gt; &lt;int name="status"&gt;0&lt;/int&gt; &lt;int name="QTime"&gt;17&lt;/int&gt;&lt;/lst&gt; &lt;/response&gt; </code></pre> <p>The URL and CSV file is correct because I can POST this CSV data successfully using REST client software like POSTMAN, a Chrome extension for REST client. Now, I want to do the same using Java.</p> <p>Am I missing something?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload