org.apache.solr.client.solrj.impl.BinaryRequestWriter Java Examples

The following examples show how to use org.apache.solr.client.solrj.impl.BinaryRequestWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Utils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static InputStreamConsumer<ByteBuffer> newBytesConsumer(int maxSize) {
  return is -> {
    try (BinaryRequestWriter.BAOS bos = new BinaryRequestWriter.BAOS()) {
      long sz = 0;
      int next = is.read();
      while (next > -1) {
        if (++sz > maxSize) throw new BufferOverflowException();
        bos.write(next);
        next = is.read();
      }
      bos.flush();
      return ByteBuffer.wrap(bos.getbuf(), 0, bos.size());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  };

}
 
Example #2
Source File: SolrExampleBinaryTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SolrClient createNewSolrClient()
{
  try {
    // setup the server...
    String url = jetty.getBaseUrl().toString() + "/collection1";
    HttpSolrClient client = getHttpSolrClient(url, DEFAULT_CONNECTION_TIMEOUT);
    client.setUseMultiPartPost(random().nextBoolean());

    // where the magic happens
    client.setParser(new BinaryResponseParser());
    client.setRequestWriter(new BinaryRequestWriter());

    return client;
  }
  catch( Exception ex ) {
    throw new RuntimeException( ex );
  }
}
 
Example #3
Source File: TestV1toV2ApiMapper.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
// commented out on: 24-Dec-2018   @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
public void testCreate() throws IOException {
  Create cmd = CollectionAdminRequest
      .createCollection("mycoll", "conf1", 3, 2)
      .setProperties(ImmutableMap.<String,String>builder()
          .put("p1","v1")
          .put("p2","v2")
          .build());
  V2Request v2r = V1toV2ApiMapper.convert(cmd).build();
  Map<?,?> m = (Map<?,?>) Utils.fromJSON(ContentStreamBase.create(new BinaryRequestWriter(), v2r).getStream());
  assertEquals("/c", v2r.getPath());
  assertEquals("v1", Utils.getObjectByPath(m,true,"/create/properties/p1"));
  assertEquals("v2", Utils.getObjectByPath(m,true,"/create/properties/p2"));
  assertEquals("3", Utils.getObjectByPath(m,true,"/create/numShards"));
  assertEquals("2", Utils.getObjectByPath(m,true,"/create/nrtReplicas"));
}
 
Example #4
Source File: SolrSchemalessExampleTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SolrClient createNewSolrClient() {
  try {
    // setup the server...
    String url = jetty.getBaseUrl().toString() + "/collection1";
    HttpSolrClient client = getHttpSolrClient(url, DEFAULT_CONNECTION_TIMEOUT);
    client.setUseMultiPartPost(random().nextBoolean());
    
    if (random().nextBoolean()) {
      client.setParser(new BinaryResponseParser());
      client.setRequestWriter(new BinaryRequestWriter());
    }
    
    return client;
  } catch (Exception ex) {
    throw new RuntimeException(ex);
  }
}
 
Example #5
Source File: SolrExampleBinaryHttp2Test.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SolrClient createNewSolrClient()
{
  try {
    // setup the server...
    String url = jetty.getBaseUrl().toString() + "/collection1";
    Http2SolrClient client = new Http2SolrClient.Builder(url)
        .connectionTimeout(DEFAULT_CONNECTION_TIMEOUT)
        .build();

    // where the magic happens
    client.setParser(new BinaryResponseParser());
    client.setRequestWriter(new BinaryRequestWriter());

    return client;
  }
  catch( Exception ex ) {
    throw new RuntimeException( ex );
  }
}
 
Example #6
Source File: SolrExampleStreamingBinaryHttp2Test.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SolrClient createNewSolrClient() {
  // setup the server...
  String url = jetty.getBaseUrl().toString() + "/collection1";
  // smaller queue size hits locks more often
  Http2SolrClient solrClient = new Http2SolrClient.Builder()
      .build();
  solrClient.setParser(new BinaryResponseParser());
  solrClient.setRequestWriter(new BinaryRequestWriter());
  ConcurrentUpdateHttp2SolrClient concurrentClient = new ErrorTrackingConcurrentUpdateSolrClient.Builder(url, solrClient)
      .withQueueSize(2)
      .withThreadCount(5)
      .build();
  return concurrentClient;
}
 
Example #7
Source File: ChronixSolrCloudStorage.java    From chronix.spark with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches a stream of time series only from a single node with HttpSolrClient.
 *
 * @param shardUrl
 * @param query
 * @param converter
 * @return
 */
private Stream<MetricTimeSeries> streamWithHttpSolrClient(String shardUrl,
                                                          SolrQuery query,
                                                          TimeSeriesConverter<MetricTimeSeries> converter) {
    HttpSolrClient solrClient = getSingleNodeSolrClient(shardUrl);
    solrClient.setRequestWriter(new BinaryRequestWriter());
    query.set("distrib", false);
    LoggerFactory.getLogger(ChronixSolrCloudStorage.class).debug("Streaming data from solr using converter {}, Solr Client {}, and Solr Query {}", converter, solrClient, query);
    SolrStreamingService<MetricTimeSeries> solrStreamingService = new SolrStreamingService<>(converter, query, solrClient, nrOfDocumentPerBatch);
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(solrStreamingService, Spliterator.SIZED), false);
}
 
