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);
}
}
}
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);
}
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);
}
}
}
}
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;
}
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;
}
Aggregations