Search in sources :

Example 6 with RestClient

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

the class SnifferBuilderTests method testBuild.

public void testBuild() throws Exception {
    int numNodes = RandomNumbers.randomIntBetween(getRandom(), 1, 5);
    HttpHost[] hosts = new HttpHost[numNodes];
    for (int i = 0; i < numNodes; i++) {
        hosts[i] = new HttpHost("localhost", 9200 + i);
    }
    try (RestClient client = RestClient.builder(hosts).build()) {
        try {
            Sniffer.builder(null).build();
            fail("should have failed");
        } catch (NullPointerException e) {
            assertEquals("restClient cannot be null", e.getMessage());
        }
        try {
            Sniffer.builder(client).setSniffIntervalMillis(RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
            fail("should have failed");
        } catch (IllegalArgumentException e) {
            assertEquals("sniffIntervalMillis must be greater than 0", e.getMessage());
        }
        try {
            Sniffer.builder(client).setSniffAfterFailureDelayMillis(RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
            fail("should have failed");
        } catch (IllegalArgumentException e) {
            assertEquals("sniffAfterFailureDelayMillis must be greater than 0", e.getMessage());
        }
        try {
            Sniffer.builder(client).setHostsSniffer(null);
            fail("should have failed");
        } catch (NullPointerException e) {
            assertEquals("hostsSniffer cannot be null", e.getMessage());
        }
        try (Sniffer sniffer = Sniffer.builder(client).build()) {
            assertNotNull(sniffer);
        }
        SnifferBuilder builder = Sniffer.builder(client);
        if (getRandom().nextBoolean()) {
            builder.setSniffIntervalMillis(RandomNumbers.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
        }
        if (getRandom().nextBoolean()) {
            builder.setSniffAfterFailureDelayMillis(RandomNumbers.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
        }
        if (getRandom().nextBoolean()) {
            builder.setHostsSniffer(new MockHostsSniffer());
        }
        try (Sniffer sniffer = builder.build()) {
            assertNotNull(sniffer);
        }
    }
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient)

Example 7 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project components by Talend.

the class ElasticsearchDatastoreRuntime method doHealthChecks.

@Override
public Iterable<ValidationResult> doHealthChecks(RuntimeContainer container) {
    ValidationResult validationResult;
    try (RestClient client = ElasticsearchConnection.createClient(properties)) {
        Response response = client.performRequest("GET", "/_cluster/health", new HashMap<String, String>(), new BasicHeader("", ""));
        ElasticsearchResponse esResponse = new ElasticsearchResponse(response);
        if (esResponse.isOk()) {
            JsonNode entity = esResponse.getEntity();
            String status = entity.path("status").asText();
            if (status != "red") {
                validationResult = ValidationResult.OK;
            } else {
                validationResult = new ValidationResult(TalendRuntimeException.createUnexpectedException(String.format("Cluster %s status is red", entity.path("cluster_name").asText())));
            }
        } else {
            validationResult = new ValidationResult(TalendRuntimeException.createUnexpectedException(esResponse.getStatusLine().toString()));
        }
    } catch (IOException e) {
        validationResult = new ValidationResult(TalendRuntimeException.createUnexpectedException(e.getMessage()));
    }
    return Arrays.asList(validationResult);
}
Also used : Response(org.elasticsearch.client.Response) RestClient(org.elasticsearch.client.RestClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ValidationResult(org.talend.daikon.properties.ValidationResult) BasicHeader(org.apache.http.message.BasicHeader)

Example 8 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project jnosql-diana-driver by eclipse.

the class ElasticsearchDocumentCollectionManagerFactory method initDatabase.

private void initDatabase(String database) {
    boolean exists = isExists(database);
    if (!exists) {
        InputStream stream = ElasticsearchDocumentCollectionManagerFactory.class.getResourceAsStream('/' + database + ".json");
        if (Objects.nonNull(stream)) {
            try {
                String mappging = getMappging(stream);
                RestClient lowLevelClient = client.getLowLevelClient();
                HttpEntity entity = new NStringEntity(mappging, ContentType.APPLICATION_JSON);
                lowLevelClient.performRequest("PUT", database, Collections.emptyMap(), entity);
            } catch (Exception ex) {
                throw new ElasticsearchException("Error when create a new mapping", ex);
            }
        }
    }
}
Also used : NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) RestClient(org.elasticsearch.client.RestClient) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException)

Example 9 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project sagacity-sqltoy by chenrenfei.

the class HttpClientUtils method doPost.

/**
 * @todo 执行post请求
 * @param sqltoyContext
 * @param nosqlConfig
 * @param url
 * @param jsonObject
 * @return
 * @throws Exception
 */
public static JSONObject doPost(SqlToyContext sqltoyContext, NoSqlConfigModel nosqlConfig, Object postValue) throws Exception {
    ElasticEndpoint esConfig = sqltoyContext.getElasticEndpoint(nosqlConfig.getUrl());
    if (esConfig.getUrl() == null)
        throw new Exception("请正确配置sqltoyContext elasticConfigs 指定es的服务地址!");
    String charset = (nosqlConfig.getCharset() == null) ? CHARSET : nosqlConfig.getCharset();
    HttpEntity httpEntity = new StringEntity(nosqlConfig.isSqlMode() ? postValue.toString() : JSON.toJSONString(postValue), charset);
    ((StringEntity) httpEntity).setContentEncoding(charset);
    ((StringEntity) httpEntity).setContentType(CONTENT_TYPE);
    String realUrl;
    // 返回结果
    HttpEntity reponseEntity = null;
    if (esConfig.getRestClient() != null) {
        realUrl = wrapUrl(esConfig.getPath(), nosqlConfig);
        if (sqltoyContext.isDebug())
            logger.debug("esRestClient执行:URL=[{}],Path={},执行的JSON=[{}]", esConfig.getUrl(), realUrl, JSON.toJSONString(postValue));
        // 默认采用post请求
        RestClient restClient = null;
        try {
            restClient = esConfig.getRestClient();
            Response response = restClient.performRequest("POST", realUrl, Collections.<String, String>emptyMap(), httpEntity);
            reponseEntity = response.getEntity();
        } catch (Exception e) {
            throw e;
        } finally {
            if (restClient != null)
                restClient.close();
        }
    } else {
        realUrl = wrapUrl(esConfig.getUrl(), nosqlConfig);
        HttpPost httpPost = new HttpPost(realUrl);
        if (sqltoyContext.isDebug())
            logger.debug("httpClient执行URL=[{}],执行的JSON=[{}]", realUrl, JSON.toJSONString(postValue));
        httpPost.setEntity(httpEntity);
        // 设置connection是否自动关闭
        httpPost.setHeader("Connection", "close");
        // 自定义超时
        if (nosqlConfig.getRequestTimeout() != 30000 || nosqlConfig.getConnectTimeout() != 10000 || nosqlConfig.getSocketTimeout() != 180000) {
            httpPost.setConfig(RequestConfig.custom().setConnectionRequestTimeout(nosqlConfig.getRequestTimeout()).setConnectTimeout(nosqlConfig.getConnectTimeout()).setSocketTimeout(nosqlConfig.getSocketTimeout()).build());
        } else
            httpPost.setConfig(requestConfig);
        CloseableHttpClient client = null;
        try {
            if (StringUtil.isNotBlank(esConfig.getUsername()) && StringUtil.isNotBlank(esConfig.getPassword())) {
                // 凭据提供器
                CredentialsProvider credsProvider = new BasicCredentialsProvider();
                credsProvider.setCredentials(AuthScope.ANY, // 认证用户名和密码
                new UsernamePasswordCredentials(esConfig.getUsername(), esConfig.getPassword()));
                client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
            } else
                client = HttpClients.createDefault();
            HttpResponse response = client.execute(httpPost);
            reponseEntity = response.getEntity();
        } catch (Exception e) {
            throw e;
        }
    }
    String result = null;
    if (reponseEntity != null) {
        result = EntityUtils.toString(reponseEntity, nosqlConfig.getCharset());
        if (sqltoyContext.isDebug())
            logger.debug("result={}", result);
    }
    if (StringUtil.isBlank(result))
        return null;
    // 将结果转换为JSON对象
    JSONObject json = JSON.parseObject(result);
    // 存在错误
    if (json.containsKey("error")) {
        String errorMessage = JSON.toJSONString(json.getJSONObject("error").getJSONArray("root_cause").get(0));
        logger.error("elastic查询失败,URL:[{}],错误信息:[{}]", nosqlConfig.getUrl(), errorMessage);
        throw new Exception("ElasticSearch查询失败,错误信息:" + errorMessage);
    }
    return json;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpEntity(org.apache.http.HttpEntity) RestClient(org.elasticsearch.client.RestClient) HttpResponse(org.apache.http.HttpResponse) ElasticEndpoint(org.sagacity.sqltoy.config.model.ElasticEndpoint) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Response(org.elasticsearch.client.Response) HttpResponse(org.apache.http.HttpResponse) StringEntity(org.apache.http.entity.StringEntity) JSONObject(com.alibaba.fastjson.JSONObject)

Example 10 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project pinpoint by naver.

the class HighLevelConnectInterceptor method getHostList.

private List<String> getHostList(Object arg) {
    if (!(arg instanceof RestClient)) {
        return Collections.emptyList();
    }
    final List<String> hostList = new ArrayList<>();
    HttpHost[] httpHosts = null;
    if (arg instanceof HttpHostInfoAccessor) {
        httpHosts = ((HttpHostInfoAccessor) arg)._$PINPOINT$_getHttpHostInfo();
    }
    // v6.4 ~
    if (httpHosts == null) {
        for (Node node : ((RestClient) arg).getNodes()) {
            final String hostAddress = HostAndPort.toHostAndPortString(node.getHost().getHostName(), node.getHost().getPort());
            hostList.add(hostAddress);
        }
    } else {
        // v6.0 ~ 6.3
        for (HttpHost httpHost : httpHosts) {
            final String hostAddress = HostAndPort.toHostAndPortString(httpHost.getHostName(), httpHost.getPort());
            hostList.add(hostAddress);
        }
    }
    return hostList;
}
Also used : HttpHostInfoAccessor(com.navercorp.pinpoint.plugin.elasticsearch.accessor.HttpHostInfoAccessor) HttpHost(org.apache.http.HttpHost) Node(org.elasticsearch.client.Node) RestClient(org.elasticsearch.client.RestClient) ArrayList(java.util.ArrayList)

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