Search in sources :

Example 16 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestHighLevelClient in project sonarqube by SonarSource.

the class EsConnectorImpl method buildRestHighLevelClient.

private RestHighLevelClient buildRestHighLevelClient() {
    HttpHost[] httpHosts = hostAndPorts.stream().map(hostAndPort -> new HttpHost(hostAndPort.getHost(), hostAndPort.getPortOrDefault(9001))).toArray(HttpHost[]::new);
    if (LOG.isDebugEnabled()) {
        String addresses = Arrays.stream(httpHosts).map(t -> t.getHostName() + ":" + t.getPort()).collect(Collectors.joining(", "));
        LOG.debug("Connected to Elasticsearch node: [{}]", addresses);
    }
    RestClientBuilder builder = RestClient.builder(httpHosts).setHttpClientConfigCallback(httpClientBuilder -> {
        if (searchPassword != null) {
            BasicCredentialsProvider provider = getBasicCredentialsProvider(searchPassword);
            httpClientBuilder.setDefaultCredentialsProvider(provider);
        }
        return httpClientBuilder;
    });
    return new RestHighLevelClient(builder);
}
Also used : RestClient(org.elasticsearch.client.RestClient) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) IOException(java.io.IOException) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) TimeValue.timeValueSeconds(org.elasticsearch.core.TimeValue.timeValueSeconds) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) HostAndPort(com.google.common.net.HostAndPort) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) AuthScope(org.apache.http.auth.AuthScope) RequestOptions(org.elasticsearch.client.RequestOptions) Optional(java.util.Optional) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) HttpHost(org.apache.http.HttpHost) Nullable(javax.annotation.Nullable) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

Example 17 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestHighLevelClient in project sonarqube by SonarSource.

the class EsConnectorImpl method getRestHighLevelClient.

private RestHighLevelClient getRestHighLevelClient() {
    RestHighLevelClient res = this.restClient.get();
    if (res != null) {
        return res;
    }
    RestHighLevelClient restHighLevelClient = buildRestHighLevelClient();
    this.restClient.set(restHighLevelClient);
    return restHighLevelClient;
}
Also used : RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

Example 18 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestHighLevelClient in project sonarqube by SonarSource.

the class EsClientProviderTest method es_client_provider_must_add_default_port_when_not_specified.

@Test
public void es_client_provider_must_add_default_port_when_not_specified() {
    settings.setProperty(CLUSTER_ENABLED.getKey(), true);
    settings.setProperty(CLUSTER_NODE_TYPE.getKey(), "application");
    settings.setProperty(CLUSTER_SEARCH_HOSTS.getKey(), format("%s,%s:8081", localhostHostname, localhostHostname));
    EsClient client = underTest.provide(settings.asConfig());
    RestHighLevelClient nativeClient = client.nativeClient();
    assertThat(nativeClient.getLowLevelClient().getNodes()).hasSize(2);
    Node node = nativeClient.getLowLevelClient().getNodes().get(0);
    assertThat(node.getHost().getAddress().getHostName()).isEqualTo(localhostHostname);
    assertThat(node.getHost().getPort()).isEqualTo(9001);
    node = nativeClient.getLowLevelClient().getNodes().get(1);
    assertThat(node.getHost().getAddress().getHostName()).isEqualTo(localhostHostname);
    assertThat(node.getHost().getPort()).isEqualTo(8081);
    assertThat(logTester.logs(LoggerLevel.INFO)).has(new Condition<>(s -> s.contains("Connected to remote Elasticsearch: [http://" + localhostHostname + ":9001, http://" + localhostHostname + ":8081]"), ""));
}
Also used : SEARCH_PORT(org.sonar.process.ProcessProperties.Property.SEARCH_PORT) ES_PORT(org.sonar.process.ProcessProperties.Property.ES_PORT) CLUSTER_ENABLED(org.sonar.process.ProcessProperties.Property.CLUSTER_ENABLED) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Node(org.elasticsearch.client.Node) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) MapSettings(org.sonar.api.config.internal.MapSettings) String.format(java.lang.String.format) InetAddress(java.net.InetAddress) CLUSTER_NODE_TYPE(org.sonar.process.ProcessProperties.Property.CLUSTER_NODE_TYPE) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) CLUSTER_SEARCH_HOSTS(org.sonar.process.ProcessProperties.Property.CLUSTER_SEARCH_HOSTS) Condition(org.assertj.core.api.Condition) CLUSTER_NAME(org.sonar.process.ProcessProperties.Property.CLUSTER_NAME) SEARCH_HOST(org.sonar.process.ProcessProperties.Property.SEARCH_HOST) Before(org.junit.Before) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) Node(org.elasticsearch.client.Node) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Test(org.junit.Test)

Example 19 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestHighLevelClient in project metron by apache.

the class ElasticsearchRequestSubmitterTest method setup.

public ElasticsearchRequestSubmitter setup(SearchResponse response) throws IOException {
    // mocks
    RestHighLevelClient highLevelClient = mock(RestHighLevelClient.class);
    ElasticsearchClient client = new ElasticsearchClient(mock(RestClient.class), highLevelClient);
    // the client should return the given search response
    when(highLevelClient.search(any())).thenReturn(response);
    return new ElasticsearchRequestSubmitter(client);
}
Also used : RestClient(org.elasticsearch.client.RestClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ElasticsearchClient(org.apache.metron.elasticsearch.client.ElasticsearchClient)

Example 20 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.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);
}
Also used : RestClient(org.elasticsearch.client.RestClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) AccessConfig(org.apache.metron.indexing.dao.AccessConfig) ElasticsearchClient(org.apache.metron.elasticsearch.client.ElasticsearchClient) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)56 HttpHost (org.apache.http.HttpHost)22 RestClientBuilder (org.elasticsearch.client.RestClientBuilder)19 IOException (java.io.IOException)12 RestClient (org.elasticsearch.client.RestClient)11 HashMap (java.util.HashMap)9 IndexRequest (org.elasticsearch.action.index.IndexRequest)8 Test (org.junit.jupiter.api.Test)7 SearchRequest (org.elasticsearch.action.search.SearchRequest)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 RequestOptions (org.elasticsearch.client.RequestOptions)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 CredentialsProvider (org.apache.http.client.CredentialsProvider)5 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)5 Test (org.junit.Test)5 TableEnvironment (org.apache.flink.table.api.TableEnvironment)4