Search in sources :

Example 41 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project beam by apache.

the class ElasticsearchIO method getBackendVersion.

static int getBackendVersion(ConnectionConfiguration connectionConfiguration) {
    try (RestClient restClient = connectionConfiguration.createClient()) {
        Request request = new Request("GET", "");
        Response response = restClient.performRequest(request);
        JsonNode jsonNode = parseResponse(response.getEntity());
        int backendVersion = Integer.parseInt(jsonNode.path("version").path("number").asText().substring(0, 1));
        checkArgument(VALID_CLUSTER_VERSIONS.contains(backendVersion), "The Elasticsearch version to connect to is %s.x. " + "This version of the ElasticsearchIO is only compatible with " + "Elasticsearch v7.x, v6.x, v5.x and v2.x", backendVersion);
        return backendVersion;
    } catch (IOException e) {
        throw new IllegalArgumentException("Cannot get Elasticsearch version", e);
    }
}
Also used : Response(org.elasticsearch.client.Response) RestClient(org.elasticsearch.client.RestClient) Request(org.elasticsearch.client.Request) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 42 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project janusgraph by JanusGraph.

the class RestClientSetup method connect.

public ElasticSearchClient connect(Configuration config) throws IOException {
    log.debug("Configuring RestClient");
    final List<HttpHost> hosts = new ArrayList<>();
    final int defaultPort = config.has(INDEX_PORT) ? config.get(INDEX_PORT) : ElasticSearchIndex.HOST_PORT_DEFAULT;
    final String httpScheme = config.get(ElasticSearchIndex.SSL_ENABLED) ? "https" : "http";
    for (String host : config.get(INDEX_HOSTS)) {
        String[] hostStringParts = host.split(":");
        String hostname = hostStringParts[0];
        int hostPort = defaultPort;
        if (hostStringParts.length == 2)
            hostPort = Integer.parseInt(hostStringParts[1]);
        log.debug("Configured remote host: {} : {}", hostname, hostPort);
        hosts.add(new HttpHost(hostname, hostPort, httpScheme));
    }
    final RestClient rc = getRestClient(hosts.toArray(new HttpHost[hosts.size()]), config);
    final int scrollKeepAlive = config.get(ElasticSearchIndex.ES_SCROLL_KEEP_ALIVE);
    Preconditions.checkArgument(scrollKeepAlive >= 1, "Scroll keep-alive should be greater than or equal to 1");
    final boolean useMappingTypesForES7 = config.get(ElasticSearchIndex.USE_MAPPING_FOR_ES7);
    final RestElasticSearchClient client = getElasticSearchClient(rc, scrollKeepAlive, useMappingTypesForES7);
    if (config.has(ElasticSearchIndex.BULK_REFRESH)) {
        client.setBulkRefresh(config.get(ElasticSearchIndex.BULK_REFRESH));
    }
    Integer retryOnConflict = config.has(ElasticSearchIndex.RETRY_ON_CONFLICT) ? config.get(ElasticSearchIndex.RETRY_ON_CONFLICT) : null;
    client.setRetryOnConflict(retryOnConflict);
    return client;
}
Also used : HttpHost(org.apache.http.HttpHost) ArrayList(java.util.ArrayList) RestClient(org.elasticsearch.client.RestClient)

Aggregations

RestClient (org.elasticsearch.client.RestClient)41 HttpHost (org.apache.http.HttpHost)21 IOException (java.io.IOException)15 Response (org.elasticsearch.client.Response)9 HttpEntity (org.apache.http.HttpEntity)6 BasicHeader (org.apache.http.message.BasicHeader)6 ResponseException (org.elasticsearch.client.ResponseException)6 Test (org.junit.jupiter.api.Test)6 RestClientBuilder (org.elasticsearch.client.RestClientBuilder)5 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 AuthScope (org.apache.http.auth.AuthScope)4 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 List (java.util.List)3 StringEntity (org.apache.http.entity.StringEntity)3 IndexRequest (org.elasticsearch.action.index.IndexRequest)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 TestHazelcastFactory (com.hazelcast.client.test.TestHazelcastFactory)2