Search in sources :

Example 1 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project thingsboard by thingsboard.

the class ElasticsearchAuditLogSink method init.

@PostConstruct
public void init() {
    try {
        log.trace("Adding elastic rest endpoint... host [{}], port [{}], scheme name [{}]", host, port, schemeName);
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, schemeName));
        if (StringUtils.isNotEmpty(userName) && StringUtils.isNotEmpty(password)) {
            log.trace("...using username [{}] and password ***", userName);
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
            builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        }
        this.restClient = builder.build();
    } catch (Exception e) {
        log.error("Sink init failed!", e);
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) PostConstruct(javax.annotation.PostConstruct)

Example 2 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project logging-log4j2 by apache.

the class LogstashIT method createClient.

private static RestHighLevelClient createClient() throws IOException {
    // Instantiate the client.
    LOGGER.info("instantiating the ES client");
    final HttpHost httpHost = new HttpHost(HOST_NAME, MavenHardcodedConstants.ES_PORT);
    final RestClientBuilder clientBuilder = RestClient.builder(httpHost);
    final RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
    // Verify the connection.
    LOGGER.info("verifying the ES connection");
    final ClusterHealthResponse healthResponse = client.cluster().health(new ClusterHealthRequest(), RequestOptions.DEFAULT);
    Assertions.assertThat(healthResponse.getStatus()).isNotEqualTo(ClusterHealthStatus.RED);
    // Delete the index.
    LOGGER.info("deleting the ES index");
    final DeleteIndexRequest deleteRequest = new DeleteIndexRequest(MavenHardcodedConstants.ES_INDEX_NAME);
    try {
        final AcknowledgedResponse deleteResponse = client.indices().delete(deleteRequest, RequestOptions.DEFAULT);
        Assertions.assertThat(deleteResponse.isAcknowledged()).isTrue();
    } catch (ElasticsearchStatusException error) {
        Assertions.assertThat(error).satisfies(ignored -> Assertions.assertThat(error.status()).isEqualTo(RestStatus.NOT_FOUND));
    }
    return client;
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Arrays(java.util.Arrays) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Level(org.apache.logging.log4j.Level) LogEvent(org.apache.logging.log4j.core.LogEvent) Duration(java.time.Duration) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) RequestOptions(org.elasticsearch.client.RequestOptions) Assertions(org.assertj.core.api.Assertions) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SearchHit(org.elasticsearch.search.SearchHit) ExecutionMode(org.junit.jupiter.api.parallel.ExecutionMode) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ThreadLocalRecyclerFactory(org.apache.logging.log4j.layout.template.json.util.ThreadLocalRecyclerFactory) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) GelfLayout(org.apache.logging.log4j.core.layout.GelfLayout) RestStatus(org.elasticsearch.rest.RestStatus) Layout(org.apache.logging.log4j.core.Layout) Awaitility(org.awaitility.Awaitility) RestClient(org.elasticsearch.client.RestClient) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Appender(org.apache.logging.log4j.core.Appender) SearchRequest(org.elasticsearch.action.search.SearchRequest) Function(java.util.function.Function) EventTemplateAdditionalField(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout.EventTemplateAdditionalField) NetUtils(org.apache.logging.log4j.core.util.NetUtils) Charset(java.nio.charset.Charset) SocketAppender(org.apache.logging.log4j.core.appender.SocketAppender) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) PrintStream(java.io.PrintStream) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) EcsLayout(co.elastic.logging.log4j2.EcsLayout) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) IOException(java.io.IOException) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) StatusLogger(org.apache.logging.log4j.status.StatusLogger) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) Execution(org.junit.jupiter.api.parallel.Execution) HttpHost(org.apache.http.HttpHost) Collections(java.util.Collections) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) HttpHost(org.apache.http.HttpHost) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException)

Example 3 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project flink by apache.

the class Elasticsearch7ApiCallBridge method createClient.

