Search in sources :

Example 36 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project hazelcast by hazelcast.

the class LocalElasticSourcesTest method when_readFromElasticSource_then_shouldCloseAllCreatedClients.

@Test
public void when_readFromElasticSource_then_shouldCloseAllCreatedClients() throws IOException {
    indexDocument("my-index", of("name", "Frantisek"));
    Pipeline p = Pipeline.create();
    BatchSource<String> source = new ElasticSourceBuilder<>().clientFn(() -> {
        RestClientBuilder builder = spy(ElasticSupport.elasticClientSupplier().get());
        when(builder.build()).thenAnswer(invocation -> {
            Object result = invocation.callRealMethod();
            RestClient elasticClient = (RestClient) spy(result);
            ClientHolder.elasticClients.add(elasticClient);
            return elasticClient;
        });
        return builder;
    }).searchRequestFn(() -> new SearchRequest("my-index")).mapToItemFn(hit -> (String) hit.getSourceAsMap().get("name")).build();
    p.readFrom(source).writeTo(Sinks.logger());
    submitJob(p);
    for (RestClient elasticClient : ClientHolder.elasticClients) {
        verify(elasticClient).close();
    }
}
Also used : RestClient(org.elasticsearch.client.RestClient) HazelcastInstance(com.hazelcast.core.HazelcastInstance) BatchSource(com.hazelcast.jet.pipeline.BatchSource) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Collections.synchronizedList(java.util.Collections.synchronizedList) Sinks(com.hazelcast.jet.pipeline.Sinks) Test(org.junit.Test) IOException(java.io.IOException) SearchRequest(org.elasticsearch.action.search.SearchRequest) Mockito.when(org.mockito.Mockito.when) Mockito.spy(org.mockito.Mockito.spy) ImmutableMap.of(com.google.common.collect.ImmutableMap.of) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) Mockito.verify(org.mockito.Mockito.verify) JetException(com.hazelcast.jet.JetException) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) After(org.junit.After) TestHazelcastFactory(com.hazelcast.client.test.TestHazelcastFactory) SearchRequest(org.elasticsearch.action.search.SearchRequest) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Test(org.junit.Test)

Example 37 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project metron by apache.

the class ElasticsearchClientFactory method create.

/**
 * Creates an Elasticsearch client from settings provided via the global config.
 *
 * @return new client
 */
public static ElasticsearchClient create(Map<String, Object> globalConfig) {
    ElasticsearchClientConfig esClientConfig = new ElasticsearchClientConfig(getEsSettings(globalConfig));
    HttpHost[] httpHosts = getHttpHosts(globalConfig, esClientConfig.getConnectionScheme());
    RestClientBuilder builder = RestClient.builder(httpHosts);
    builder.setRequestConfigCallback(reqConfigBuilder -> {
        // Modifies request config builder with connection and socket timeouts.
        // https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.6/_timeouts.html
        reqConfigBuilder.setConnectTimeout(esClientConfig.getConnectTimeoutMillis());
        reqConfigBuilder.setSocketTimeout(esClientConfig.getSocketTimeoutMillis());
        return reqConfigBuilder;
    });
    builder.setMaxRetryTimeoutMillis(esClientConfig.getMaxRetryTimeoutMillis());
    builder.setHttpClientConfigCallback(clientBuilder -> {
        clientBuilder.setDefaultIOReactorConfig(getIOReactorConfig(esClientConfig));
        clientBuilder.setDefaultCredentialsProvider(getCredentialsProvider(esClientConfig));
        clientBuilder.setSSLContext(getSSLContext(esClientConfig));
        return clientBuilder;
    });
    RestClient lowLevelClient = builder.build();
    RestHighLevelClient client = new RestHighLevelClient(lowLevelClient);
    return new ElasticsearchClient(lowLevelClient, client);
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ElasticsearchClientConfig(org.apache.metron.elasticsearch.config.ElasticsearchClientConfig)

Example 38 with RestClient

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

the class ElasticClient method get.

@Test
public void get() throws IOException {
    RestClientBuilder builder = RestClient.builder(new HttpHost("sl010a-analysisdb1", 9200, "https"), new HttpHost("sl010a-analysisdb2", 9200, "https"), new HttpHost("sl010a-analysisdb3", 9200, "https"));
    Header[] defaultHeaders = new Header[] { new BasicHeader("Authorization", "Basic YWRtaW46YWRtaW4=") };
    builder.setDefaultHeaders(defaultHeaders);
    RestClient restClient = builder.build();
    RestHighLevelClient client = new RestHighLevelClient(restClient);
    GetRequest getRequest = new GetRequest("test-index", "test-all", "26269");
    GetResponse getResponse = client.get(getRequest);
    System.out.println(getResponse.getSourceAsString());
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpHost(org.apache.http.HttpHost) GetRequest(org.elasticsearch.action.get.GetRequest) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) GetResponse(org.elasticsearch.action.get.GetResponse) BasicHeader(org.apache.http.message.BasicHeader) Test(org.junit.Test)

Example 39 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project pancm_project by xuwujing.

the class EsHighLevelCluster method setCool.

/**
 * @return void
 * @Author pancm
 * @Description 设置该节点为冷节点
 * @Date 2020/1/2
 * @Param [index]
 */
public static void setCool(String index) throws IOException {
    RestClient restClient = null;
    try {
        Objects.requireNonNull(index, "index is not null");
        restClient = client.getLowLevelClient();
        String source = "{\"index.routing.allocation.require.box_type\": \"%s\"}";
        source = String.format(source, "cool");
        HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
        restClient.performRequest("PUT", "/" + index + "/_settings", Collections.<String, String>emptyMap(), entity);
    } catch (IOException e) {
        throw e;
    } finally {
        if (restClient != null) {
            restClient.close();
        }
    }
}
Also used : NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpEntity(org.apache.http.HttpEntity) RestClient(org.elasticsearch.client.RestClient) IOException(java.io.IOException)

Example 40 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project pancm_project by xuwujing.

the class EsHighLevelCluster method setAlias.

public void setAlias(String index, String aliasIndex) throws IOException {
    RestClient restClient = null;
    try {
        Objects.requireNonNull(index, "index is not null");
        restClient = client.getLowLevelClient();
        String msg = "/" + index + "/_alias" + "/" + aliasIndex;
        restClient.performRequest("PUT", msg);
    } finally {
        if (restClient != null) {
            restClient.close();
        }
    }
}
Also used : RestClient(org.elasticsearch.client.RestClient)

Aggregations

RestClient (org.elasticsearch.client.RestClient)41 HttpHost (org.apache.http.HttpHost)21 IOException (java.io.IOException)15 Response (org.elasticsearch.client.Response)9 HttpEntity (org.apache.http.HttpEntity)6 BasicHeader (org.apache.http.message.BasicHeader)6 ResponseException (org.elasticsearch.client.ResponseException)6 Test (org.junit.jupiter.api.Test)6 RestClientBuilder (org.elasticsearch.client.RestClientBuilder)5 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 AuthScope (org.apache.http.auth.AuthScope)4 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 List (java.util.List)3 StringEntity (org.apache.http.entity.StringEntity)3 IndexRequest (org.elasticsearch.action.index.IndexRequest)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 TestHazelcastFactory (com.hazelcast.client.test.TestHazelcastFactory)2