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();
}
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();
}
}
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();
}
}
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();
}
}
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;
}
Aggregations