Search in sources :

Example 11 with RestHighLevelClient

use of org.elasticsearch.client.RestHighLevelClient in project beam by apache.

the class HadoopFormatIOElasticIT method prepareElasticIndex.

private static void prepareElasticIndex() throws IOException {
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(options.getElasticServerIp(), options.getElasticServerPort(), "http")));
    for (int i = 0; i < 1000; i++) {
        IndexRequest request = new IndexRequest(ELASTIC_INDEX_NAME).source(createElasticRow(i));
        client.index(request, RequestOptions.DEFAULT);
    }
}
Also used : HttpHost(org.apache.http.HttpHost) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 12 with RestHighLevelClient

use of org.elasticsearch.client.RestHighLevelClient in project beam by apache.

the class HadoopFormatIOElasticTest method prepareElasticIndex.

private static void prepareElasticIndex() throws IOException {
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(elasticsearch.getContainerIpAddress(), elasticsearch.getMappedPort(9200), "http")));
    for (int i = 0; i < TEST_DATA_ROW_COUNT; i++) {
        IndexRequest request = new IndexRequest(ELASTIC_INDEX_NAME).source(createElasticRow(ELASTIC_TYPE_ID_PREFIX + i, "Faraday" + i));
        client.index(request, RequestOptions.DEFAULT);
    }
}
Also used : HttpHost(org.apache.http.HttpHost) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 13 with RestHighLevelClient

use of org.elasticsearch.client.RestHighLevelClient in project hazelcast by hazelcast.

the class BaseElasticTest method setUpBase.

@Before
public void setUpBase() {
    if (elasticClient == null) {
        elasticClient = new RestHighLevelClient(elasticClientSupplier().get());
    }
    cleanElasticData();
    if (hz == null) {
        hz = createHazelcastInstance();
    }
    results = hz.getList("results");
    results.clear();
}
Also used : RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Before(org.junit.Before)

Example 14 with RestHighLevelClient

use of org.elasticsearch.client.RestHighLevelClient 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)

Example 15 with RestHighLevelClient

use of org.elasticsearch.client.RestHighLevelClient in project gora by apache.

the class ElasticsearchStore method createClient.

public static RestHighLevelClient createClient(ElasticsearchParameters parameters) {
    RestClientBuilder clientBuilder = RestClient.builder(new HttpHost(parameters.getHost(), parameters.getPort()));
    // Choosing the authentication method.
    switch(parameters.getAuthenticationType()) {
        case BASIC:
            if (parameters.getUsername() != null && parameters.getPassword() != null) {
                final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(parameters.getUsername(), parameters.getPassword()));
                clientBuilder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
            } else {
                throw new IllegalArgumentException("Missing username or password for BASIC authentication.");
            }
            break;
        case TOKEN:
            if (parameters.getAuthorizationToken() != null) {
                Header[] defaultHeaders = new Header[] { new BasicHeader("Authorization", parameters.getAuthorizationToken()) };
                clientBuilder.setDefaultHeaders(defaultHeaders);
            } else {
                throw new IllegalArgumentException("Missing authorization token for TOKEN authentication.");
            }
            break;
        case APIKEY:
            if (parameters.getApiKeyId() != null && parameters.getApiKeySecret() != null) {
                String apiKeyAuth = Base64.getEncoder().encodeToString((parameters.getApiKeyId() + ":" + parameters.getApiKeySecret()).getBytes(StandardCharsets.UTF_8));
                Header[] defaultHeaders = new Header[] { new BasicHeader("Authorization", "ApiKey " + apiKeyAuth) };
                clientBuilder.setDefaultHeaders(defaultHeaders);
            } else {
                throw new IllegalArgumentException("Missing API Key ID or API Key Secret for APIKEY authentication.");
            }
            break;
    }
    if (parameters.getConnectTimeout() != 0) {
        clientBuilder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(parameters.getConnectTimeout()));
    }
    if (parameters.getSocketTimeout() != 0) {
        clientBuilder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setSocketTimeout(parameters.getSocketTimeout()));
    }
    if (parameters.getIoThreadCount() != 0) {
        clientBuilder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(parameters.getIoThreadCount()).build()));
    }
    return new RestHighLevelClient(clientBuilder);
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) BasicHeader(org.apache.http.message.BasicHeader) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)61 HttpHost (org.apache.http.HttpHost)23 RestClientBuilder (org.elasticsearch.client.RestClientBuilder)21 IOException (java.io.IOException)14 RestClient (org.elasticsearch.client.RestClient)13 HashMap (java.util.HashMap)10 IndexRequest (org.elasticsearch.action.index.IndexRequest)8 RequestOptions (org.elasticsearch.client.RequestOptions)7 Test (org.junit.jupiter.api.Test)7 CredentialsProvider (org.apache.http.client.CredentialsProvider)6 SearchRequest (org.elasticsearch.action.search.SearchRequest)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)5 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)5 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5