use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project warn-report by saaavsaaa.
the class ElasticClient method search.
@Test
public void search() throws IOException {
String elasticsearchHost = "sl010a-analysisdb1:9200,sl010a-analysisdb2:9200,sl010a-analysisdb3:9200";
String[] elasticHosts = elasticsearchHost.split(",");
HttpHost[] httpHosts = new HttpHost[elasticHosts.length];
for (int i = 0; i < elasticHosts.length; i++) {
String ip = elasticHosts[i].split(":")[0];
String port = elasticHosts[i].split(":")[1];
httpHosts[i] = new HttpHost(ip, Integer.parseInt(port), "https");
}
RestClient restClient = RestClient.builder(httpHosts).setDefaultHeaders(new Header[] { new BasicHeader("Authorization", "Basic Q049ZGVtb3VzZXI6Q049ZGVtb3VzZXI=") }).build();
RestHighLevelClient client = new RestHighLevelClient(restClient);
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
System.out.println(searchResponse.getTotalShards());
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project kylo by Teradata.
the class ElasticSearchRestService method commit.
@Override
public void commit(@Nonnull String indexName) {
try (RestClient restClient = buildRestClient()) {
restClient.performRequest(POST_METHOD, getIndexRefreshEndPoint(indexName));
log.debug("Committed index with name {}", indexName);
} catch (ResponseException responseException) {
log.error("Index refresh encountered issues in Elasticsearch for index name {" + indexName + "}", responseException);
} catch (ClientProtocolException clientProtocolException) {
log.error("Http protocol error for refresh for index name {" + indexName + "}", clientProtocolException);
} catch (IOException ioException) {
log.error("IO Error in rest client", ioException);
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project kylo by Teradata.
the class ElasticSearchRestService method delete.
@Override
public void delete(@Nonnull String indexName, @Nonnull String typeName, @Nonnull String id, @Nonnull String schema, @Nonnull String table) {
try (RestClient restClient = buildRestClient()) {
// Delete schema
restClient.performRequest(DELETE_METHOD, getIndexDeleteEndPoint(indexName, typeName, id));
log.info("Deleted schema document for index={}, type={}, id={}", indexName, typeName, id);
final String dataIndexName = "kylo-data";
final String dataIndexType = "hive-data";
// Delete data
if ((restClientConfig.getEsversion() != null) && (restClientConfig.getEsversion().equals(VERSION_TWO))) {
log.debug("Elasticsearch v2");
restClient.performRequest(DELETE_METHOD, getDataDeleteEndPointEsV2(dataIndexName, dataIndexType), new HashMap<>(), getDataDeleteRequestBodyDslEsV2(schema, table));
} else {
log.debug("Elasticsearch v5 or above");
restClient.performRequest(POST_METHOD, getDataDeleteEndPoint(dataIndexName, dataIndexType), new HashMap<>(), getDataDeleteRequestBodyDsl(schema, table));
}
log.info("Deleted data for index={}, type={}, schema={}, table={}", dataIndexName, dataIndexType, schema, table);
} catch (ResponseException responseException) {
log.error("Index document deletion encountered issues in Elasticsearch for index={}, type={}, id={}", indexName, typeName, responseException);
} catch (ClientProtocolException clientProtocolException) {
log.error("Http protocol error for delete document for index={}, type={}, id={}", indexName, typeName, id, clientProtocolException);
} catch (IOException ioException) {
log.error("IO Error in rest client", ioException);
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project incubator-gobblin by apache.
the class ElasticsearchRestWriter method buildRestClient.
// TODO: Support pass through of configuration (e.g. timeouts etc) of rest client from above
private static RestClient buildRestClient(List<InetSocketTransportAddress> hosts, int threadCount, boolean sslEnabled, String keyStoreType, String keyStoreFilePassword, String identityFilepath, String trustStoreType, String trustStoreFilePassword, String cacertsFilepath) throws Exception {
HttpHost[] httpHosts = new HttpHost[hosts.size()];
String scheme = sslEnabled ? "https" : "http";
for (int h = 0; h < httpHosts.length; h++) {
InetSocketTransportAddress host = hosts.get(h);
httpHosts[h] = new HttpHost(host.getAddress(), host.getPort(), scheme);
}
RestClientBuilder builder = RestClient.builder(httpHosts);
if (sslEnabled) {
log.info("ssl configuration: trustStoreType = {}, cacertsFilePath = {}", trustStoreType, cacertsFilepath);
KeyStore truststore = KeyStore.getInstance(trustStoreType);
FileInputStream trustInputStream = new FileInputStream(cacertsFilepath);
try {
truststore.load(trustInputStream, trustStoreFilePassword.toCharArray());
} finally {
trustInputStream.close();
}
SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null);
log.info("ssl key configuration: keyStoreType = {}, keyFilePath = {}", keyStoreType, identityFilepath);
KeyStore keystore = KeyStore.getInstance(keyStoreType);
FileInputStream keyInputStream = new FileInputStream(identityFilepath);
try {
keystore.load(keyInputStream, keyStoreFilePassword.toCharArray());
} finally {
keyInputStream.close();
}
sslBuilder.loadKeyMaterial(keystore, keyStoreFilePassword.toCharArray());
final SSLContext sslContext = sslBuilder.build();
builder = builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(threadCount).build()));
} else {
builder = builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(threadCount).build()));
}
// Configure timeouts
builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectionRequestTimeout(// Important, otherwise the client has spurious timeouts
0));
return builder.build();
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project incubator-gobblin by apache.
the class RestWriterVariant method getTestClient.
@Override
public TestClient getTestClient(Config config) throws IOException {
final ElasticsearchRestWriter restWriter = new ElasticsearchRestWriter(config);
final RestHighLevelClient highLevelClient = restWriter.getRestHighLevelClient();
return new TestClient() {
@Override
public GetResponse get(GetRequest getRequest) throws IOException {
return highLevelClient.get(getRequest);
}
@Override
public void recreateIndex(String indexName) throws IOException {
RestClient restClient = restWriter.getRestLowLevelClient();
try {
restClient.performRequest("DELETE", "/" + indexName);
} catch (Exception e) {
// ok since index may not exist
}
String indexSettings = "{\"settings\" : {\"index\":{\"number_of_shards\":1,\"number_of_replicas\":1}}}";
HttpEntity entity = new StringEntity(indexSettings, ContentType.APPLICATION_JSON);
Response putResponse = restClient.performRequest("PUT", "/" + indexName, Collections.emptyMap(), entity);
Assert.assertEquals(putResponse.getStatusLine().getStatusCode(), 200, "Recreate index succeeded");
}
@Override
public void close() throws IOException {
restWriter.close();
}
};
}
Aggregations