@Override
public RestHighLevelClient createClient(Map<String, String> clientConfig) {
    RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()]));
    restClientFactory.configureRestClientBuilder(builder);
    RestHighLevelClient rhlClient = new RestHighLevelClient(builder);
    return rhlClient;
}
Also used : HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

Example 4 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project flink by apache.

the class Elasticsearch6ApiCallBridge method createClient.

@Override
public RestHighLevelClient createClient(Map<String, String> clientConfig) {
    RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()]));
    restClientFactory.configureRestClientBuilder(builder);
    RestHighLevelClient rhlClient = new RestHighLevelClient(builder);
    return rhlClient;
}
Also used : HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

Example 5 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project graylog2-server by Graylog2.

the class RestHighLevelClientProvider method buildClient.

private RestHighLevelClient buildClient(List<URI> hosts, Duration connectTimeout, Duration socketTimeout, int maxTotalConnections, int maxTotalConnectionsPerRoute, boolean useExpectContinue, boolean muteElasticsearchDeprecationWarnings, CredentialsProvider credentialsProvider) {
    final HttpHost[] esHosts = hosts.stream().map(uri -> new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme())).toArray(HttpHost[]::new);
    final RestClientBuilder restClientBuilder = RestClient.builder(esHosts).setRequestConfigCallback(requestConfig -> requestConfig.setConnectTimeout(Math.toIntExact(connectTimeout.toMilliseconds())).setSocketTimeout(Math.toIntExact(socketTimeout.toMilliseconds())).setExpectContinueEnabled(useExpectContinue).setAuthenticationEnabled(true)).setHttpClientConfigCallback(httpClientConfig -> {
        httpClientConfig.setMaxConnTotal(maxTotalConnections).setMaxConnPerRoute(maxTotalConnectionsPerRoute).setDefaultCredentialsProvider(credentialsProvider);
        if (muteElasticsearchDeprecationWarnings) {
            httpClientConfig.addInterceptorFirst(new ElasticsearchFilterDeprecationWarningsInterceptor());
        }
        return httpClientConfig;
    });
    return new RestHighLevelClient(restClientBuilder);
}
Also used : GracefulShutdownService(org.graylog2.system.shutdown.GracefulShutdownService) Provider(javax.inject.Provider) ElasticsearchNodesSniffer(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.sniff.ElasticsearchNodesSniffer) Singleton(javax.inject.Singleton) Supplier(java.util.function.Supplier) RestClientBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClientBuilder) Inject(javax.inject.Inject) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CredentialsProvider(org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider) Locale(java.util.Locale) RestClient(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient) Sniffer(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.sniff.Sniffer) Suppliers(com.google.common.base.Suppliers) Duration(com.github.joschi.jadconfig.util.Duration) Named(javax.inject.Named) URI(java.net.URI) RestHighLevelClient(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestHighLevelClient) HttpHost(org.graylog.shaded.elasticsearch7.org.apache.http.HttpHost) NodesSniffer(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.sniff.NodesSniffer) Nullable(javax.annotation.Nullable) HttpHost(org.graylog.shaded.elasticsearch7.org.apache.http.HttpHost) RestClientBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestHighLevelClient)

Aggregations

RestClientBuilder (org.elasticsearch.client.RestClientBuilder)32 HttpHost (org.apache.http.HttpHost)26 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)21 RestClient (org.elasticsearch.client.RestClient)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)7 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 File (java.io.File)6 Locale (java.util.Locale)6 List (java.util.List)5 Map (java.util.Map)5 StringUtils (org.apache.commons.lang.StringUtils)5 AuthSchemeProvider (org.apache.http.auth.AuthSchemeProvider)5 AuthSchemes (org.apache.http.client.config.AuthSchemes)5 Lookup (org.apache.http.config.Lookup)5 RegistryBuilder (org.apache.http.config.RegistryBuilder)5 SPNegoSchemeFactory (org.apache.http.impl.auth.SPNegoSchemeFactory)5 RequestOptions (org.elasticsearch.client.RequestOptions)5