Example #8
Source File: TestFastJavabinDecoder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testTagRead() throws Exception {
  BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
  FastOutputStream faos = FastOutputStream.wrap(baos);

  try (JavaBinCodec codec = new JavaBinCodec(faos, null)) {
    codec.writeVal(10);
    codec.writeVal(100);
    codec.writeVal("Hello!");
  }

  faos.flushBuffer();
  faos.close();


  FastInputStream fis = new FastInputStream(null, baos.getbuf(), 0, baos.size());
  try (FastJavaBinDecoder.StreamCodec scodec = new FastJavaBinDecoder.StreamCodec(fis)) {
    scodec.start();
    Tag tag = scodec.getTag();
    assertEquals(Tag._SINT, tag);
    assertEquals(10, scodec.readSmallInt(scodec.dis));
    tag = scodec.getTag();
    assertEquals(Tag._SINT, tag);
    assertEquals(100, scodec.readSmallInt(scodec.dis));
    tag = scodec.getTag();
    assertEquals(Tag._STR, tag);
    assertEquals("Hello!", scodec.readStr(fis));
  }
}
 
Example #9
Source File: TestSolrJErrorHandling.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithBinary() throws Exception {
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.setRequestWriter(new BinaryRequestWriter());
  client.deleteByQuery("*:*"); // delete everything!
  doIt(client);
}
 
Example #10
Source File: TestBatchUpdate.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithBinaryBean()throws Exception{
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.setRequestWriter(new BinaryRequestWriter());
  client.deleteByQuery("*:*"); // delete everything!
  final int[] counter = new int[1];
  counter[0] = 0;
  client.addBeans(new Iterator<Bean>() {

    @Override
    public boolean hasNext() {
      return counter[0] < numdocs;
    }

    @Override
    public Bean next() {
      Bean bean = new Bean();
      bean.id = "" + (++counter[0]);
      bean.cat = "foocat";
      return bean;
    }

    @Override
    public void remove() {
      //do nothing
    }
  });
  client.commit();
  SolrQuery query = new SolrQuery("*:*");
  QueryResponse response = client.query(query);
  assertEquals(0, response.getStatus());
  assertEquals(numdocs, response.getResults().getNumFound());
}
 
Example #11
Source File: TestBatchUpdate.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithBinary()throws Exception{
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.setRequestWriter(new BinaryRequestWriter());
  client.deleteByQuery("*:*"); // delete everything!
  doIt(client);
}
 
Example #12
Source File: SolrExampleStreamingBinaryTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SolrClient createNewSolrClient() {
  ConcurrentUpdateSolrClient client = (ConcurrentUpdateSolrClient)super.createNewSolrClient();
  client.setParser(new BinaryResponseParser());
  client.setRequestWriter(new BinaryRequestWriter());
  return client;
}
 
