Search in sources :

Example 1 with RestClientBuilder

use of org.opensearch.client.RestClientBuilder in project opensearch-java by opensearch-project.

the class OpenSearchRestHighLevelClientTestCase method buildClient.

@Override
protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOException {
    RestClientBuilder builder = RestClient.builder(hosts);
    builder.setStrictDeprecationMode(true);
    if (isHttps()) {
        configureHttpsClient(builder);
    }
    return builder.build();
}
Also used : RestClientBuilder(org.opensearch.client.RestClientBuilder)

Example 2 with RestClientBuilder

use of org.opensearch.client.RestClientBuilder in project ml-commons by opensearch-project.

the class MLCommonsRestTestCase method buildClient.

@Override
protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOException {
    boolean strictDeprecationMode = settings.getAsBoolean("strictDeprecationMode", true);
    RestClientBuilder builder = RestClient.builder(hosts);
    if (isHttps()) {
        String keystore = settings.get(OPENSEARCH_SECURITY_SSL_HTTP_KEYSTORE_FILEPATH);
        if (Objects.nonNull(keystore)) {
            URI uri = null;
            try {
                uri = this.getClass().getClassLoader().getResource("security/sample.pem").toURI();
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }
            Path configPath = PathUtils.get(uri).getParent().toAbsolutePath();
            return new SecureRestClientBuilder(settings, configPath).build();
        } else {
            configureHttpsClient(builder, settings);
            builder.setStrictDeprecationMode(strictDeprecationMode);
            return builder.build();
        }
    } else {
        configureClient(builder, settings);
        builder.setStrictDeprecationMode(strictDeprecationMode);
        return builder.build();
    }
}
Also used : Path(java.nio.file.Path) SecureRestClientBuilder(org.opensearch.commons.rest.SecureRestClientBuilder) RestClientBuilder(org.opensearch.client.RestClientBuilder) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) SecureRestClientBuilder(org.opensearch.commons.rest.SecureRestClientBuilder)

Example 3 with RestClientBuilder

use of org.opensearch.client.RestClientBuilder in project opensearch-java by opensearch-project.

the class MigrateHlrcTest method migrate.

@Test
public void migrate() {
    // tag::migrate
    // Create the low-level client
    RestClientBuilder httpClientBuilder = RestClient.builder(new HttpHost("localhost", 9200));
    // Create the HLRC
    RestHighLevelClient hlrc = new RestHighLevelClient(httpClientBuilder);
    // Create the new Java Client with the same low level client
    OpenSearchTransport transport = new RestClientTransport(hlrc.getLowLevelClient(), new JacksonJsonpMapper());
    OpenSearchClient esClient = new OpenSearchClient(transport);
// hlrc and esClient share the same httpClient
// end::migrate
}
Also used : RestClientTransport(org.opensearch.client.transport.rest_client.RestClientTransport) HttpHost(org.apache.http.HttpHost) JacksonJsonpMapper(org.opensearch.client.json.jackson.JacksonJsonpMapper) RestClientBuilder(org.opensearch.client.RestClientBuilder) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) OpenSearchTransport(org.opensearch.client.transport.OpenSearchTransport) Test(org.junit.Test)

Example 4 with RestClientBuilder

use of org.opensearch.client.RestClientBuilder in project OpenSearch by opensearch-project.

the class RestClientDocumentation method usage.

