Search in sources :

Example 16 with RestClientBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClientBuilder in project components by Talend.

the class ElasticsearchConnection method createClient.

public static RestClient createClient(ElasticsearchDatastoreProperties datastore) throws MalformedURLException {
    String urlStr = datastore.nodes.getValue();
    String[] urls = urlStr.split(",");
    HttpHost[] hosts = new HttpHost[urls.length];
    int i = 0;
    for (String address : urls) {
        URL url = new URL("http://" + address);
        hosts[i] = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
        i++;
    }
    RestClientBuilder restClientBuilder = RestClient.builder(hosts);
    if (datastore.auth.useAuth.getValue()) {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(datastore.auth.userId.getValue(), datastore.auth.password.getValue()));
        restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
                return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            }
        });
    }
    return restClientBuilder.build();
}
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) URL(java.net.URL) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder)

Example 17 with RestClientBuilder

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

the class ElasticSinkBuilderTest method when_writeToFailingSink_then_shouldCloseClient.

@Test
public void when_writeToFailingSink_then_shouldCloseClient() throws IOException {
    ClientHolder.elasticClients.clear();
    Sink<String> elasticSink = new ElasticSinkBuilder<>().clientFn(() -> {
        RestClientBuilder builder = spy(RestClient.builder(HttpHost.create("localhost:9200")));
        when(builder.build()).thenAnswer(invocation -> {
            Object result = invocation.callRealMethod();
            RestClient client = (RestClient) spy(result);
            ClientHolder.elasticClients.add(client);
            return client;
        });
        return builder;
    }).bulkRequestFn(() -> new BulkRequest().setRefreshPolicy(RefreshPolicy.IMMEDIATE)).mapToRequestFn((String item) -> new IndexRequest("my-index").source(Collections.emptyMap())).retries(0).build();
    p.readFrom(TestSources.items("a", "b", "c")).writeTo(elasticSink);
    try {
        execute();
    } catch (Exception e) {
    // ignore - elastic is not running
    }
    for (RestClient client : ClientHolder.elasticClients) {
        verify(client).close();
    }
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest) IOException(java.io.IOException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 18 with RestClientBuilder

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

the class LocalElasticSinkTest method when_writeToSink_then_shouldCloseClient.

@Test
public void when_writeToSink_then_shouldCloseClient() throws IOException {
    ClientHolder.elasticClients.clear();
    Sink<String> elasticSink = new ElasticSinkBuilder<>().clientFn(() -> {
        RestClientBuilder builder = spy(RestClient.builder(HttpHost.create(ElasticSupport.elastic.get().getHttpHostAddress())));
        when(builder.build()).thenAnswer(invocation -> {
            Object result = invocation.callRealMethod();
            RestClient client = (RestClient) spy(result);
            ClientHolder.elasticClients.add(client);
            return client;
        });
        return builder;
    }).bulkRequestFn(() -> new BulkRequest().setRefreshPolicy(RefreshPolicy.IMMEDIATE)).mapToRequestFn((String item) -> new IndexRequest("my-index").source(Collections.emptyMap())).build();
    Pipeline p = Pipeline.create();
    p.readFrom(TestSources.items("a", "b", "c")).writeTo(elasticSink);
    hz.getJet().newJob(p).join();
    for (RestClient client : ClientHolder.elasticClients) {
        verify(client).close();
    }
}
Also used : RestClient(org.elasticsearch.client.RestClient) HazelcastInstance(com.hazelcast.core.HazelcastInstance) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Mockito.spy(org.mockito.Mockito.spy) Mockito.verify(org.mockito.Mockito.verify) TestSources(com.hazelcast.jet.pipeline.test.TestSources) IndexRequest(org.elasticsearch.action.index.IndexRequest) ClientHolder(com.hazelcast.jet.elastic.ElasticSinkBuilderTest.ClientHolder) After(org.junit.After) TestHazelcastFactory(com.hazelcast.client.test.TestHazelcastFactory) HttpHost(org.apache.http.HttpHost) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) RefreshPolicy(org.elasticsearch.action.support.WriteRequest.RefreshPolicy) Collections(java.util.Collections) Sink(com.hazelcast.jet.pipeline.Sink) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Test(org.junit.Test)

Example 19 with RestClientBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClientBuilder 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 20 with RestClientBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClientBuilder in project nutch by apache.

the class ElasticIndexWriter method makeClient.

/**
 * Generates a RestHighLevelClient with the hosts given
 * @param parameters implementation specific {@link org.apache.nutch.indexer.IndexWriterParams}
 * @return an initialized {@link org.elasticsearch.client.RestHighLevelClient}
 * @throws IOException if there is an error reading the
 * {@link org.apache.nutch.indexer.IndexWriterParams}
 */
protected RestHighLevelClient makeClient(IndexWriterParams parameters) throws IOException {
    hosts = parameters.getStrings(ElasticConstants.HOSTS);
    port = parameters.getInt(ElasticConstants.PORT, DEFAULT_PORT);
    scheme = parameters.get(ElasticConstants.SCHEME, HttpHost.DEFAULT_SCHEME_NAME);
    auth = parameters.getBoolean(ElasticConstants.USE_AUTH, false);
    user = parameters.get(ElasticConstants.USER, DEFAULT_USER);
    password = parameters.get(ElasticConstants.PASSWORD, "");
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
    RestHighLevelClient client = null;
    if (hosts != null && port > 1) {
        HttpHost[] hostsList = new HttpHost[hosts.length];
        int i = 0;
        for (String host : hosts) {
            hostsList[i++] = new HttpHost(host, port, scheme);
        }
        RestClientBuilder restClientBuilder = RestClient.builder(hostsList);
        if (auth) {
            restClientBuilder.setHttpClientConfigCallback(new HttpClientConfigCallback() {

                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder arg0) {
                    return arg0.setDefaultCredentialsProvider(credentialsProvider);
                }
            });
        }
        client = new RestHighLevelClient(restClientBuilder);
    } else {
        throw new IOException("ElasticRestClient initialization Failed!!!\\n\\nPlease Provide the hosts");
    }
    return client;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) HttpHost(org.apache.http.HttpHost) HttpClientConfigCallback(org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback)

Aggregations

RestClientBuilder (org.elasticsearch.client.RestClientBuilder)34 HttpHost (org.apache.http.HttpHost)27 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)23 RestClient (org.elasticsearch.client.RestClient)15 IOException (java.io.IOException)11 CredentialsProvider (org.apache.http.client.CredentialsProvider)11 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 KeyStore (java.security.KeyStore)6 List (java.util.List)6 Locale (java.util.Locale)6 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