Example #13
Source File: TestV1toV2ApiMapper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
// commented out on: 24-Dec-2018   @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
public void testSetCollectionProperty() throws IOException {
  CollectionAdminRequest.CollectionProp collectionProp = CollectionAdminRequest.setCollectionProperty("mycoll", "prop", "value");
  V2Request v2r = V1toV2ApiMapper.convert(collectionProp).build();
  Map<?,?> m = (Map<?,?>) Utils.fromJSON(ContentStreamBase.create(new BinaryRequestWriter(), v2r).getStream());
  assertEquals("/c/mycoll", v2r.getPath());
  assertEquals("prop", Utils.getObjectByPath(m,true,"/set-collection-property/name"));
  assertEquals("value", Utils.getObjectByPath(m,true,"/set-collection-property/value"));
}
 
Example #14
Source File: TestV1toV2ApiMapper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
// commented out on: 24-Dec-2018   @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
public void testAddReplica() throws IOException {
  CollectionAdminRequest.AddReplica addReplica = CollectionAdminRequest.addReplicaToShard("mycoll", "shard1");
  V2Request v2r = V1toV2ApiMapper.convert(addReplica).build();
  Map<?,?> m = (Map<?,?>) Utils.fromJSON(ContentStreamBase.create(new BinaryRequestWriter(), v2r).getStream());
  assertEquals("/c/mycoll/shards", v2r.getPath());
  assertEquals("shard1", Utils.getObjectByPath(m,true,"/add-replica/shard"));
  assertEquals("NRT", Utils.getObjectByPath(m,true,"/add-replica/type"));
}
 
Example #15
Source File: Utils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static InputStream toJavabin(Object o) throws IOException {
  try (final JavaBinCodec jbc = new JavaBinCodec()) {
    BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
    jbc.marshal(o, baos);
    return new ByteBufferInputStream(ByteBuffer.wrap(baos.getbuf(), 0, baos.size()));
  }
}
 
Example #16
Source File: MCRSolrCore.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
protected void setup(String serverURL, String name) {
    if (!serverURL.endsWith("/")) {
        serverURL += "/";
    }
    this.serverURL = serverURL;
    this.name = name;
    String coreURL = getV1CoreURL();
    int connectionTimeout = MCRConfiguration2
        .getOrThrow(SOLR_CONFIG_PREFIX + "SolrClient.ConnectionTimeout", Integer::parseInt);
    int socketTimeout = MCRConfiguration2
        .getOrThrow(SOLR_CONFIG_PREFIX + "SolrClient.SocketTimeout", Integer::parseInt);

    // default server
    solrClient = new HttpSolrClient.Builder(coreURL)
        .withConnectionTimeout(connectionTimeout)
        .withSocketTimeout(socketTimeout)
        .build();
    solrClient.setRequestWriter(new BinaryRequestWriter());
    // concurrent server
    if (USE_CONCURRENT_SERVER) {
        int queueSize = MCRConfiguration2
            .getOrThrow(SOLR_CONFIG_PREFIX + "ConcurrentUpdateSolrClient.QueueSize", Integer::parseInt);
        int threadCount = MCRConfiguration2
            .getOrThrow(SOLR_CONFIG_PREFIX + "ConcurrentUpdateSolrClient.ThreadCount", Integer::parseInt);
        concurrentClient = new ConcurrentUpdateSolrClient.Builder(coreURL)
            .withQueueSize(queueSize)
            .withConnectionTimeout(connectionTimeout)
            .withSocketTimeout(socketTimeout)
            .withThreadCount(threadCount)
            .build();
        concurrentClient.setRequestWriter(new BinaryRequestWriter());
    }
    // shutdown handler
    MCRShutdownHandler.getInstance().addCloseable(new MCRShutdownHandler.Closeable() {

        @Override
        public void prepareClose() {
        }

        @Override
        public int getPriority() {
            return Integer.MIN_VALUE + 5;
        }

        @Override
        public void close() {
            shutdown();
        }
    });
}
 
Example #17
Source File: MultiContentWriterRequest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public static ByteBuffer readByteBuffer(InputStream is) throws IOException {
  BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
  org.apache.commons.io.IOUtils.copy(is, baos);
  return ByteBuffer.wrap(baos.getbuf(), 0, baos.size());
}