use of org.elasticsearch.client.RestHighLevelClient in project metron by apache.
the class ElasticsearchUpdateDaoTest method setup.
@BeforeEach
public void setup() {
accessConfig = new AccessConfig();
retrieveLatestDao = mock(ElasticsearchRetrieveLatestDao.class);
RestHighLevelClient highLevel = mock(RestHighLevelClient.class);
ElasticsearchClient client = new ElasticsearchClient(mock(RestClient.class), highLevel);
updateDao = new ElasticsearchUpdateDao(client, accessConfig, retrieveLatestDao);
}
use of org.elasticsearch.client.RestHighLevelClient in project ranger by apache.
the class ElasticSearchAuditDestination method newClient.
private RestHighLevelClient newClient() {
try {
if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password) && password.contains("keytab") && new File(password).exists()) {
subject = CredentialsProviderUtil.login(user, password);
}
RestClientBuilder restClientBuilder = getRestClientBuilder(hosts, protocol, user, password, port);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
if (LOG.isDebugEnabled()) {
LOG.debug("Initialized client");
}
boolean exits = false;
try {
exits = restHighLevelClient.indices().open(new OpenIndexRequest(this.index), RequestOptions.DEFAULT).isShardsAcknowledged();
} catch (Exception e) {
LOG.warn("Error validating index " + this.index);
}
if (exits) {
if (LOG.isDebugEnabled()) {
LOG.debug("Index exists");
}
} else {
LOG.info("Index does not exist");
}
return restHighLevelClient;
} catch (Throwable t) {
lastLoggedAt.updateAndGet(lastLoggedAt -> {
long now = System.currentTimeMillis();
long elapsed = now - lastLoggedAt;
if (elapsed > TimeUnit.MINUTES.toMillis(1)) {
LOG.error("Can't connect to ElasticSearch server: " + connectionString(), t);
return now;
} else {
return lastLoggedAt;
}
});
return null;
}
}
use of org.elasticsearch.client.RestHighLevelClient in project datashare by ICIJ.
the class ElasticsearchConfiguration method createESClient.
public static RestHighLevelClient createESClient(final PropertiesProvider propertiesProvider) {
System.setProperty("es.set.netty.runtime.available.processors", "false");
try {
URL indexUrl = new URL(propertiesProvider.get(INDEX_ADDRESS_PROP).orElse(DEFAULT_ADDRESS));
HttpHost httpHost = create(format("%s://%s:%d", indexUrl.getProtocol(), indexUrl.getHost(), indexUrl.getPort()));
RestClientBuilder.HttpClientConfigCallback httpClientConfigCallback = httpClientBuilder -> httpClientBuilder;
if (indexUrl.getUserInfo() != null) {
String[] userInfo = indexUrl.getUserInfo().split(":");
LOGGER.info("using credentials from url (user={})", userInfo[0]);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userInfo[0], userInfo[1]));
httpClientConfigCallback = httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHost).setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(5000).setSocketTimeout(60000)).setHttpClientConfigCallback(httpClientConfigCallback));
String clusterName = propertiesProvider.get(CLUSTER_PROP).orElse(ES_CLUSTER_NAME);
return client;
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
use of org.elasticsearch.client.RestHighLevelClient in project datashare by ICIJ.
the class ElasticsearchConfigurationTest method test_create_client_with_user_pass.
@Test
public void test_create_client_with_user_pass() throws Exception {
RestHighLevelClient esClient = ElasticsearchConfiguration.createESClient(new PropertiesProvider(new HashMap<String, String>() {
{
put("elasticsearchAddress", "http://user:pass@elasticsearch:9200");
}
}));
Response response = esClient.getLowLevelClient().performRequest(new Request("GET", TEST_INDEX));
assertThat(EntityUtils.toString(response.getEntity())).contains("settings");
}
use of org.elasticsearch.client.RestHighLevelClient 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