// end::rest-client-options-singleton
@SuppressWarnings("unused")
public void usage() throws IOException, InterruptedException {
    // tag::rest-client-init
    RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http"), new HttpHost("localhost", 9201, "http")).build();
    // end::rest-client-init
    // tag::rest-client-close
    restClient.close();
    // end::rest-client-close
    {
        // tag::rest-client-init-default-headers
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        Header[] defaultHeaders = new Header[] { new BasicHeader("header", "value") };
        // <1>
        builder.setDefaultHeaders(defaultHeaders);
    // end::rest-client-init-default-headers
    }
    {
        // tag::rest-client-init-node-selector
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        // <1>
        builder.setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS);
    // end::rest-client-init-node-selector
    }
    {
        // tag::rest-client-init-allocation-aware-selector
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        builder.setNodeSelector(new // <1>
        NodeSelector() {

            @Override
            public void select(Iterable<Node> nodes) {
                /*
                     * Prefer any node that belongs to rack_one. If none is around
                     * we will go to another rack till it's time to try and revive
                     * some of the nodes that belong to rack_one.
                     */
                boolean foundOne = false;
                for (Node node : nodes) {
                    String rackId = node.getAttributes().get("rack_id").get(0);
                    if ("rack_one".equals(rackId)) {
                        foundOne = true;
                        break;
                    }
                }
                if (foundOne) {
                    Iterator<Node> nodesIt = nodes.iterator();
                    while (nodesIt.hasNext()) {
                        Node node = nodesIt.next();
                        String rackId = node.getAttributes().get("rack_id").get(0);
                        if ("rack_one".equals(rackId) == false) {
                            nodesIt.remove();
                        }
                    }
                }
            }
        });
    // end::rest-client-init-allocation-aware-selector
    }
    {
        // tag::rest-client-init-failure-listener
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        builder.setFailureListener(new RestClient.FailureListener() {

            @Override
            public void onFailure(Node node) {
            // <1>
            }
        });
    // end::rest-client-init-failure-listener
    }
    {
        // tag::rest-client-init-request-config-callback
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {

            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                // <1>
                return requestConfigBuilder.setSocketTimeout(10000);
            }
        });
    // end::rest-client-init-request-config-callback
    }
    {
        // tag::rest-client-init-client-config-callback
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {

            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                return httpClientBuilder.setProxy(// <1>
                new HttpHost("proxy", 9000, "http"));
            }
        });
    // end::rest-client-init-client-config-callback
    }
    {
        // tag::rest-client-sync
        Request request = new Request(// <1>
        "GET", // <2>
        "/");
        Response response = restClient.performRequest(request);
    // end::rest-client-sync
    }
    {
        // tag::rest-client-async
        Request request = new Request(// <1>
        "GET", // <2>
        "/");
        Cancellable cancellable = restClient.performRequestAsync(request, new ResponseListener() {

            @Override
            public void onSuccess(Response response) {
            // <3>
            }

            @Override
            public void onFailure(Exception exception) {
            // <4>
            }
        });
    // end::rest-client-async
    }
    {
        Request request = new Request("GET", "/");
        // tag::rest-client-parameters
        request.addParameter("pretty", "true");
        // end::rest-client-parameters
        // tag::rest-client-body
        request.setEntity(new NStringEntity("{\"json\":\"text\"}", ContentType.APPLICATION_JSON));
        // end::rest-client-body
        // tag::rest-client-body-shorter
        request.setJsonEntity("{\"json\":\"text\"}");
        // end::rest-client-body-shorter
        // tag::rest-client-options-set-singleton
        request.setOptions(COMMON_OPTIONS);
        // end::rest-client-options-set-singleton
        {
            // tag::rest-client-options-customize-header
            RequestOptions.Builder options = COMMON_OPTIONS.toBuilder();
            options.addHeader("cats", "knock things off of other things");
            request.setOptions(options);
        // end::rest-client-options-customize-header
        }
    }
    {
        HttpEntity[] documents = new HttpEntity[10];
        // tag::rest-client-async-example
        final CountDownLatch latch = new CountDownLatch(documents.length);
        for (int i = 0; i < documents.length; i++) {
            Request request = new Request("PUT", "/posts/doc/" + i);
            // let's assume that the documents are stored in an HttpEntity array
            request.setEntity(documents[i]);
            restClient.performRequestAsync(request, new ResponseListener() {

                @Override
                public void onSuccess(Response response) {
                    // <1>
                    latch.countDown();
                }

                @Override
                public void onFailure(Exception exception) {
                    // <2>
                    latch.countDown();
                }
            });
        }
        latch.await();
    // end::rest-client-async-example
    }
    {
        // tag::rest-client-async-cancel
        Request request = new Request("GET", "/posts/_search");
        Cancellable cancellable = restClient.performRequestAsync(request, new ResponseListener() {

            @Override
            public void onSuccess(Response response) {
            // <1>
            }

            @Override
            public void onFailure(Exception exception) {
            // <2>
            }
        });
        cancellable.cancel();
    // end::rest-client-async-cancel
    }
    {
        // tag::rest-client-response2
        Response response = restClient.performRequest(new Request("GET", "/"));
        // <1>
        RequestLine requestLine = response.getRequestLine();
        // <2>
        HttpHost host = response.getHost();
        // <3>
        int statusCode = response.getStatusLine().getStatusCode();
        // <4>
        Header[] headers = response.getHeaders();
        // <5>
        String responseBody = EntityUtils.toString(response.getEntity());
    // end::rest-client-response2
    }
}
Also used : RequestOptions(org.opensearch.client.RequestOptions) Cancellable(org.opensearch.client.Cancellable) Node(org.opensearch.client.Node) RestClientBuilder(org.opensearch.client.RestClientBuilder) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) RestClientBuilder(org.opensearch.client.RestClientBuilder) NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpHost(org.apache.http.HttpHost) RequestConfig(org.apache.http.client.config.RequestConfig) RestClient(org.opensearch.client.RestClient) Request(org.opensearch.client.Request) ResponseListener(org.opensearch.client.ResponseListener) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) Response(org.opensearch.client.Response) RequestLine(org.apache.http.RequestLine) HttpClientConfigCallback(org.opensearch.client.RestClientBuilder.HttpClientConfigCallback) NodeSelector(org.opensearch.client.NodeSelector) BasicHeader(org.apache.http.message.BasicHeader)

Example 5 with RestClientBuilder

use of org.opensearch.client.RestClientBuilder in project OpenSearch by opensearch-project.

the class Reindexer method buildRestClient.

/**
 * Build the {@link RestClient} used for reindexing from remote clusters.
 *
 * @param remoteInfo connection information for the remote cluster
 * @param sslConfig configuration for potential outgoing HTTPS connections
 * @param taskId the id of the current task. This is added to the thread name for easier tracking
 * @param threadCollector a list in which we collect all the threads created by the client
 * @param restInterceptor an optional HttpRequestInterceptor
 */
