Search in sources :

Example 16 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project sagacity-sqltoy by chenrenfei.

the class ElasticEndpoint method initRestClient.

/**
 * @param restClient
 *            the restClient to set
 */
public void initRestClient() {
    if (StringUtil.isBlank(this.getUrl()))
        return;
    if (restClient == null) {
        String splitSign = ";";
        if (this.getUrl().indexOf(";") == -1)
            splitSign = ",";
        String[] urls = this.getUrl().split(splitSign);
        // 当为单一地址时使用httpclient直接调用
        if (urls.length < 2)
            return;
        List<HttpHost> hosts = new ArrayList<HttpHost>();
        for (String urlStr : urls) {
            try {
                if (StringUtil.isNotBlank(urlStr)) {
                    URL url = new java.net.URL(urlStr.trim());
                    hosts.add(new HttpHost(url.getHost(), url.getPort(), url.getProtocol()));
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        if (!hosts.isEmpty()) {
            HttpHost[] hostAry = new HttpHost[hosts.size()];
            hosts.toArray(hostAry);
            RestClientBuilder builder = RestClient.builder(hostAry);
            final ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Charset.forName(this.charset == null ? "UTF-8" : this.charset)).build();
            RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(this.requestTimeout).setConnectTimeout(this.connectTimeout).setSocketTimeout(this.socketTimeout).build();
            final CredentialsProvider credsProvider = new BasicCredentialsProvider();
            final boolean hasCrede = (StringUtil.isNotBlank(this.getUsername()) && StringUtil.isNotBlank(getPassword())) ? true : false;
            // 凭据提供器
            if (hasCrede) {
                credsProvider.setCredentials(AuthScope.ANY, // 认证用户名和密码
                new UsernamePasswordCredentials(getUsername(), getPassword()));
            }
            builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                    httpClientBuilder.setDefaultConnectionConfig(connectionConfig).setDefaultRequestConfig(requestConfig);
                    if (hasCrede)
                        httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
                    return httpClientBuilder;
                }
            });
            restClient = builder.build();
        }
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) MalformedURLException(java.net.MalformedURLException) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ArrayList(java.util.ArrayList) 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) HttpHost(org.apache.http.HttpHost) ConnectionConfig(org.apache.http.config.ConnectionConfig)

Example 17 with RestClientBuilder

use of 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 18 with RestClientBuilder

use of 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 19 with RestClientBuilder

use of 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 20 with RestClientBuilder

use of 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)

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 List (java.util.List)6 Locale (java.util.Locale)6 KeyStore (java.security.KeyStore)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