Search in sources :

Example 1 with ContentStreamUpdateRequest

use of org.apache.solr.client.solrj.request.ContentStreamUpdateRequest in project lucene-solr by apache.

the class SolrCLI method postJsonToSolr.

public static NamedList<Object> postJsonToSolr(SolrClient solrClient, String updatePath, String jsonBody) throws Exception {
    ContentStreamBase.StringStream contentStream = new ContentStreamBase.StringStream(jsonBody);
    contentStream.setContentType(JSON_CONTENT_TYPE);
    ContentStreamUpdateRequest req = new ContentStreamUpdateRequest(updatePath);
    req.addContentStream(contentStream);
    return solrClient.request(req);
}
Also used : ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 2 with ContentStreamUpdateRequest

use of org.apache.solr.client.solrj.request.ContentStreamUpdateRequest in project lucene-solr by apache.

the class TestSubQueryTransformerDistrib method addDocs.

private void addDocs(String collection, List<String> docs) throws SolrServerException, IOException {
    StringBuilder upd = new StringBuilder("<update>");
    upd.append("<delete><query>*:*</query></delete>");
    for (Iterator<String> iterator = docs.iterator(); iterator.hasNext(); ) {
        String add = iterator.next();
        upd.append(add);
        if (rarely()) {
            upd.append(commit("softCommit", "true"));
        }
        if (rarely() || !iterator.hasNext()) {
            if (!iterator.hasNext()) {
                upd.append(commit("softCommit", "false"));
            }
            upd.append("</update>");
            ContentStreamUpdateRequest req = new ContentStreamUpdateRequest("/update");
            req.addContentStream(new ContentStreamBase.StringStream(upd.toString(), "text/xml"));
            cluster.getSolrClient().request(req, collection);
            upd.setLength("<update>".length());
        }
    }
}
Also used : ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 3 with ContentStreamUpdateRequest

use of org.apache.solr.client.solrj.request.ContentStreamUpdateRequest in project lucene-solr by apache.

the class SolrExampleTests method testMultiContentStreamRequest.

@Test
public void testMultiContentStreamRequest() throws Exception {
    SolrClient client = getSolrClient();
    // delete everything!
    client.deleteByQuery("*:*");
    client.commit();
    QueryResponse rsp = client.query(new SolrQuery("*:*"));
    Assert.assertEquals(0, rsp.getResults().getNumFound());
    ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update");
    // 2
    up.addFile(getFile("solrj/docs1.xml"), "application/xml");
    // 3
    up.addFile(getFile("solrj/docs2.xml"), "application/xml");
    up.setParam("a", "ሴ");
    up.setParam(CommonParams.HEADER_ECHO_PARAMS, CommonParams.EchoParamStyle.ALL.toString());
    up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    NamedList<Object> result = client.request(up);
    Assert.assertEquals("ሴ", ((NamedList) ((NamedList) result.get("responseHeader")).get("params")).get("a"));
    assertNotNull("Couldn't upload xml files", result);
    rsp = client.query(new SolrQuery("*:*"));
    Assert.assertEquals(5, rsp.getResults().getNumFound());
}
Also used : ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) NamedList(org.apache.solr.common.util.NamedList) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) Test(org.junit.Test)

Example 4 with ContentStreamUpdateRequest

use of org.apache.solr.client.solrj.request.ContentStreamUpdateRequest in project lucene-solr by apache.

the class SolrExampleTests method testContentStreamRequest.

@Test
public void testContentStreamRequest() throws Exception {
    SolrClient client = getSolrClient();
    // delete everything!
    client.deleteByQuery("*:*");
    client.commit();
    QueryResponse rsp = client.query(new SolrQuery("*:*"));
    Assert.assertEquals(0, rsp.getResults().getNumFound());
    ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update");
    up.addFile(getFile("solrj/books.csv"), "application/csv");
    up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    NamedList<Object> result = client.request(up);
    assertNotNull("Couldn't upload books.csv", result);
    rsp = client.query(new SolrQuery("*:*"));
    Assert.assertEquals(10, rsp.getResults().getNumFound());
}
Also used : ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) Test(org.junit.Test)

Example 5 with ContentStreamUpdateRequest

use of org.apache.solr.client.solrj.request.ContentStreamUpdateRequest in project nifi by apache.