static RestClient buildRestClient(RemoteInfo remoteInfo, ReindexSslConfig sslConfig, long taskId, List<Thread> threadCollector, Optional<HttpRequestInterceptor> restInterceptor) {
    Header[] clientHeaders = new Header[remoteInfo.getHeaders().size()];
    int i = 0;
    for (Map.Entry<String, String> header : remoteInfo.getHeaders().entrySet()) {
        clientHeaders[i++] = new BasicHeader(header.getKey(), header.getValue());
    }
    final RestClientBuilder builder = RestClient.builder(new HttpHost(remoteInfo.getHost(), remoteInfo.getPort(), remoteInfo.getScheme())).setDefaultHeaders(clientHeaders).setRequestConfigCallback(c -> {
        c.setConnectTimeout(Math.toIntExact(remoteInfo.getConnectTimeout().millis()));
        c.setSocketTimeout(Math.toIntExact(remoteInfo.getSocketTimeout().millis()));
        return c;
    }).setHttpClientConfigCallback(c -> {
        // Enable basic auth if it is configured
        if (remoteInfo.getUsername() != null) {
            UsernamePasswordCredentials creds = new UsernamePasswordCredentials(remoteInfo.getUsername(), remoteInfo.getPassword());
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, creds);
            c.setDefaultCredentialsProvider(credentialsProvider);
        } else {
            restInterceptor.ifPresent(interceptor -> c.addInterceptorLast(interceptor));
        }
        // Stick the task id in the thread name so we can track down tasks from stack traces
        AtomicInteger threads = new AtomicInteger();
        c.setThreadFactory(r -> {
            String name = "es-client-" + taskId + "-" + threads.getAndIncrement();
            Thread t = new Thread(r, name);
            threadCollector.add(t);
            return t;
        });
        // Limit ourselves to one reactor thread because for now the search process is single threaded.
        c.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build());
        c.setSSLStrategy(sslConfig.getStrategy());
        return c;
    });
    if (Strings.hasLength(remoteInfo.getPathPrefix()) && "/".equals(remoteInfo.getPathPrefix()) == false) {
        builder.setPathPrefix(remoteInfo.getPathPrefix());
    }
    return builder.build();
}
Also used : IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) BiFunction(java.util.function.BiFunction) RemoteScrollableHitSource(org.opensearch.index.reindex.remote.RemoteScrollableHitSource) Header(org.apache.http.Header) Strings(org.opensearch.common.Strings) XContentParser(org.opensearch.common.xcontent.XContentParser) RemoteReindexExtension(org.opensearch.index.reindex.spi.RemoteReindexExtension) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BackoffPolicy(org.opensearch.action.bulk.BackoffPolicy) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) Map(java.util.Map) RestClient(org.opensearch.client.RestClient) RestClientBuilder(org.opensearch.client.RestClientBuilder) ActionListener(org.opensearch.action.ActionListener) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) ScriptService(org.opensearch.script.ScriptService) Client(org.opensearch.client.Client) Collections.synchronizedList(java.util.Collections.synchronizedList) Collections.emptyList(java.util.Collections.emptyList) Script(org.opensearch.script.Script) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) VersionType(org.opensearch.index.VersionType) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Optional(java.util.Optional) XContentType(org.opensearch.common.xcontent.XContentType) CredentialsProvider(org.apache.http.client.CredentialsProvider) BytesReference(org.opensearch.common.bytes.BytesReference) ThreadPool(org.opensearch.threadpool.ThreadPool) DocWriteRequest(org.opensearch.action.DocWriteRequest) ArrayList(java.util.ArrayList) Objects.requireNonNull(java.util.Objects.requireNonNull) VersionFieldMapper(org.opensearch.index.mapper.VersionFieldMapper) Versions(org.opensearch.common.lucene.uid.Versions) ParentTaskAssigningClient(org.opensearch.client.ParentTaskAssigningClient) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) INTERNAL(org.opensearch.index.VersionType.INTERNAL) AuthScope(org.apache.http.auth.AuthScope) BasicHeader(org.apache.http.message.BasicHeader) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) IndexRequest(org.opensearch.action.index.IndexRequest) HttpHost(org.apache.http.HttpHost) LogManager(org.apache.logging.log4j.LogManager) DeprecationHandler(org.opensearch.common.xcontent.DeprecationHandler) InputStream(java.io.InputStream) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) RestClientBuilder(org.opensearch.client.RestClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpHost(org.apache.http.HttpHost) Map(java.util.Map) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

RestClientBuilder (org.opensearch.client.RestClientBuilder)9 HttpHost (org.apache.http.HttpHost)4 IOException (java.io.IOException)3 UncheckedIOException (java.io.UncheckedIOException)2 ArrayList (java.util.ArrayList)2 BasicHeader (org.apache.http.message.BasicHeader)2 Node (org.opensearch.client.Node)2 RestClient (org.opensearch.client.RestClient)2 InputStream (java.io.InputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.synchronizedList (java.util.Collections.synchronizedList)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1