Search in sources :

Example 11 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.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 12 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.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 13 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.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 14 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.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)

Example 15 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestHighLevelClient in project warn-report by saaavsaaa.

the class ElasticClient method search.

@Test
public void search() throws IOException {
    String elasticsearchHost = "sl010a-analysisdb1:9200,sl010a-analysisdb2:9200,sl010a-analysisdb3:9200";
    String[] elasticHosts = elasticsearchHost.split(",");
    HttpHost[] httpHosts = new HttpHost[elasticHosts.length];
    for (int i = 0; i < elasticHosts.length; i++) {
        String ip = elasticHosts[i].split(":")[0];
        String port = elasticHosts[i].split(":")[1];
        httpHosts[i] = new HttpHost(ip, Integer.parseInt(port), "https");
    }
    RestClient restClient = RestClient.builder(httpHosts).setDefaultHeaders(new Header[] { new BasicHeader("Authorization", "Basic Q049ZGVtb3VzZXI6Q049ZGVtb3VzZXI=") }).build();
    RestHighLevelClient client = new RestHighLevelClient(restClient);
    SearchRequest searchRequest = new SearchRequest();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest);
    System.out.println(searchResponse.getTotalShards());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) BasicHeader(org.apache.http.message.BasicHeader) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Aggregations

RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)56 HttpHost (org.apache.http.HttpHost)22 RestClientBuilder (org.elasticsearch.client.RestClientBuilder)19 IOException (java.io.IOException)12 RestClient (org.elasticsearch.client.RestClient)11 HashMap (java.util.HashMap)9 IndexRequest (org.elasticsearch.action.index.IndexRequest)8 Test (org.junit.jupiter.api.Test)7 SearchRequest (org.elasticsearch.action.search.SearchRequest)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 RequestOptions (org.elasticsearch.client.RequestOptions)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 CredentialsProvider (org.apache.http.client.CredentialsProvider)5 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)5 Test (org.junit.Test)5 TableEnvironment (org.apache.flink.table.api.TableEnvironment)4