the class PutSolrContentStream method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final AtomicReference<Exception> error = new AtomicReference<>(null);
    final AtomicReference<Exception> connectionError = new AtomicReference<>(null);
    final boolean isSolrCloud = SOLR_TYPE_CLOUD.equals(context.getProperty(SOLR_TYPE).getValue());
    final String collection = context.getProperty(COLLECTION).evaluateAttributeExpressions(flowFile).getValue();
    final Long commitWithin = context.getProperty(COMMIT_WITHIN).evaluateAttributeExpressions(flowFile).asLong();
    final String contentStreamPath = context.getProperty(CONTENT_STREAM_PATH).evaluateAttributeExpressions(flowFile).getValue();
    final MultiMapSolrParams requestParams = new MultiMapSolrParams(getRequestParams(context, flowFile));
    StopWatch timer = new StopWatch(true);
    session.read(flowFile, new InputStreamCallback() {

        @Override
        public void process(final InputStream in) throws IOException {
            ContentStreamUpdateRequest request = new ContentStreamUpdateRequest(contentStreamPath);
            request.setParams(new ModifiableSolrParams());
            // add the extra params, don't use 'set' in case of repeating params
            Iterator<String> paramNames = requestParams.getParameterNamesIterator();
            while (paramNames.hasNext()) {
                String paramName = paramNames.next();
                for (String paramValue : requestParams.getParams(paramName)) {
                    request.getParams().add(paramName, paramValue);
                }
            }
            // specify the collection for SolrCloud
            if (isSolrCloud) {
                request.setParam(COLLECTION_PARAM_NAME, collection);
            }
            if (commitWithin != null && commitWithin > 0) {
                request.setParam(COMMIT_WITHIN_PARAM_NAME, commitWithin.toString());
            }
            // if a username and password were provided then pass them for basic auth
            if (isBasicAuthEnabled()) {
                request.setBasicAuthCredentials(getUsername(), getPassword());
            }
            try (final BufferedInputStream bufferedIn = new BufferedInputStream(in)) {
                // add the FlowFile's content on the UpdateRequest
                request.addContentStream(new ContentStreamBase() {

                    @Override
                    public InputStream getStream() throws IOException {
                        return bufferedIn;
                    }

                    @Override
                    public String getContentType() {
                        return context.getProperty(CONTENT_TYPE).evaluateAttributeExpressions().getValue();
                    }
                });
                UpdateResponse response = request.process(getSolrClient());
                getLogger().debug("Got {} response from Solr", new Object[] { response.getStatus() });
            } catch (SolrException e) {
                error.set(e);
            } catch (SolrServerException e) {
                if (causedByIOException(e)) {
                    connectionError.set(e);
                } else {
                    error.set(e);
                }
            } catch (IOException e) {
                connectionError.set(e);
            }
        }
    });
    timer.stop();
    if (error.get() != null) {
        getLogger().error("Failed to send {} to Solr due to {}; routing to failure", new Object[] { flowFile, error.get() });
        session.transfer(flowFile, REL_FAILURE);
    } else if (connectionError.get() != null) {
        getLogger().error("Failed to send {} to Solr due to {}; routing to connection_failure", new Object[] { flowFile, connectionError.get() });
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_CONNECTION_FAILURE);
    } else {
        StringBuilder transitUri = new StringBuilder("solr://");
        transitUri.append(getSolrLocation());
        if (isSolrCloud) {
            transitUri.append(":").append(collection);
        }
        final long duration = timer.getDuration(TimeUnit.MILLISECONDS);
        session.getProvenanceReporter().send(flowFile, transitUri.toString(), duration, true);
        getLogger().info("Successfully sent {} to Solr in {} millis", new Object[] { flowFile, duration });
        session.transfer(flowFile, REL_SUCCESS);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) SolrServerException(org.apache.solr.client.solrj.SolrServerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) IOException(java.io.IOException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) StopWatch(org.apache.nifi.util.StopWatch) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) BufferedInputStream(java.io.BufferedInputStream) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) Iterator(java.util.Iterator) SolrException(org.apache.solr.common.SolrException) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Aggregations

ContentStreamUpdateRequest (org.apache.solr.client.solrj.request.ContentStreamUpdateRequest)11 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)3 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)3 Test (org.junit.Test)3 File (java.io.File)2 ErrorTrackingConcurrentUpdateSolrClient (org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient)2 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)2 SolrException (org.apache.solr.common.SolrException)2 ContentStream (org.apache.solr.common.util.ContentStream)2 BufferedInputStream (java.io.BufferedInputStream)1 FilenameFilter (java.io.FilenameFilter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Random (java.util.Random)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)1 WrappedFile (org.apache.camel.WrappedFile)1