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