发送大字符串时ContentStreamUpdateRequest套接字写入错误

时间:2011-11-08 09:11:10

标签: java http solr solrj

使用SolrJ的ContentStreamUpdateRequest将数据发送到solr时出现以下异常

Exception in thread "main" org.apache.solr.client.solrj.SolrServerException: java.net.SocketException: Software caused connection abort: socket write error
    at org.apache.solr.client.solrj.impl.CommonsHttpSolrServer.request(CommonsHttpSolrServer.java:478)
    at org.apache.solr.client.solrj.impl.CommonsHttpSolrServer.request(CommonsHttpSolrServer.java:244)
    at com.atosworldline.documentarchiver.core.solr.SolrIndexer.addDocument(SolrIndexer.java:48)
    at com.atosworldline.documentarchiver.core.solr.SolrIndexerTest.main(SolrIndexerTest.java:14)
Caused by: java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105)
    at org.apache.commons.httpclient.HttpConnection.write(HttpConnection.java:975)
    at org.apache.commons.httpclient.HttpConnection.write(HttpConnection.java:943)
    at org.apache.commons.httpclient.HttpConnection.print(HttpConnection.java:1033)
    at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.print(MultiThreadedHttpConnectionManager.java:1644)
    at org.apache.commons.httpclient.HttpMethodBase.writeRequestLine(HttpMethodBase.java:2218)
    at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2059)
    at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
    at org.apache.solr.client.solrj.impl.CommonsHttpSolrServer.request(CommonsHttpSolrServer.java:422)
    ... 3 more

该异常的原因是,我正在将文档的二进制数据发送给solr。如果我删除它,应用程序运行成功。 我正在使用以下代码发送请求

SolrServer solrServer = new CommonsHttpSolrServer(url);
ContentStreamUpdateRequest request = new ContentStreamUpdateRequest(EXTRACT);
request.addFile(document);

//request.setParam("literal.id", documentName);
request.setAction(ACTION.COMMIT, true, true);
byte[] byteContent = FileUtils.readFileToByteArray(document);
String base64encodedFile = Base64.byteArrayToBase64(byteContent, 0, byteContent.length);
request.setParam(SolrDBSchema.DOCUMENT.toString(), base64encodedFile);
request.setParam("literal." + SolrDBSchema.OWNER.toString(), owner);
request.setParam("literal." + SolrDBSchema.VALID.toString(), Boolean.toString(valid));
request.setParam("literal." + SolrDBSchema.DATE.toString(), validFor.toString());
request.setParam("literal." + SolrDBSchema.DOCUMENT_TYPE.toString(), documentType);
solrServer.request(request);

我认为String base64encodedFile对HTTP请求很长,但我不知道该问题的解决方案。据我所知,Solr无法通过配置存储文档。

1 个答案:

答案 0 :(得分:0)

由于您使用的是SolrJ,请考虑将RequestWriter设置为使用BinaryRequestWriter,如下所示:

   solrServer.setRequestWriter(new BinaryRequestWriter());

这将允许您以二进制格式将数据写入Solr,并可以绕过您将大数据流作为XML发送的问题。

请注意,您需要确保在solrconfig.xml设置中启用了BinaryUpdateRequestHandler。

  <requestHandler name="/update/javabin" class="solr.BinaryUpdateRequestHandler" />